-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_some.go
57 lines (41 loc) · 869 Bytes
/
option_some.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
package shine
type Some[T any] [1]T
func (s Some[T]) UnwrapOr(def T) T {
return s[0]
}
func (s Some[T]) UnwrapOrDefault() T {
return s[0]
}
func (s Some[T]) UnwrapOrElse(fn func() T) T {
return s[0]
}
func (s Some[T]) OkOr(err error) Result[T] {
return NewOk[T](s[0])
}
func (s Some[T]) OkOrElse(fn func() error) Result[T] {
return NewOk[T](s[0])
}
func (s Some[T]) Filter(predicate func(v T) bool) Option[T] {
if predicate(s[0]) {
return s
}
return NewNone[T]()
}
func (s Some[T]) AndThen(fn func(v T) Option[T]) Option[T] {
return fn(s[0])
}
func (s Some[T]) Xor(other Option[T]) Option[T] {
if _, ok := other.(None[T]); ok {
return s
}
return NewNone[T]()
}
func (Some[T]) option() {
}
func (s Some[T]) Value() T {
return s[0]
}
func NewSome[T any](v T) Some[T] {
return Some[T]{v}
}
var _ Option[struct{}] = (*Some[struct{}])(nil)