Skip to content

Commit

Permalink
fix: Writer monad
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 1, 2024
1 parent 839ef47 commit 3e09a19
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 5 additions & 2 deletions writer/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"

A "github.com/IBM/fp-go/array"
EQ "github.com/IBM/fp-go/eq"
F "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/utils"
M "github.com/IBM/fp-go/monoid"
Expand All @@ -28,6 +29,8 @@ import (
var (
monoid = A.Monoid[string]()
sg = M.ToSemigroup(monoid)

eq = Eq(A.Eq[string](EQ.FromStrictEquals[string]()), EQ.FromStrictEquals[string]())
)

func getLastName(s utils.Initial) Writer[[]string, string] {
Expand All @@ -47,7 +50,7 @@ func TestBind(t *testing.T) {
Map[[]string](utils.GetFullName),
)

assert.Equal(t, res(), Of[string](monoid)("John Doe")())
assert.True(t, eq.Equals(res, Of[string](monoid)("John Doe")))
}

func TestApS(t *testing.T) {
Expand All @@ -59,5 +62,5 @@ func TestApS(t *testing.T) {
Map[[]string](utils.GetFullName),
)

assert.Equal(t, res(), Of[string](monoid)("John Doe")())
assert.True(t, eq.Equals(res, Of[string](monoid)("John Doe")))
}
19 changes: 19 additions & 0 deletions writer/generic/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ package generic
import (
F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/internal/chain"
FC "github.com/IBM/fp-go/internal/functor"
IO "github.com/IBM/fp-go/io/generic"
M "github.com/IBM/fp-go/monoid"
SG "github.com/IBM/fp-go/semigroup"
T "github.com/IBM/fp-go/tuple"
)

func Tell[GA ~func() T.Tuple3[any, W, SG.Semigroup[W]], W any](s SG.Semigroup[W]) func(W) GA {
return F.Flow2(
F.Bind13of3(T.MakeTuple3[any, W, SG.Semigroup[W]])(nil, s),
IO.Of[GA],
)
}

func Of[GA ~func() T.Tuple3[A, W, SG.Semigroup[W]], W, A any](m M.Monoid[W]) func(A) GA {
return F.Flow2(
F.Bind23of3(T.MakeTuple3[A, W, SG.Semigroup[W]])(m.Empty(), M.ToSemigroup(m)),
Expand Down Expand Up @@ -132,3 +140,14 @@ func MonadListens[GA ~func() T.Tuple3[A, W, SG.Semigroup[W]], GAB ~func() T.Tupl
func Listens[GA ~func() T.Tuple3[A, W, SG.Semigroup[W]], GAB ~func() T.Tuple3[T.Tuple2[A, B], W, SG.Semigroup[W]], FCT ~func(W) B, W, A, B any](f FCT) func(GA) GAB {
return F.Bind2nd(MonadListens[GA, GAB, FCT], f)
}

func MonadFlap[FAB ~func(A) B, GFAB ~func() T.Tuple3[FAB, W, SG.Semigroup[W]], GB ~func() T.Tuple3[B, W, SG.Semigroup[W]], W, A, B any](fab GFAB, a A) GB {
return FC.MonadFlap(
MonadMap[GB, GFAB, func(FAB) B],
fab,
a)
}

func Flap[FAB ~func(A) B, GFAB ~func() T.Tuple3[FAB, W, SG.Semigroup[W]], GB ~func() T.Tuple3[B, W, SG.Semigroup[W]], W, A, B any](a A) func(GFAB) GB {
return FC.Flap(Map[GB, GFAB, func(FAB) B], a)
}
21 changes: 17 additions & 4 deletions writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import (

type Writer[W, A any] IO.IO[T.Tuple3[A, W, S.Semigroup[W]]]

// Tell appends a value to the accumulator
func Tell[W any](s S.Semigroup[W]) func(W) Writer[W, any] {
return G.Tell[Writer[W, any]](s)
}

func Of[A, W any](m M.Monoid[W]) func(A) Writer[W, A] {
return G.Of[Writer[W, A]](m)
}
Expand Down Expand Up @@ -87,21 +92,29 @@ func Evaluate[W, A any](fa Writer[W, A]) A {
}

// MonadCensor modifies the final accumulator value by applying a function
func MonadCensor[FCT ~func(W) W, W, A any](fa Writer[W, A], f FCT) Writer[W, A] {
func MonadCensor[A any, FCT ~func(W) W, W any](fa Writer[W, A], f FCT) Writer[W, A] {
return G.MonadCensor[Writer[W, A]](fa, f)
}

// Censor modifies the final accumulator value by applying a function
func Censor[FCT ~func(W) W, W, A any](f FCT) func(Writer[W, A]) Writer[W, A] {
func Censor[A any, FCT ~func(W) W, W any](f FCT) func(Writer[W, A]) Writer[W, A] {
return G.Censor[Writer[W, A]](f)
}

// MonadListens projects a value from modifications made to the accumulator during an action
func MonadListens[FCT ~func(W) B, W, A, B any](fa Writer[W, A], f FCT) Writer[W, T.Tuple2[A, B]] {
func MonadListens[A any, FCT ~func(W) B, W, B any](fa Writer[W, A], f FCT) Writer[W, T.Tuple2[A, B]] {
return G.MonadListens[Writer[W, A], Writer[W, T.Tuple2[A, B]]](fa, f)
}

// Listens projects a value from modifications made to the accumulator during an action
func Listens[FCT ~func(W) B, W, A, B any](f FCT) func(Writer[W, A]) Writer[W, T.Tuple2[A, B]] {
func Listens[A any, FCT ~func(W) B, W, B any](f FCT) func(Writer[W, A]) Writer[W, T.Tuple2[A, B]] {
return G.Listens[Writer[W, A], Writer[W, T.Tuple2[A, B]]](f)
}

func MonadFlap[W, B, A any](fab Writer[W, func(A) B], a A) Writer[W, B] {
return G.MonadFlap[func(A) B, Writer[W, func(A) B], Writer[W, B]](fab, a)
}

func Flap[W, B, A any](a A) func(Writer[W, func(A) B]) Writer[W, B] {
return G.Flap[func(A) B, Writer[W, func(A) B], Writer[W, B]](a)
}

0 comments on commit 3e09a19

Please sign in to comment.