-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbranch.jl
More file actions
195 lines (165 loc) · 3.5 KB
/
branch.jl
File metadata and controls
195 lines (165 loc) · 3.5 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
###
### Experimental API
###
# This is very similar to Rust's `Try`` trait and `ControlFlow` enum.
# https://doc.rust-lang.org/std/ops/trait.Try.html
# https://doc.rust-lang.org/std/result/enum.Result.html
# https://doc.rust-lang.org/std/ops/enum.ControlFlow.html
struct Break{T}
result::T
end
struct Continue{T}
result::T
end
function branch end
function resultof end
function valueof end
###
### Implementation
###
branch(ok::Ok) = Continue(ok)
branch(err::Err) = Break(err)
branch(result::AbstractResult) =
if Try.isok(result)
Continue(result)
else
Break(result)
end
resultof(br) = br.result
valueof(br::Continue{<:AbstractResult}) = Try.unwrap(br.result)
valueof(br::Break{<:AbstractResult}) = Try.unwrap_err(br.result)
branch(some::Some) = Continue(some)
branch(::Nothing) = Break(nothing)
valueof(br::Continue{<:Some}) = something(br.result)
valueof(::Break{Nothing}) = nothing
const var"@or_return" = var"@?"
macro or_return(ex) # aka @?
quote
br = branch($(esc(ex)))
if br isa Break
return br.result
else
valueof(br)
end
end
end
macro and_return(ex)
quote
br = branch($(esc(ex)))
if br isa Continue
return br.result
else
valueof(br)
end
end
end
const var"@_return" = Try.var"@return"
macro _return(ex)
quote
br = branch($(esc(ex)))
if br isa Continue
return valueof(br)
else
valueof(br)
end
end
end
function Try.and_then(f, result)
br = branch(result)
if br isa Continue
f(valueof(br))
else
br.result
end
end
function Try.or_else(f, result)
br = branch(result)
if br isa Break
f(valueof(br))
else
br.result
end
end
function Try.unwrap_or_else(f, result)
br = branch(result)
if br isa Break
f(valueof(br))
else
valueof(br)
end
end
Try.and(result) = result
function Try.and(a, b)
br = branch(a)
if br isa Break
br.result
else
b
end
end
Try.and(a, b, c, rest...) = Try.and(Try.and(a, b), c, rest...)
Try.or(result) = result
function Try.or(a, b)
br = branch(a)
if br isa Continue
br.result
else
b
end
end
Try.or(a, b, c, rest...) = Try.or(Try.or(a, b), c, rest...)
macro and(ex, rest...)
exprs = map(esc, Any[ex, rest...])
foldr(exprs; init = pop!(exprs)) do result, ex
br = esc(gensym(:br))
quote
$br = branch($result)
if $br isa Break
$br.result
else
$ex
end
end
end
end
macro or(ex, rest...)
exprs = map(esc, Any[ex, rest...])
foldr(exprs; init = pop!(exprs)) do result, ex
br = esc(gensym(:br))
quote
$br = branch($result)
if $br isa Continue
$br.result
else
$ex
end
end
end
end
function Try.astuple(result)
br = branch(result)
if br isa Break
()
else
(valueof(br),)
end
end
###
### Currying
###
# TODO: Automate currying?
function Try.and_then(f::F) where {F}
function and_then_closure(result)
Try.and_then(f, result)
end
end
function Try.or_else(f::F) where {F}
function or_else_closure(result)
Try.or_else(f, result)
end
end
function Try.unwrap_or_else(f::F) where {F}
function unwrap_or_else(result)
Try.unwrap_or_else(f, result)
end
end