-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
102 lines (81 loc) · 2.61 KB
/
option.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package g
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
)
// Some creates an Option containing a non-nil value.
func Some[T any](value T) Option[T] { return Option[T]{&value} }
// None creates an Option containing a nil value.
func None[T any]() Option[T] { return Option[T]{nil} }
// OptionMap applies the given function to the value inside the Option, producing a new Option with the transformed value.
// If the input Option is None, the output Option will also be None.
// Parameters:
// - o: The input Option to map over.
// - fn: The function to apply to the value inside the Option.
//
// Returns:
//
// A new Option with the transformed value.
func OptionMap[T, U any](o Option[T], fn func(T) Option[U]) Option[U] {
if o.IsNone() {
return None[U]()
}
return fn(o.Some())
}
// Some returns the value held in the Option.
func (o Option[T]) Some() T { return *o.value }
// IsSome returns true if the Option contains a non-nil value.
func (o Option[T]) IsSome() bool { return o.value != nil }
// IsNone returns true if the Option contains a nil value.
func (o Option[T]) IsNone() bool { return o.value == nil }
// Unwrap returns the value held in the Option. If the Option contains a nil value, it panics.
func (o Option[T]) Unwrap() T {
if o.IsNone() {
err := errors.New("can't unwrap none value")
if pc, file, line, ok := runtime.Caller(1); ok {
out := fmt.Sprintf("[%s:%d] [%s] %v", filepath.Base(file), line, runtime.FuncForPC(pc).Name(), err)
fmt.Fprintln(os.Stderr, out)
}
panic(err)
}
return o.Some()
}
// UnwrapOr returns the value held in the Option. If the Option contains a nil value, it returns the provided default value.
func (o Option[T]) UnwrapOr(value T) T {
if o.IsNone() {
return value
}
return o.Some()
}
// UnwrapOrDefault returns the value held in the Option. If the Option contains a value,
// it returns the value. If the Option is None, it returns the default value for type T.
func (o Option[T]) UnwrapOrDefault() T {
if o.IsNone() {
return *new(T)
}
return o.Some()
}
// Expect returns the value held in the Option. If the Option contains a nil value, it panics with the provided message.
func (o Option[T]) Expect(msg string) T {
if o.IsNone() {
panic(msg)
}
return o.Some()
}
// Then applies the function fn to the value inside the Option and returns a new Option.
// If the Option is None, it returns the same Option without applying fn.
func (o Option[T]) Then(fn func(T) Option[T]) Option[T] {
if o.IsNone() {
return o
}
return fn(o.Some())
}
func (o Option[T]) String() string {
if o.IsSome() {
return fmt.Sprintf("Some(%v)", o.Some())
}
return "None"
}