Skip to content

Commit

Permalink
Merge pull request #1029 from aol/betterWriterTypeclassInstance
Browse files Browse the repository at this point in the history
better Writer Typeclass Instance
  • Loading branch information
johnmcclean authored Jan 31, 2019
2 parents 27d97ac + 56a2586 commit 607578b
Showing 1 changed file with 53 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,12 +82,12 @@ public <T, R> Monad<Higher<writer, W>> monad() {

@Override
public <T, R> Option<MonadZero<Higher<writer, W>>> monadZero() {
return Maybe.nothing();
return Option.none();
}

@Override
public <T> Option<MonadPlus<Higher<writer, W>>> monadPlus() {
return Maybe.nothing();
return Option.none();
}

@Override
Expand All @@ -96,7 +97,7 @@ public <T> MonadRec<Higher<writer, W>> monadRec() {

@Override
public <T> Option<MonadPlus<Higher<writer, W>>> monadPlus(MonoidK<Higher<writer, W>> m) {
return Maybe.nothing();
return Option.none();
}

@Override
Expand All @@ -111,109 +112,87 @@ public <T> Foldable<Higher<writer, W>> foldable() {

@Override
public <T> Option<Comonad<Higher<writer, W>>> comonad() {
return Maybe.nothing();
return Option.none();
}

@Override
public <T> Option<Unfoldable<Higher<writer, W>>> unfoldable() {
return Maybe.nothing();
return Option.none();
}
};
}
public static <W> Functor<Higher<writer, W>> functor() {
return new Functor<Higher<writer, W>>() {
@Override
public <T, R> Higher<Higher<writer, W>, R> map(Function<? super T, ? extends R> fn, Higher<Higher<writer, W>, T> ds) {
return narrowK(ds).map(fn);
}
};
}
public static <W> Pure<Higher<writer, W>> unit(Monoid<W> monoid) {
return new Pure<Higher<writer, W>>() {

@Override
public <T> Higher<Higher<writer, W>, T> unit(T value) {
return Writer.writer(value,monoid);
}
};
}
public static <W> Applicative<Higher<writer, W>> applicative(Monoid<W> monoid) {
return new Applicative<Higher<writer, W>>() {

@Override
public <T, R> Higher<Higher<writer, W>, R> ap(Higher<Higher<writer, W>, ? extends Function<T, R>> fn, Higher<Higher<writer, W>, T> apply) {
Writer<W, ? extends Function<T, R>> f = narrowK(fn);
Writer<W, T> ap = narrowK(apply);
return f.flatMap(fn1->ap.map(a->fn1.apply(a)));
}
@AllArgsConstructor
public static class WriterTypeclasses<W> implements Monad<Higher<writer, W>>,
TraverseByTraverse<Higher<writer, W>>,
MonadRec<Higher<writer, W>> {

@Override
public <T, R> Higher<Higher<writer, W>, R> map(Function<? super T, ? extends R> fn, Higher<Higher<writer, W>, T> ds) {
return WriterInstances.<W>functor().map(fn,ds);
}
private final Monoid<W> monoid;

@Override
public <T> Higher<Higher<writer, W>, T> unit(T value) {
return WriterInstances.<W>unit(monoid).unit(value);
}
};
}
public static <W> Monad<Higher<writer, W>> monad(Monoid<W> monoid) {
return new Monad<Higher<writer, W>>() {


@Override
public <T, R> Higher<Higher<writer, W>, R> ap(Higher<Higher<writer, W>, ? extends Function<T, R>> fn, Higher<Higher<writer, W>, T> apply) {
return WriterInstances.<W>applicative(monoid).ap(fn,apply);
public <T, R> Higher<Higher<writer, W>, R> flatMap(Function<? super T, ? extends Higher<Higher<writer, W>, R>> fn, Higher<Higher<writer, W>, T> ds) {
return narrowK(ds).flatMap(fn.andThen(h->narrowK(h)));
}

@Override
public <T, R> Higher<Higher<writer, W>, R> map(Function<? super T, ? extends R> fn, Higher<Higher<writer, W>, T> ds) {
return WriterInstances.<W>functor().map(fn,ds);
}
public <T, R> Higher<Higher<writer, W>, R> tailRec(T initial, Function<? super T, ? extends Higher<Higher<writer, W>, ? extends Either<T, R>>> fn) {
Writer<W,? extends Either<T, R>> next[] = new Writer[1];
next[0] = Writer.writer(Either.left(initial),monoid);

@Override
public <T> Higher<Higher<writer, W>, T> unit(T value) {
return WriterInstances.<W>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 <T, R> Higher<Higher<writer, W>, R> flatMap(Function<? super T, ? extends Higher<Higher<writer, W>, R>> fn, Higher<Higher<writer, W>, T> ds) {
return narrowK(ds).flatMap(fn.andThen(h->narrowK(h)));
}
};
}
public static <W> Traverse<Higher<writer, W>> traverse(Monoid<W> monoid) {
return new Traverse<Higher<writer, W>>() {
@Override
public <C2, T, R> Higher<C2, Higher<Higher<writer, W>, R>> traverseA(Applicative<C2> applicative, Function<? super T, ? extends Higher<C2, R>> fn, Higher<Higher<writer, W>, T> ds) {
Writer<W, T> w = narrowK(ds);
Higher<C2, R> r = w.fold((t, m) -> fn.apply(t._1()));
Higher<C2, Higher<Higher<writer, W>, R>> x = applicative.map_(r, t -> widen(Writer.writer(t, monoid)));
return x;

}

@Override
public <C2, T> Higher<C2, Higher<Higher<writer, W>, T>> sequenceA(Applicative<C2> applicative, Higher<Higher<writer, W>, Higher<C2, T>> ds) {
return traverseA(applicative,Function.identity(),ds);
Writer<W, T> w = narrowK(ds);
Higher<C2, R> r = w.fold((t, m) -> fn.apply(t._1()));
Higher<C2, Higher<Higher<writer, W>, R>> x = applicative.map_(r, t -> widen(Writer.writer(t, monoid)));
return x;
}

@Override
public <T, R> Higher<Higher<writer, W>, R> ap(Higher<Higher<writer, W>, ? extends Function<T, R>> fn, Higher<Higher<writer, W>, T> apply) {
return WriterInstances.applicative(monoid).ap(fn,apply);
Writer<W, ? extends Function<T, R>> f = narrowK(fn);
Writer<W, T> ap = narrowK(apply);
return f.flatMap(fn1->ap.map(a->fn1.apply(a)));
}

@Override
public <T> Higher<Higher<writer, W>, T> unit(T value) {
return WriterInstances.unit(monoid).unit(value);
return Writer.writer(value,monoid);
}

@Override
public <T, R> Higher<Higher<writer, W>, R> map(Function<? super T, ? extends R> fn, Higher<Higher<writer, W>, T> ds) {
return WriterInstances.<W>functor().map(fn,ds);
return narrowK(ds).map(fn);
}
}
public static <W> Functor<Higher<writer, W>> functor() {
return new Functor<Higher<writer, W>>() {
@Override
public <T, R> Higher<Higher<writer, W>, R> map(Function<? super T, ? extends R> fn, Higher<Higher<writer, W>, T> ds) {
return narrowK(ds).map(fn);
}
};
}
public static <W> Pure<Higher<writer, W>> unit(Monoid<W> monoid) {
return new WriterTypeclasses<>(monoid);
}
public static <W> Applicative<Higher<writer, W>> applicative(Monoid<W> monoid) {
return new WriterTypeclasses<>(monoid);
}
public static <W> Monad<Higher<writer, W>> monad(Monoid<W> monoid) {
return new WriterTypeclasses<>(monoid);
}
public static <W> Traverse<Higher<writer, W>> traverse(Monoid<W> monoid) {
return new WriterTypeclasses<>(monoid);
}

public static <W> Foldable<Higher<writer,W>> foldable() {
Expand All @@ -237,7 +216,7 @@ public <T, R> R foldMap(Monoid<R> mb, Function<? super T, ? extends R> fn, Highe
}
};
}
public static <W, T, R> MonadRec<Higher<writer, W>> monadRec(Monoid<W> monoid) {
public static <W> MonadRec<Higher<writer, W>> monadRec(Monoid<W> monoid) {
return new MonadRec<Higher<writer, W>>() {
@Override
public <T, R> Higher<Higher<writer, W>, R> tailRec(T initial, Function<? super T, ? extends Higher<Higher<writer, W>, ? extends Either<T, R>>> fn) {
Expand Down

0 comments on commit 607578b

Please sign in to comment.