-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.go
More file actions
233 lines (205 loc) · 5.49 KB
/
Copy pathoption.go
File metadata and controls
233 lines (205 loc) · 5.49 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package gofp
import (
"encoding/json"
"fmt"
)
// Option is a type that represents an optional value. It is either Some or
// None.
//
// Type parameter T represents the value type.
type Option[T any] struct {
value T
valid bool
}
// Map applies a function to transform the value of an [Option].
func (o Option[T]) Map(fn func(T) T) Option[T] {
return OptionMap(o, fn)
}
// FlatMap composes two [Option] values by using the value of the first to
// create the second.
func (o Option[T]) FlatMap(fn func(T) Option[T]) Option[T] {
return OptionFlatMap(o, fn)
}
// Some returns an [Option] with a value.
func Some[T any](value T) Option[T] {
return Option[T]{value: value, valid: true}
}
// None returns an [Option] with no value.
func None[T any]() Option[T] {
return Option[T]{}
}
// OptionMap applies a function to transform the value type of an
// [Option]. Similar to the [Option.Map] method but allows changing the value
// type.
func OptionMap[T, U any](o Option[T], fn func(T) U) Option[U] {
if !o.valid {
return None[U]()
}
return Some(fn(o.value))
}
// OptionApply applies an [Option] containing a function to an [Option]
// containing a value. This is useful for combining multiple [Option] value when
// the function to combine them is itself an Option.
func OptionApply[T, U any](o Option[T], fn Option[func(T) U]) Option[U] {
if !o.valid || !fn.valid {
return None[U]()
}
return Some(fn.value(o.value))
}
// OptionFlatMap composes two [Option] value by using the result of the first to
// create the second. Similar to the [Option.FlatMap] method but allows changing
// the value type.
func OptionFlatMap[T, U any](o Option[T], fn func(T) Option[U]) Option[U] {
if !o.valid {
return None[U]()
}
return fn(o.value)
}
// OptionSequence transforms a slice of [Option] values into a single [Option]
// of a slice. If all values are Some, it returns Some with a slice of all
// values, preserving order. If any value is None, it returns None.
func OptionSequence[T any](options []Option[T]) Option[[]T] {
values := Some([]T{})
for _, o := range options {
values = OptionFlatMap(values, func(vs []T) Option[[]T] {
return OptionMap(o, func(v T) []T {
return append(vs, v)
})
})
}
return values
}
// OptionFold applies one of two functions to the value of the [Option]
// depending on whether it is Some or None.
func OptionFold[T, R any](o Option[T], none func() R, some func(T) R) R {
if !o.valid {
return none()
}
return some(o.value)
}
func (o Option[T]) String() string {
if o.valid {
return fmt.Sprintf("Some(%v)", o.value)
}
return "None"
}
// IsSome returns true if the [Option] is Some.
func (o Option[T]) IsSome() bool {
return o.valid
}
// IsNone returns true if the [Option] is None.
func (o Option[T]) IsNone() bool {
return !o.valid
}
// TryUnwrap returns the value of the [Option] and a boolean indicating whether
// the [Option] is Some.
func (o Option[T]) TryUnwrap() (T, bool) {
if !o.valid {
var zero T
return zero, false
}
return o.value, true
}
// Unwrap returns the value of the [Option] or panics if the [Option] is None.
func (o Option[T]) Unwrap() T {
if !o.valid {
panic("unwrapping None")
}
return o.value
}
// UnwrapOr returns the value of the [Option] or a default value if the [Option]
// is None.
func (o Option[T]) UnwrapOr(defaultValue T) T {
if !o.valid {
return defaultValue
}
return o.value
}
// UnwrapOrElse returns the value of the [Option] or the result of the given
// function if the [Option] is None.
func (o Option[T]) UnwrapOrElse(fn func() T) T {
if !o.valid {
return fn()
}
return o.value
}
// And returns the receiver [Options] if it is None, otherwise it returns the
// given [Option].
func (o Option[T]) And(opt Option[T]) Option[T] {
if !o.valid {
return None[T]()
}
return opt
}
// AndThen returns the receiver [Option] if it is None, otherwise it returns the
// [Option] produced by the given function.
func (o Option[T]) AndThen(fn func(T) Option[T]) Option[T] {
if !o.valid {
return None[T]()
}
return fn(o.value)
}
// Or returns the receiver [Option] if it is Some, otherwise it returns the
// given [Option].
func (o Option[T]) Or(opt Option[T]) Option[T] {
if o.valid {
return o
}
return opt
}
// OrElse returns the receiver [Option] if it is Some, otherwise it returns the
// [Option] produced by the given function.
func (o Option[T]) OrElse(fn func() Option[T]) Option[T] {
if o.valid {
return o
}
return fn()
}
// Filter converts a Some value to None if it doesn't satisfy the given
// predicate.
func (o Option[T]) Filter(fn func(T) bool) Option[T] {
if !o.valid || !fn(o.value) {
return None[T]()
}
return o
}
func (o Option[T]) MarshalJSON() ([]byte, error) {
if !o.valid {
return nil, nil
}
return json.Marshal(o.value)
}
func (o *Option[T]) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == "null" {
*o = None[T]()
return nil
}
var value T
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
*o = Some(value)
return nil
}
// NullableOption is a variant of [Option] that serializes None as JSON null.
type NullableOption[T any] Option[T]
func (o NullableOption[T]) MarshalJSON() ([]byte, error) {
if !o.valid {
return []byte("null"), nil
}
return json.Marshal(o.value)
}
func (o *NullableOption[T]) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == "null" {
*o = NullableOption[T](None[T]())
return nil
}
var value T
err := json.Unmarshal(data, &value)
if err != nil {
return err
}
*o = NullableOption[T](Some(value))
return nil
}