cyclops.instances.control.LazyEither3Instances Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-pure Show documentation
Show all versions of cyclops-pure Show documentation
Platform for Functional Reactive Programming with Java 8
The newest version!
package cyclops.instances.control;
import com.oath.cyclops.hkt.DataWitness.lazyEither3;
import com.oath.cyclops.hkt.Higher;
import cyclops.arrow.Cokleisli;
import cyclops.arrow.Kleisli;
import cyclops.control.Either;
import cyclops.control.LazyEither3;
import cyclops.control.Maybe;
import cyclops.control.Option;
import cyclops.function.Monoid;
import cyclops.hkt.Active;
import cyclops.hkt.Nested;
import cyclops.typeclasses.*;
import cyclops.typeclasses.comonad.Comonad;
import cyclops.typeclasses.foldable.Foldable;
import cyclops.typeclasses.foldable.Unfoldable;
import cyclops.arrow.MonoidK;
import cyclops.typeclasses.functor.Functor;
import cyclops.typeclasses.monad.*;
import lombok.AllArgsConstructor;
import java.util.function.Function;
import static cyclops.control.LazyEither3.narrowK;
public interface LazyEither3Instances {
public static Kleisli, LT2>,LazyEither3,T> kindKleisli(){
return Kleisli.of(LazyEither3Instances.monad(), LazyEither3::widen);
}
public static Cokleisli,LT2>,T,LazyEither3> kindCokleisli(){
return Cokleisli.of(LazyEither3::narrowK);
}
public static Active, LT2>,RT> allTypeclasses(LazyEither3 l3){
return Active.of(l3, LazyEither3Instances.definitions());
}
public static Nested, LT2>,W2,R> mapM(LazyEither3 l3, Function super RT,? extends Higher> fn, InstanceDefinitions defs){
return Nested.of(l3.map(fn), LazyEither3Instances.definitions(), defs);
}
public static InstanceDefinitions, L2>> definitions() {
return new InstanceDefinitions, L2>> () {
@Override
public Functor, L2>> functor() {
return LazyEither3Instances.functor();
}
@Override
public Pure, L2>> unit() {
return LazyEither3Instances.unit();
}
@Override
public Applicative, L2>> applicative() {
return LazyEither3Instances.applicative();
}
@Override
public Monad, L2>> monad() {
return LazyEither3Instances.monad();
}
@Override
public Option, L2>>> monadZero() {
return Option.none();
}
@Override
public Option, L2>>> monadPlus() {
return Option.none();
}
@Override
public MonadRec, L2>> monadRec() {
return LazyEither3Instances.monadRec();
}
@Override
public Option, L2>>> monadPlus(MonoidK, L2>> m) {
return Maybe.nothing();
}
@Override
public Traverse, L2>> traverse() {
return LazyEither3Instances.traverse();
}
@Override
public Foldable, L2>> foldable() {
return LazyEither3Instances.foldable();
}
@Override
public Option, L2>>> comonad() {
return Option.none();
}
@Override
public Option, L2>>> unfoldable() {
return Option.none();
}
};
}
final static LazyEither3Typeclasses INSTANCE = new LazyEither3Typeclasses<>();
public static LazyEither3Typeclasses getInstance(){
return INSTANCE;
}
@AllArgsConstructor
public static class LazyEither3Typeclasses implements Monad, L2>>,
MonadRec, L2>>,
Traverse, L2>>,
Foldable, L2>>{
@Override
public T foldRight(Monoid monoid, Higher, L2>, T> ds) {
return narrowK(ds).fold(monoid);
}
@Override
public T foldLeft(Monoid monoid, Higher, L2>, T> ds) {
return narrowK(ds).fold(monoid);
}
@Override
public R foldMap(Monoid mb, Function super T, ? extends R> fn, Higher, L2>, T> nestedA) {
return foldLeft(mb,narrowK(nestedA).map(fn));
}
@Override
public Higher, L2>, R> flatMap(Function super T, ? extends Higher, L2>, R>> fn, Higher, L2>, T> ds) {
return narrowK(ds).flatMap(fn.andThen(m->narrowK(m)));
}
@Override
public Higher, L2>, R>> traverseA(Applicative applicative, Function super T, ? extends Higher> fn, Higher, L2>, T> ds) {
LazyEither3 maybe = narrowK(ds);
return maybe.fold(left-> applicative.unit(LazyEither3.left1(left)),
middle->applicative.unit(LazyEither3.left2(middle)),
right->applicative.map(m-> LazyEither3.right(m), fn.apply(right)));
}
@Override
public Higher, L2>, T>> sequenceA(Applicative applicative, Higher, L2>, Higher> ds) {
return null;
}
@Override
public Higher, L2>, R> ap(Higher, L2>, ? extends Function> fn, Higher, L2>, T> apply) {
return narrowK(fn).flatMap(x -> narrowK(apply).map(x));
}
@Override
public Higher, L2>, T> unit(T value) {
return LazyEither3.right(value);
}
@Override
public Higher, L2>, R> map(Function super T, ? extends R> fn, Higher, L2>, T> ds) {
return null;
}
@Override
public Higher, L2>, R> tailRec(T initial, Function super T, ? extends Higher, L2>, ? extends Either>> fn) {
return narrowK(fn.apply(initial)).flatMap( eval ->
eval.fold(s->narrowK(tailRec(s,fn)), p-> LazyEither3.right(p)));
}
}
public static Functor, L2>> functor() {
return new Functor, L2>>() {
@Override
public Higher, L2>, R> map(Function super T, ? extends R> fn, Higher, L2>, T> ds) {
return narrowK(ds).map(fn);
}
};
}
public static Pure, L2>> unit() {
return INSTANCE;
}
public static Applicative, L2>> applicative() {
return INSTANCE;
}
public static Monad, L2>> monad() {
return INSTANCE;
}
public static MonadRec, L2>> monadRec(){
return INSTANCE;
}
public static Traverse, L2>> traverse() {
return INSTANCE;
}
public static Foldable, L2>> foldable() {
return INSTANCE;
}
}