tangled
alpha
login
or
join now
oeiuwq.com
/
fx.go
0
fork
atom
An Algebraic Effect System for Golang.
0
fork
atom
overview
issues
pulls
pipelines
Stop
oeiuwq.com
10 months ago
790fe2df
fb0a544f
+14
-15
2 changed files
expand all
collapse all
unified
split
abort
result.go
fx.go
+1
-1
abort/result.go
···
13
13
var err Result[V, E]
14
14
var abortFn AbortFn[E] = func(e E) fx.FxNil {
15
15
err = failure[V](e)
16
16
-
return fx.Halt[fx.Nil, fx.Nil]()
16
16
+
return fx.Stop[fx.Nil, fx.Nil]()
17
17
}
18
18
succeeded := fx.Map(fx.ProvideLeft(e, abortFn), success[V, E])
19
19
failed := func() fx.FxPure[Result[V, E]] { return fx.Pure(err) }
+13
-14
fx.go
···
3
3
type Fx[S, V any] struct {
4
4
imm func() V
5
5
sus func(S) Fx[S, V]
6
6
-
hlt func()
6
6
+
stp func()
7
7
}
8
8
9
9
type FxPure[V any] = Fx[Nil, V]
···
29
29
30
30
func Ctx[V any]() Fx[V, V] { return Func(identity[V]) }
31
31
32
32
-
// An effect that will never be continued.
33
33
-
func Halt[S, V any]() Fx[S, V] { return Fx[S, V]{hlt: func() {}} }
32
32
+
func Stop[S, V any]() Fx[S, V] { return Fx[S, V]{stp: func() {}} }
34
33
35
35
-
// Replace with y if x is already Halted. Otherwise x continues.
34
34
+
// Replace with y if x is already stopped. Otherwise x continues.
36
35
func Replace[S, V any](y func() Fx[S, V]) func(Fx[S, V]) Fx[S, V] {
37
36
return func(x Fx[S, V]) Fx[S, V] {
38
38
-
if x.hlt != nil {
37
37
+
if x.stp != nil {
39
38
return y()
40
39
}
41
40
if x.imm != nil {
···
48
47
// Continue an effect by transforming its immediate value into another effect.
49
48
func Cont[T, U, S, V any](cmap func(T) S, fmap func(V) Fx[T, U]) func(Fx[S, V]) Fx[T, U] {
50
49
return func(e Fx[S, V]) Fx[T, U] {
51
51
-
if e.hlt != nil {
52
52
-
return Halt[T, U]()
50
50
+
if e.stp != nil {
51
51
+
return Stop[T, U]()
53
52
}
54
53
if e.imm != nil {
55
54
return fmap(e.imm())
···
80
79
81
80
func ContraMap[V, S, R any](f func(R) S) func(Fx[S, V]) Fx[R, V] {
82
81
return func(e Fx[S, V]) Fx[R, V] {
83
83
-
if e.hlt != nil {
84
84
-
return Halt[R, V]()
82
82
+
if e.stp != nil {
83
83
+
return Stop[R, V]()
85
84
}
86
85
if e.imm != nil {
87
86
return Const[R](e.imm())
···
161
160
}
162
161
163
162
func ProvideLeft[A, B, V any](e Fx[And[A, B], V], a A) Fx[B, V] {
164
164
-
if e.hlt != nil {
165
165
-
return Halt[B, V]()
163
163
+
if e.stp != nil {
164
164
+
return Stop[B, V]()
166
165
}
167
166
if e.imm != nil {
168
167
return Const[B](e.imm())
···
171
170
var ab And[A, B] = func() (A, B) { return a, b }
172
171
for {
173
172
e = e.sus(ab)
174
174
-
if e.hlt != nil {
175
175
-
return Halt[B, V]()
173
173
+
if e.stp != nil {
174
174
+
return Stop[B, V]()
176
175
}
177
176
if e.imm != nil {
178
177
return Const[B](e.imm())
···
199
198
200
199
func Eval[V any](e Fx[Nil, V]) V {
201
200
for {
202
202
-
if e.hlt != nil {
201
201
+
if e.stp != nil {
203
202
panic("tried to evaluate halted effect. try using fx.Replace with another effect.")
204
203
}
205
204
if e.imm != nil {