diff --git a/v2/reader/array.go b/v2/reader/array.go index 234e5bd..9ff6942 100644 --- a/v2/reader/array.go +++ b/v2/reader/array.go @@ -37,7 +37,7 @@ import ( // } // r := reader.MonadTraverseArray(numbers, addPrefix) // result := r(Config{Prefix: "num"}) // ["num1", "num2", "num3"] -func MonadTraverseArray[R, A, B any](ma []A, f func(A) Reader[R, B]) Reader[R, []B] { +func MonadTraverseArray[R, A, B any](ma []A, f Kleisli[R, A, B]) Reader[R, []B] { return array.MonadTraverse[[]A]( Of[R, []B], Map[R, []B, func(B) []B], @@ -62,7 +62,7 @@ func MonadTraverseArray[R, A, B any](ma []A, f func(A) Reader[R, B]) Reader[R, [ // transform := reader.TraverseArray(multiply) // r := transform([]int{1, 2, 3}) // result := r(Config{Multiplier: 10}) // [10, 20, 30] -func TraverseArray[R, A, B any](f func(A) Reader[R, B]) func([]A) Reader[R, []B] { +func TraverseArray[R, A, B any](f Kleisli[R, A, B]) func([]A) Reader[R, []B] { return array.Traverse[[]A]( Of[R, []B], Map[R, []B, func(B) []B], diff --git a/v2/reader/bind.go b/v2/reader/bind.go index 1fd489d..aac68ab 100644 --- a/v2/reader/bind.go +++ b/v2/reader/bind.go @@ -83,7 +83,7 @@ func Do[R, S any]( // ) func Bind[R, S1, S2, T any]( setter func(T) func(S1) S2, - f func(S1) Reader[R, T], + f Kleisli[R, S1, T], ) Operator[R, S1, S2] { return chain.Bind( Chain[R, S1, S2], @@ -283,7 +283,7 @@ func ApSL[R, S, T any]( // ) func BindL[R, S, T any]( lens L.Lens[S, T], - f func(T) Reader[R, T], + f Kleisli[R, T, T], ) Operator[R, S, S] { return Bind[R, S, S, T](lens.Set, func(s S) Reader[R, T] { return f(lens.Get(s)) diff --git a/v2/reader/curry.go b/v2/reader/curry.go index 16d11cb..83ddcfd 100644 --- a/v2/reader/curry.go +++ b/v2/reader/curry.go @@ -48,7 +48,7 @@ func Curry0[R, A any](f func(R) A) Reader[R, A] { // curried := reader.Curry1(addPrefix) // r := curried("hello") // result := r(Config{Prefix: ">> "}) // ">> hello" -func Curry1[R, T1, A any](f func(R, T1) A) func(T1) Reader[R, A] { +func Curry1[R, T1, A any](f func(R, T1) A) Kleisli[R, T1, A] { return G.Curry1[Reader[R, A]](f) } @@ -121,7 +121,7 @@ func Uncurry0[R, A any](f Reader[R, A]) func(R) A { // } // f := reader.Uncurry1(curried) // result := f(Config{Prefix: ">> "}, "hello") // ">> hello" -func Uncurry1[R, T1, A any](f func(T1) Reader[R, A]) func(R, T1) A { +func Uncurry1[R, T1, A any](f Kleisli[R, T1, A]) func(R, T1) A { return G.Uncurry1(f) } diff --git a/v2/reader/reader.go b/v2/reader/reader.go index f49cce1..75d32b0 100644 --- a/v2/reader/reader.go +++ b/v2/reader/reader.go @@ -154,7 +154,7 @@ func Of[R, A any](a A) Reader[R, A] { // } // r := reader.MonadChain(getUser, getUserName) // name := r(Config{UserId: 42}) // "User42" -func MonadChain[R, A, B any](ma Reader[R, A], f func(A) Reader[R, B]) Reader[R, B] { +func MonadChain[R, A, B any](ma Reader[R, A], f Kleisli[R, A, B]) Reader[R, B] { return func(r R) B { return f(ma(r))(r) } @@ -172,7 +172,7 @@ func MonadChain[R, A, B any](ma Reader[R, A], f func(A) Reader[R, B]) Reader[R, // } // r := reader.Chain(getUserName)(getUser) // name := r(Config{UserId: 42}) // "User42" -func Chain[R, A, B any](f func(A) Reader[R, B]) Operator[R, A, B] { +func Chain[R, A, B any](f Kleisli[R, A, B]) Operator[R, A, B] { return function.Bind2nd(MonadChain[R, A, B], f) } @@ -187,7 +187,7 @@ func Chain[R, A, B any](f func(A) Reader[R, B]) Operator[R, A, B] { // } // flat := reader.Flatten(nested) // result := flat(Config{Value: 5}) // 10 (5 + 5) -func Flatten[R, A any](mma func(R) Reader[R, A]) Reader[R, A] { +func Flatten[R, A any](mma Reader[R, Reader[R, A]]) Reader[R, A] { return MonadChain(mma, function.Identity[Reader[R, A]]) } diff --git a/v2/reader/types.go b/v2/reader/types.go index 131f7d9..a5c6d28 100644 --- a/v2/reader/types.go +++ b/v2/reader/types.go @@ -47,6 +47,8 @@ type ( // apiKey := getAPIKey(config) // "secret" Reader[R, A any] = func(R) A + Kleisli[R, A, B any] = func(A) Reader[R, B] + // Operator represents a transformation from one Reader to another. // It takes a Reader[R, A] and produces a Reader[R, B], where both readers // share the same environment type R. @@ -69,5 +71,5 @@ type ( // getNumber := reader.Asks(func(c Config) int { return c.Multiplier }) // getString := intToString(getNumber) // result := getString(Config{Multiplier: 42}) // "42" - Operator[R, A, B any] = func(Reader[R, A]) Reader[R, B] + Operator[R, A, B any] = Kleisli[R, Reader[R, A], B] )