Skip to content

Commit d1a29b7

Browse files
committed
Move core into package
1 parent 96a4aa7 commit d1a29b7

15 files changed

Lines changed: 263 additions & 256 deletions

File tree

abort/abort.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package abort
22

3-
import fx "github.com/vic/fx.go"
3+
import "github.com/vic/fx.go/fx"
44

55
type AbortFn[E any] func(E) fx.FxNil
66
type AbortAb[E any] = fx.And[AbortFn[E], fx.Nil]

abort/abort_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package abort
33
import (
44
"testing"
55

6-
fx "github.com/vic/fx.go"
6+
"github.com/vic/fx.go/fx"
77
)
88

99
func TestSuccess(t *testing.T) {

abort/result.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package abort
22

3-
import fx "github.com/vic/fx.go"
3+
import "github.com/vic/fx.go/fx"
44

55
type Result[V, E any] func() (*V, *E)
66

fx.go

Lines changed: 0 additions & 250 deletions
This file was deleted.

fx/and.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package fx
2+
3+
type And[A, B any] func() (A, B)
4+
5+
func swap[A, B any](ab And[A, B]) And[B, A] {
6+
var ba And[B, A] = func() (B, A) {
7+
a, b := ab()
8+
return b, a
9+
}
10+
return ba
11+
}
12+
13+
func left[A, B any](ab And[A, B]) A {
14+
a, _ := ab()
15+
return a
16+
}
17+
18+
func right[A, B any](ab And[A, B]) B {
19+
_, b := ab()
20+
return b
21+
}
22+
23+
func AndNil[S, V any](e Fx[S, V]) Fx[And[S, Nil], V] {
24+
return Then[And[S, Nil], V](left, Const)(e)
25+
}
26+
27+
func AndContra[A, B, V any](e Fx[A, V], f func(B) A) Fx[B, V] {
28+
return ContraMap[V](f)(e)
29+
}
30+
31+
func AndSwap[A, B, V any](e Fx[And[A, B], V]) Fx[And[B, A], V] {
32+
return Then[And[B, A], V](swap[B, A], Const)(e)
33+
}
34+
35+
func AndFlat[A, B, V any](e Fx[A, Fx[B, V]]) Fx[And[A, B], V] {
36+
return FlatMap(e, identity)
37+
}
38+
39+
func AndNest[A, B, V any](e Fx[And[A, B], V]) Fx[A, Fx[B, V]] {
40+
return Pending(func(a A) Fx[A, Fx[B, V]] { return Const[A](ProvideLeft(e, a)) })
41+
}
42+
43+
func AndCollapse[A, V any](e Fx[And[A, A], V]) Fx[A, V] {
44+
return Pending(func(a A) Fx[A, V] { return ProvideLeft(e, a) })
45+
}

fx/fx.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package fx
2+
3+
type Fx[S, V any] struct {
4+
// an immediate value
5+
imm func() V
6+
// the continuation of a suspended effect
7+
sus func(S) Fx[S, V]
8+
// the resume function of an stopped effect
9+
res func() Fx[S, V]
10+
}
11+
12+
func Pure[V any](v V) FxPure[V] { return Const[Nil](v) }
13+
14+
func identity[V any](v V) V { return v }
15+
16+
func Const[S, V any](v V) Fx[S, V] { return Fx[S, V]{imm: func() V { return v }} }
17+
18+
func Pending[S, V any](f func(S) Fx[S, V]) Fx[S, V] { return Fx[S, V]{sus: f} }
19+
20+
func Func[S, V any](f func(S) V) Fx[S, V] {
21+
return Pending(func(s S) Fx[S, V] { return Const[S](f(s)) })
22+
}
23+
24+
func Ctx[V any]() Fx[V, V] { return Func(identity[V]) }
25+
26+
// Continue an effect by transforming its immediate value into another effect.
27+
func Then[T, U, S, V any](cmap func(T) S, fmap func(V) Fx[T, U]) func(Fx[S, V]) Fx[T, U] {
28+
return func(e Fx[S, V]) Fx[T, U] {
29+
if e.res != nil {
30+
return Stop(func() Fx[T, U] { return Then(cmap, fmap)(e.res()) })
31+
}
32+
if e.imm != nil {
33+
return fmap(e.imm())
34+
}
35+
return Pending(func(t T) Fx[T, U] { return Then(cmap, fmap)(e.sus(cmap(t))) })
36+
}
37+
}
38+
39+
func Eval[V any](e Fx[Nil, V]) V {
40+
for {
41+
if e.res != nil {
42+
panic("tried to evaluate an stopped effect. try using fx.Resume or fx.Replace on it.")
43+
}
44+
if e.imm != nil {
45+
return e.imm()
46+
}
47+
e = e.sus(PNil)
48+
}
49+
}
File renamed without changes.

fx/handle.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package fx
2+
3+
func Apply[F ~func(I) O, I, O any](i I) Fx[F, O] {
4+
return Map(Ctx[F](), func(f F) O { return f(i) })
5+
}
6+
7+
func Suspend[A ~func(I) Fx[B, O], B, I, O any](i I) Fx[And[A, B], O] {
8+
return AndFlat(Apply[A](i))
9+
}
10+
11+
func Handler[A ~func(I) Fx[B, O], B, I, O any](a A) func(Fx[And[A, B], O]) Fx[B, O] {
12+
return func(e Fx[And[A, B], O]) Fx[B, O] { return ProvideLeft(e, a) }
13+
}
14+
15+
func Handle[F ~func(Fx[And[A, B], O]) Fx[B, O], A ~func(I) Fx[B, O], B, I, O any](i I) Fx[And[F, B], O] {
16+
return Suspend[F](Suspend[A](i))
17+
}

0 commit comments

Comments
 (0)