cyclops.instances.data.tuple.Tuple1Instances 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.data.tuple;
import com.oath.cyclops.hkt.DataWitness.tuple1;
import com.oath.cyclops.hkt.Higher;
import cyclops.control.Either;
import cyclops.control.Identity;
import cyclops.control.Maybe;
import cyclops.control.Option;
import cyclops.data.tuple.Tuple1;
import cyclops.function.Monoid;
import cyclops.arrow.Cokleisli;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.arrow.Kleisli;
import cyclops.typeclasses.Pure;
import cyclops.typeclasses.comonad.Comonad;
import cyclops.typeclasses.comonad.ComonadByPure;
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.data.tuple.Tuple1.narrowK;
public class Tuple1Instances {
public static Identity toIdentity(Tuple1 t1){
return Identity.of(t1._1());
}
public static InstanceDefinitions definitions(){
return new InstanceDefinitions() {
@Override
public Functor functor() {
return Tuple1Instances.functor();
}
@Override
public Pure unit() {
return Tuple1Instances.unit();
}
@Override
public Applicative applicative() {
return Tuple1Instances.applicative();
}
@Override
public Monad monad() {
return Tuple1Instances.monad();
}
@Override
public Option> monadZero() {
return Option.none();
}
@Override
public Option> monadPlus() {
return Option.none();
}
@Override
public MonadRec monadRec() {
return Tuple1Instances.monadRec();
}
@Override
public Option> monadPlus(MonoidK m) {
return Option.none();
}
@Override
public Traverse traverse() {
return Tuple1Instances.traverse();
}
@Override
public Foldable foldable() {
return Tuple1Instances.foldable();
}
@Override
public Option> comonad() {
return Option.some(Tuple1Instances.comonad());
}
@Override
public Option> unfoldable() {
return Option.none();
}
};
}
private final static Tuple1Typeclasses INSTANCE = new Tuple1Typeclasses();
@AllArgsConstructor
public static class Tuple1Typeclasses implements Monad,
MonadRec,
TraverseByTraverse,
Foldable,
Comonad{
@Override
public T foldRight(Monoid monoid, Higher ds) {
return monoid.apply(narrowK(ds)._1(),monoid.zero());
}
@Override
public T foldLeft(Monoid monoid, Higher ds) {
return monoid.apply(monoid.zero(),narrowK(ds)._1());
}
@Override
public Higher flatMap(Function super T, ? extends Higher> fn, Higher ds) {
return Tuple1.narrowK(ds).flatMap(t-> Tuple1.narrowK(fn.apply(t)));
}
@Override
public Higher> traverseA(Applicative applicative, Function super T, ? extends Higher> fn, Higher ds) {
Tuple1 tuple1 = Tuple1.narrowK(ds);
return applicative.map(Tuple1::of, fn.apply(tuple1._1()));
}
@Override
public R foldMap(Monoid mb, Function super T, ? extends R> fn, Higher ds) {
Tuple1 opt = Tuple1.narrowK(ds).map(fn);
return foldLeft(mb,opt);
}
@Override
public Higher ap(Higher> fn, Higher apply) {
return Tuple1.narrowK(apply).zip(Tuple1.narrowK(fn), (a, b)->b.apply(a));
}
@Override
public Higher unit(T value) {
return Tuple1.of(value);
}
@Override
public Higher map(Function super T, ? extends R> fn, Higher ds) {
return Tuple1.narrowK(ds).map(fn);
}
@Override
public Higher tailRec(T initial, Function super T, ? extends Higher>> fn) {
return Tuple1.tailRec(initial,t-> Tuple1.narrowK(fn.apply(t)));
}
@Override
public Higher> nest(Higher ds) {
return Tuple1.of(ds);
}
@Override
public Higher coflatMap(Function super Higher, R> mapper, Higher ds) {
return Tuple1.of(mapper.apply(ds));
}
@Override
public T extract(Higher ds) {
return Tuple1.narrowK(ds)._1();
}
}
public static Functor functor(){
return INSTANCE;
}
public static Pure unit(){
return INSTANCE;
}
public static Applicative applicative(){
return INSTANCE;
}
public static Monad monad(){
return INSTANCE;
}
public static Comonad comonad(){
return INSTANCE;
}
public static MonadRec monadRec(){
return INSTANCE;
}
public static Traverse traverse(){
return INSTANCE;
}
public static Foldable foldable(){
return INSTANCE;
}
public static Kleisli,T> kindKleisli(){
return Kleisli.of(Tuple1Instances.monad(), Tuple1Instances::widen);
}
public static Higher widen(Tuple1 narrow) {
return narrow;
}
public static Cokleisli> kindCokleisli(){
return Cokleisli.of(Tuple1::narrowK);
}
}