-
Notifications
You must be signed in to change notification settings - Fork 0
/
ior.gleam
206 lines (184 loc) · 4.08 KB
/
ior.gleam
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
import gleam/option.{type Option, None, Some}
pub type Ior(left, right) {
Left(val: left)
Right(val: right)
Both(left: left, right: right)
}
pub fn fold(
ior: Ior(left, right),
case_left: fn(left) -> new,
case_right: fn(right) -> new,
case_both: fn(left, right) -> new,
) -> new {
case ior {
Left(val) -> case_left(val)
Right(val) -> case_right(val)
Both(left, right) -> case_both(left, right)
}
}
pub fn bimap(
ior: Ior(left, right),
left_map: fn(left) -> new_left,
right_map: fn(right) -> new_right,
) -> Ior(new_left, new_right) {
case ior {
Left(val) -> Left(left_map(val))
Right(val) -> Right(right_map(val))
Both(left, right) -> Both(left_map(left), right_map(right))
}
}
pub fn map(ior: Ior(left, right), f: fn(right) -> new) -> Ior(left, new) {
case ior {
Left(val) -> Left(val)
Right(val) -> Right(f(val))
Both(left, right) -> Both(left, f(right))
}
}
pub fn left_map(ior: Ior(left, right), f: fn(left) -> new) -> Ior(new, right) {
case ior {
Left(val) -> Left(f(val))
Right(val) -> Right(val)
Both(left, right) -> Both(f(left), right)
}
}
pub fn flat_map(
ior: Ior(left, right),
combine: fn(left, left) -> left,
f: fn(right) -> Ior(left, new),
) -> Ior(left, new) {
case ior {
Left(val) -> Left(val)
Right(val) -> f(val)
Both(left, right) ->
f(right)
|> left_map(combine(left, _))
}
}
pub fn combine(
ior: Ior(left, right),
ior2: Ior(left, right),
combine_left: fn(left, left) -> left,
combine_right: fn(right, right) -> right,
) -> Ior(left, right) {
let left_map = case
ior2
|> get_left
{
Some(val) -> combine_left(_, val)
None -> fn(val) { val }
}
let right_map = case
ior2
|> get_right
{
Some(val) -> combine_right(_, val)
None -> fn(val) { val }
}
ior
|> bimap(left_map, right_map)
}
pub fn put_left(ior: Ior(left, right), left: new) -> Ior(new, right) {
case ior {
Left(_) -> Left(left)
Right(val) -> Both(left, val)
Both(_, right) -> Both(left, right)
}
}
pub fn put_right(ior: Ior(left, right), right: new) -> Ior(left, new) {
case ior {
Left(value) -> Both(value, right)
Right(_) -> Right(right)
Both(left, _) -> Both(left, right)
}
}
pub fn is_left(ior: Ior(left, right)) -> Bool {
case ior {
Left(_) -> True
_ -> False
}
}
pub fn is_right(ior: Ior(left, right)) -> Bool {
case ior {
Right(_) -> True
_ -> False
}
}
pub fn is_both(ior: Ior(left, right)) -> Bool {
case ior {
Both(_, _) -> True
_ -> False
}
}
pub fn get_left(ior: Ior(left, right)) -> Option(left) {
case ior {
Left(val) -> Some(val)
Right(_) -> None
Both(left, _) -> Some(left)
}
}
pub fn get_right(ior: Ior(left, right)) -> Option(right) {
case ior {
Left(_) -> None
Right(val) -> Some(val)
Both(_, right) -> Some(right)
}
}
pub fn only_left(ior: Ior(left, right)) -> Option(left) {
case ior {
Left(val) -> Some(val)
_ -> None
}
}
pub fn only_right(ior: Ior(left, right)) -> Option(right) {
case ior {
Right(val) -> Some(val)
_ -> None
}
}
pub fn only_both(ior: Ior(left, right)) -> Option(#(left, right)) {
case ior {
Both(left, right) -> Some(#(left, right))
_ -> None
}
}
pub fn as_options(ior: Ior(left, right)) -> #(Option(left), Option(right)) {
case ior {
Left(val) -> #(Some(val), None)
Right(val) -> #(None, Some(val))
Both(left, right) -> #(Some(left), Some(right))
}
}
pub fn swap(ior: Ior(left, right)) -> Ior(right, left) {
case ior {
Left(val) -> Right(val)
Right(val) -> Left(val)
Both(left, right) -> Both(right, left)
}
}
pub fn exists(ior: Ior(left, right), predicate: fn(right) -> Bool) -> Bool {
case
ior
|> get_right
{
Some(val) -> predicate(val)
None -> False
}
}
pub fn for_all(ior: Ior(left, right), predicate: fn(right) -> Bool) -> Bool {
case
ior
|> get_right
{
Some(val) -> predicate(val)
None -> True
}
}
pub fn get_or_else(ior: Ior(left, right), default: right) -> right {
case
ior
|> get_right
{
Some(val) -> val
None -> default
}
}