-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathoption.go
More file actions
67 lines (57 loc) · 1.63 KB
/
option.go
File metadata and controls
67 lines (57 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ldcommon
// Option represents an optional value: every Option is either Some and contains a value, or None, and does not.
type Option[T any] struct {
value T
isDefined bool
}
// Some creates an Option with a value.
func Some[T any](value T) Option[T] {
return Option[T]{value: value, isDefined: true}
}
// None creates an Option with no value.
func None[T any]() Option[T] {
return Option[T]{isDefined: false}
}
// IsSome returns true if the Option contains a value.
func (o Option[T]) IsSome() bool {
return o.isDefined
}
// IsNone returns true if the Option does not contain a value.
func (o Option[T]) IsNone() bool {
return !o.isDefined
}
// Unwrap returns the contained value, or panics if the Option is None.
func (o Option[T]) Unwrap() T {
if o.IsNone() {
panic("called Unwrap on a None Option")
}
return o.value
}
// UnwrapOr returns the contained value, or a provided default if the Option is None.
func (o Option[T]) UnwrapOr(defaultValue T) T {
if o.IsNone() {
return defaultValue
}
return o.value
}
// AsPtr returns a pointer to the contained value, or nil if the Option is None.
func (o Option[T]) AsPtr() *T {
if o.IsNone() {
return nil
}
return &o.value
}
// Map applies a function to the contained value (if any), and returns a new Option containing the result.
func Map[T any, U any](o Option[T], f func(T) U) Option[U] {
if o.IsNone() {
return None[U]()
}
return Some(f(o.value))
}
// OrElse returns the Option if it contains a value, or calls the provided function and returns its result otherwise.
func (o Option[T]) OrElse(f func() Option[T]) Option[T] {
if o.IsSome() {
return o
}
return f()
}