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

cyclops.instances.reactive.PublisherInstances Maven / Gradle / Ivy

The newest version!
package cyclops.instances.reactive;

import com.oath.cyclops.hkt.DataWitness.reactiveSeq;
import com.oath.cyclops.hkt.Higher;
import com.oath.cyclops.react.ThreadPools;
import cyclops.arrow.MonoidK;
import cyclops.arrow.MonoidKs;
import cyclops.control.Either;
import cyclops.control.Maybe;
import cyclops.control.Option;
import cyclops.data.tuple.Tuple2;
import cyclops.function.Monoid;
import cyclops.reactive.ReactiveSeq;
import cyclops.reactive.Spouts;
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.Wither;
import org.reactivestreams.Publisher;

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

import static cyclops.reactive.ReactiveSeq.narrowK;

//typeclass instances for reactive-streams publishers
//via ReactiveSeq Spouts (push based async reactive-streams)
public class PublisherInstances {
  public static  Higher connectHKT(Publisher p){
    return Spouts.from(p);
  }
  public static > R convertHKT(Higher hkt, Function,? extends R> fn){
    return fn.apply(Spouts.narrowK(hkt));
  }
  public static InstanceDefinitions definitions(Executor ex){
    return new InstanceDefinitions() {
      @Override
      public  Functor functor() {
        return PublisherInstances.functor();
      }

      @Override
      public  Pure unit() {
        return PublisherInstances.unit();
      }

      @Override
      public  Applicative applicative() {
        return PublisherInstances.zippingApplicative();
      }

      @Override
      public  Monad monad() {
        return PublisherInstances.monad();
      }

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

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

      @Override
      public  MonadRec monadRec() {
        return PublisherInstances.monadRec(ex);
      }

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

      @Override
      public  Traverse traverse() {
        return PublisherInstances.traverse();
      }

      @Override
      public  Foldable foldable() {
        return PublisherInstances.foldable();
      }

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

      @Override
      public  Option> unfoldable() {
        return Maybe.just(PublisherInstances.unfoldable(ex));
      }
    };
  }
  public static InstanceDefinitions definitions(){
    return definitions(ThreadPools.getCurrentThreadExecutor());
  }

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

        private final MonoidK monoidK;
        public ReactiveSeqTypeClasses(){
            monoidK = MonoidKs.combineReactiveSeq();
        }
        @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 Spouts.of(value);
        }

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


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

        @Override
        public  Higher> traverseA(Applicative ap, Function> fn, Higher ds) {
            ReactiveSeq v = narrowK(ds);
            return v.>>foldLeft(ap.unit(Spouts.empty()),
                (a, b) -> ap.zip(fn.apply(b), a, (sn, vec) -> narrowK(vec).plus(sn)));


        }

        @Override
        public  R foldMap(Monoid mb, Function fn, Higher ds) {
            ReactiveSeq 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 Spouts.unfold(b,fn);
        }


    }
  public static Unfoldable unfoldable(Executor ex){
    return INSTANCE;
  }

  public static Functor functor(){
    return INSTANCE;
  }

  public static  Pure unit(){
    return INSTANCE;
  }

  public static  Applicative zippingApplicative(){
    return INSTANCE;
  }

  public static  Monad monad(){
      return INSTANCE;
  }

  public static  MonadZero monadZero(){
      return INSTANCE;
  }

  public static  MonadPlus monadPlus(){
      return INSTANCE;
  }

  public static  MonadPlus monadPlus(MonoidK m){
      return INSTANCE;
  }
  public static  MonadRec monadRec(Executor ex){

    return new ReactiveSeqTypeClasses(){
      @Override
      public  Higher tailRec(T initial, Function>> fn) {
        return  Spouts.reactive(ReactiveSeq.deferFromStream( ()-> ReactiveSeq.tailRec(initial, fn.andThen(ReactiveSeq::narrowK))),ex);

      }
    };
  }

  public static  Traverse traverse(){
      return INSTANCE;
  }


  public static  Foldable foldable(){
      return INSTANCE;
  }

    public static  Higher> widen2(Higher> flux) {
    // a functor could be used (if C2 is a functor / one exists for C2 type)
    // instead of casting
    // cast seems safer as Higher must be a ReactiveSeq
    return (Higher) flux;
  }


  public static  ReactiveSeq narrow(final Higher completableList) {

    return ((ReactiveSeq) completableList);

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy