-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_example_test.go
173 lines (134 loc) · 2.73 KB
/
option_example_test.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
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
package typact_test
import (
"fmt"
"slices"
"go.l0nax.org/typact"
)
func assertEq[T comparable](src T, expected T) {
if src != expected {
panic(fmt.Sprintf("expected %v, got %v", expected, src))
}
}
func ExampleOption_Insert() {
opt := typact.None[int]()
val := opt.Insert(5)
fmt.Println(*val)
fmt.Println(opt.Unwrap())
*val = 3
fmt.Println(opt.Unwrap())
// Output:
// 5
// 5
// 3
}
func ExampleOption_Unwrap_some() {
x := typact.Some("foo")
fmt.Println(x.Unwrap())
// Output:
// foo
}
func ExampleOption_Unwrap_none() {
// WARN: This function panics!
x := typact.None[string]()
fmt.Println(x.Unwrap())
}
func ExampleOption_UnwrapOr() {
fmt.Println(typact.Some("foo").UnwrapOr("bar"))
fmt.Println(typact.None[string]().UnwrapOr("world"))
// Output:
// foo
// world
}
func ExampleOption_Inspect() {
x := typact.Some("foo")
x.Inspect(func(val string) {
fmt.Println(val)
})
// Does print nothing
y := typact.None[string]()
y.Inspect(func(val string) {
fmt.Println(val)
})
// Output:
// foo
}
func ExampleOption_Deconstruct() {
x := typact.Some("foo")
val, ok := x.Deconstruct()
fmt.Printf("%q, %t\n", val, ok)
y := typact.None[string]()
val, ok = y.Deconstruct()
fmt.Printf("%q, %t\n", val, ok)
// Output:
// "foo", true
// "", false
}
func ExampleOption_GetOrInsertWith() {
x := typact.None[int]()
{
y := x.GetOrInsertWith(func() int { return 2 })
fmt.Println(*y)
*y = 20
}
fmt.Println(x.Unwrap())
// Output:
// 2
// 20
}
func ExampleOption_IsSomeAnd() {
x := typact.Some("foo")
ok := x.IsSomeAnd(func(str string) bool {
return str == "foo"
})
fmt.Printf("%t\n", ok)
ok = x.IsSomeAnd(func(str string) bool {
return str == ""
})
fmt.Printf("%t\n", ok)
y := typact.None[string]()
ok = y.IsSomeAnd(func(str string) bool {
return str != ""
})
fmt.Printf("%t\n", ok)
// Output:
// true
// false
// false
}
func ExampleOption_Filter_slice() {
x := []typact.Option[string]{
typact.Some("foo"),
typact.None[string](),
typact.Some("bar"),
typact.Some("baz"),
typact.None[string](),
typact.Some("hello"),
typact.Some("world"),
}
x = slices.DeleteFunc(x, func(val typact.Option[string]) bool {
return !val.IsSomeAnd(IsNotZero[string])
})
fmt.Println(x)
// Output:
// [Some(foo) Some(bar) Some(baz) Some(hello) Some(world)]
}
func IsZero[T comparable](val T) bool {
var zero T
return val == zero
}
func IsNotZero[T comparable](val T) bool {
var zero T
return val != zero
}
func ExampleOption_CloneWith_custom() {
cloner := func(x []string) []string {
return slices.Clone(x)
}
x := typact.Some([]string{"Hello", "World"})
y := x.CloneWith(cloner)
// alter elem in x
(*x.UnwrapAsRef())[0] = "Foo"
fmt.Println(y.Unwrap())
// Output:
// [Hello World]
}