From 56a2586861f0f89803c75d5705e8de915b852ca1 Mon Sep 17 00:00:00 2001 From: johnmcclean Date: Thu, 31 Jan 2019 14:35:07 +0000 Subject: [PATCH] better Writer Typeclass Instance --- .../instances/control/WriterInstances.java | 127 ++++++++---------- 1 file changed, 53 insertions(+), 74 deletions(-) diff --git a/cyclops-pure/src/main/java/cyclops/instances/control/WriterInstances.java b/cyclops-pure/src/main/java/cyclops/instances/control/WriterInstances.java index 70d91b1557..c6f0eb21f5 100644 --- a/cyclops-pure/src/main/java/cyclops/instances/control/WriterInstances.java +++ b/cyclops-pure/src/main/java/cyclops/instances/control/WriterInstances.java @@ -15,6 +15,7 @@ import cyclops.hkt.Product; import cyclops.typeclasses.InstanceDefinitions; import cyclops.typeclasses.functor.Functor; +import lombok.AllArgsConstructor; import lombok.experimental.UtilityClass; import java.util.function.Function; @@ -81,12 +82,12 @@ public Monad> monad() { @Override public Option>> monadZero() { - return Maybe.nothing(); + return Option.none(); } @Override public Option>> monadPlus() { - return Maybe.nothing(); + return Option.none(); } @Override @@ -96,7 +97,7 @@ public MonadRec> monadRec() { @Override public Option>> monadPlus(MonoidK> m) { - return Maybe.nothing(); + return Option.none(); } @Override @@ -111,109 +112,87 @@ public Foldable> foldable() { @Override public Option>> comonad() { - return Maybe.nothing(); + return Option.none(); } @Override public Option>> unfoldable() { - return Maybe.nothing(); + return Option.none(); } }; } - public static Functor> functor() { - return new Functor>() { - @Override - public Higher, R> map(Function fn, Higher, T> ds) { - return narrowK(ds).map(fn); - } - }; - } - public static Pure> unit(Monoid monoid) { - return new Pure>() { - - @Override - public Higher, T> unit(T value) { - return Writer.writer(value,monoid); - } - }; - } - public static Applicative> applicative(Monoid monoid) { - return new Applicative>() { - @Override - public Higher, R> ap(Higher, ? extends Function> fn, Higher, T> apply) { - Writer> f = narrowK(fn); - Writer ap = narrowK(apply); - return f.flatMap(fn1->ap.map(a->fn1.apply(a))); - } + @AllArgsConstructor + public static class WriterTypeclasses implements Monad>, + TraverseByTraverse>, + MonadRec> { - @Override - public Higher, R> map(Function fn, Higher, T> ds) { - return WriterInstances.functor().map(fn,ds); - } + private final Monoid monoid; @Override - public Higher, T> unit(T value) { - return WriterInstances.unit(monoid).unit(value); - } - }; - } - public static Monad> monad(Monoid monoid) { - return new Monad>() { - - - @Override - public Higher, R> ap(Higher, ? extends Function> fn, Higher, T> apply) { - return WriterInstances.applicative(monoid).ap(fn,apply); + public Higher, R> flatMap(Function, R>> fn, Higher, T> ds) { + return narrowK(ds).flatMap(fn.andThen(h->narrowK(h))); } @Override - public Higher, R> map(Function fn, Higher, T> ds) { - return WriterInstances.functor().map(fn,ds); - } + public Higher, R> tailRec(T initial, Function, ? extends Either>> fn) { + Writer> next[] = new Writer[1]; + next[0] = Writer.writer(Either.left(initial),monoid); - @Override - public Higher, T> unit(T value) { - return WriterInstances.unit(monoid).unit(value); + boolean cont = true; + do { + cont = next[0].fold((p, __) -> p._1().fold(s -> { + next[0] = narrowK(fn.apply(s)); + return true; + }, pr -> false)); + } while (cont); + return next[0].map(x->x.orElse(null)); } - @Override - public Higher, R> flatMap(Function, R>> fn, Higher, T> ds) { - return narrowK(ds).flatMap(fn.andThen(h->narrowK(h))); - } - }; - } - public static Traverse> traverse(Monoid monoid) { - return new Traverse>() { @Override public Higher, R>> traverseA(Applicative applicative, Function> fn, Higher, T> ds) { - Writer w = narrowK(ds); - Higher r = w.fold((t, m) -> fn.apply(t._1())); - Higher, R>> x = applicative.map_(r, t -> widen(Writer.writer(t, monoid))); - return x; - - } - - @Override - public Higher, T>> sequenceA(Applicative applicative, Higher, Higher> ds) { - return traverseA(applicative,Function.identity(),ds); + Writer w = narrowK(ds); + Higher r = w.fold((t, m) -> fn.apply(t._1())); + Higher, R>> x = applicative.map_(r, t -> widen(Writer.writer(t, monoid))); + return x; } @Override public Higher, R> ap(Higher, ? extends Function> fn, Higher, T> apply) { - return WriterInstances.applicative(monoid).ap(fn,apply); + Writer> f = narrowK(fn); + Writer ap = narrowK(apply); + return f.flatMap(fn1->ap.map(a->fn1.apply(a))); } @Override public Higher, T> unit(T value) { - return WriterInstances.unit(monoid).unit(value); + return Writer.writer(value,monoid); } @Override public Higher, R> map(Function fn, Higher, T> ds) { - return WriterInstances.functor().map(fn,ds); + return narrowK(ds).map(fn); + } + } + public static Functor> functor() { + return new Functor>() { + @Override + public Higher, R> map(Function fn, Higher, T> ds) { + return narrowK(ds).map(fn); } }; + } + public static Pure> unit(Monoid monoid) { + return new WriterTypeclasses<>(monoid); + } + public static Applicative> applicative(Monoid monoid) { + return new WriterTypeclasses<>(monoid); + } + public static Monad> monad(Monoid monoid) { + return new WriterTypeclasses<>(monoid); + } + public static Traverse> traverse(Monoid monoid) { + return new WriterTypeclasses<>(monoid); } public static Foldable> foldable() { @@ -237,7 +216,7 @@ public R foldMap(Monoid mb, Function fn, Highe } }; } - public static MonadRec> monadRec(Monoid monoid) { + public static MonadRec> monadRec(Monoid monoid) { return new MonadRec>() { @Override public Higher, R> tailRec(T initial, Function, ? extends Either>> fn) {