All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cyclops.instances.data.LazySeqInstances Maven / Gradle / Ivy

The newest version!
package cyclops.instances.data;

import com.oath.cyclops.hkt.DataWitness.lazySeq;
import com.oath.cyclops.hkt.Higher;
import cyclops.arrow.Cokleisli;
import cyclops.arrow.Kleisli;
import cyclops.arrow.MonoidK;
import cyclops.arrow.MonoidKs;
import cyclops.control.Either;
import cyclops.control.Maybe;
import cyclops.control.Option;
import cyclops.data.LazySeq;
import cyclops.data.tuple.Tuple2;
import cyclops.function.Monoid;
import cyclops.hkt.Active;
import cyclops.hkt.Coproduct;
import cyclops.hkt.Nested;
import cyclops.hkt.Product;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Pure;
import cyclops.typeclasses.comonad.Comonad;
import cyclops.typeclasses.foldable.Foldable;
import cyclops.typeclasses.foldable.Unfoldable;
import cyclops.typeclasses.functor.Functor;
import cyclops.typeclasses.monad.Applicative;
import cyclops.typeclasses.monad.Monad;
import cyclops.typeclasses.monad.MonadPlus;
import cyclops.typeclasses.monad.MonadRec;
import cyclops.typeclasses.monad.MonadZero;
import cyclops.typeclasses.monad.Traverse;
import cyclops.typeclasses.monad.TraverseByTraverse;
import lombok.AllArgsConstructor;
import lombok.experimental.UtilityClass;
import lombok.experimental.Wither;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import static cyclops.data.LazySeq.narrowK;


@UtilityClass
public class LazySeqInstances {

  public static   Kleisli,T> kindKleisli(){
    return Kleisli.of(LazySeqInstances.monad(), LazySeq::widen);
  }

  public static   Cokleisli> kindCokleisli(){
    return Cokleisli.of(LazySeq::narrowK);
  }
  public static  Nested nested(LazySeq> nested, InstanceDefinitions def2){
    return Nested.of(nested, LazySeqInstances.definitions(),def2);
  }
  public static  Product product(LazySeq l, Active active){
    return Product.of(allTypeclasses(l),active);
  }
  public static  Coproduct coproduct(LazySeq l, InstanceDefinitions def2){
    return Coproduct.right(l,def2, LazySeqInstances.definitions());
  }
  public static  Active allTypeclasses(LazySeq l){
    return Active.of(l, LazySeqInstances.definitions());
  }
  public static  Nested mapM(LazySeq l, Function> fn, InstanceDefinitions defs){
    return Nested.of(l.map(fn), LazySeqInstances.definitions(), defs);
  }

  public static InstanceDefinitions definitions(){
    return new InstanceDefinitions() {
      @Override
      public  Functor functor() {
        return INSTANCE;
      }

      @Override
      public  Pure unit() {
        return INSTANCE;
      }

      @Override
      public  Applicative applicative() {
        return INSTANCE;
      }

      @Override
      public  Monad monad() {
        return INSTANCE;
      }

      @Override
      public  Option> monadZero() {
        return Option.some(INSTANCE);
      }

      @Override
      public  Option> monadPlus() {
        return Option.some(INSTANCE);
      }

      @Override
      public  MonadRec monadRec() {
        return INSTANCE;
      }

      @Override
      public  Option> monadPlus(MonoidK m) {
        return Option.some(LazySeqInstances.monadPlus(m));
      }

      @Override
      public  Traverse traverse() {
        return INSTANCE;
      }

      @Override
      public  Foldable foldable() {
        return INSTANCE;
      }

      @Override
      public  Option> comonad() {
        return Maybe.nothing();
      }

      @Override
      public  Option> unfoldable() {
        return Option.some(INSTANCE);
      }
    };
  }




  private final static LazySeqTypeClasses INSTANCE = new LazySeqTypeClasses();
  @AllArgsConstructor
  @Wither
  public static class LazySeqTypeClasses implements MonadPlus,
                                                    MonadRec,
                                                    TraverseByTraverse,
                                                    Foldable,
                                                    Unfoldable{

      private final MonoidK monoidK;
      public LazySeqTypeClasses(){
          monoidK = MonoidKs.lazySeqConcat();
      }
      @Override
      public  Higher filter(Predicate predicate, Higher ds) {
          return narrowK(ds).filter(predicate);
      }

      @Override
      public  Higher> zip(Higher fa, Higher fb) {
          return narrowK(fa).zip(narrowK(fb));
      }

      @Override
      public  Higher zip(Higher fa, Higher fb, BiFunction f) {
          return narrowK(fa).zip(narrowK(fb),f);
      }

      @Override
      public  MonoidK monoid() {
          return monoidK;
      }

      @Override
      public  Higher flatMap(Function> fn, Higher ds) {
          return narrowK(ds).flatMap(i->narrowK(fn.apply(i)));
      }

      @Override
      public  Higher ap(Higher> fn, Higher apply) {
          return narrowK(apply)
                            .zip(narrowK(fn),(a,b)->b.apply(a));
      }

      @Override
      public  Higher unit(T value) {
          return LazySeq.of(value);
      }

      @Override
      public  Higher map(Function fn, Higher ds) {
          return narrowK(ds).map(fn);
      }

      @Override
      public  Higher tailRec(T initial, Function>> fn) {
          return LazySeq.tailRec(initial,i->narrowK(fn.apply(i)));
      }

      @Override
      public  Higher> traverseA(Applicative ap, Function> fn, Higher ds) {
          LazySeq v = narrowK(ds);

          return v.>>lazyFoldRight(ap.unit(LazySeq.empty()),
              (b, a) ->  ap.zip(fn.apply(b), a.get(), (sn, vec) -> narrowK(vec).plus(sn)));

      }

      @Override
      public  R foldMap(Monoid mb, Function fn, Higher ds) {
          LazySeq x = narrowK(ds);
          return x.foldLeft(mb.zero(),(a,b)->mb.apply(a,fn.apply(b)));
      }

      @Override
      public  Higher> zipWithIndex(Higher ds) {
          return narrowK(ds).zipWithIndex();
      }

      @Override
      public  T foldRight(Monoid monoid, Higher ds) {
          return narrowK(ds).foldRight(monoid);
      }


      @Override
      public  T foldLeft(Monoid monoid, Higher ds) {
          return narrowK(ds).foldLeft(monoid);
      }


      @Override
      public  Higher unfold(T b, Function>> fn) {
          return LazySeq.unfold(b,fn);
      }
  }



  public static MonadPlus monadPlus(MonoidK m){

      return INSTANCE.withMonoidK(m);
  }
    public static  Applicative zippingApplicative(){
      return INSTANCE;
    }
    public static Functor functor(){
        return INSTANCE;
    }

    public static  Monad monad(){
        return INSTANCE;
    }

    public static  MonadZero monadZero(){

        return INSTANCE;
    }

    public static  MonadPlus monadPlus(){

        return INSTANCE;
    }
    public static  MonadRec monadRec(){

        return INSTANCE;
    }


    public static  Traverse traverse(){
        return INSTANCE;
    }

    public static  Foldable foldable(){
      return INSTANCE;
    }
    public static Unfoldable unfoldable(){
        return INSTANCE;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy