-
Notifications
You must be signed in to change notification settings - Fork 661
/
maybe.go
76 lines (64 loc) · 1.94 KB
/
maybe.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package maybe
import "fmt"
// Maybe T = Some T | Nothing.
// A data wrapper that allows values to be something [Some T] or nothing [Nothing].
// Invariant: If [hasValue] is false, then [value] is the zero value of type T.
// Maybe is used to wrap types:
// * That can't be represented by nil.
// * That use nil as a valid value instead of an indicator of a missing value.
// For more info see https://en.wikipedia.org/wiki/Option_type
type Maybe[T any] struct {
hasValue bool
// If [hasValue] is false, [value] is the zero value of type T.
value T
}
// Some returns a new Maybe[T] with the value val.
// If m.IsNothing(), returns the zero value of type T.
func Some[T any](val T) Maybe[T] {
return Maybe[T]{
value: val,
hasValue: true,
}
}
// Nothing returns a new Maybe[T] with no value.
func Nothing[T any]() Maybe[T] {
return Maybe[T]{}
}
// IsNothing returns false iff [m] has a value.
func (m Maybe[T]) IsNothing() bool {
return !m.hasValue
}
// HasValue returns true iff [m] has a value.
func (m Maybe[T]) HasValue() bool {
return m.hasValue
}
// Value returns the value of [m].
func (m Maybe[T]) Value() T {
return m.value
}
func (m Maybe[T]) String() string {
if !m.hasValue {
return fmt.Sprintf("Nothing[%T]", m.value)
}
return fmt.Sprintf("Some[%T]{%v}", m.value, m.value)
}
// Bind returns Nothing iff [m] is Nothing.
// Otherwise applies [f] to the value of [m] and returns the result as a Some.
func Bind[T, U any](m Maybe[T], f func(T) U) Maybe[U] {
if m.IsNothing() {
return Nothing[U]()
}
return Some(f(m.Value()))
}
// Equal returns true if both m1 and m2 are nothing or have the same value according to [equalFunc].
func Equal[T any](m1 Maybe[T], m2 Maybe[T], equalFunc func(T, T) bool) bool {
if m1.IsNothing() {
return m2.IsNothing()
}
if m2.IsNothing() {
return false
}
return equalFunc(m1.Value(), m2.Value())
}