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

cyclops.arrow.SemigroupKs Maven / Gradle / Ivy

There is a newer version: 10.4.1
Show newest version
package cyclops.arrow;

import com.oath.cyclops.hkt.Higher;
import cyclops.control.*;
import com.oath.cyclops.hkt.DataWitness.*;
import com.oath.cyclops.hkt.DataWitness.optional;
import cyclops.data.LazySeq;
import cyclops.data.Seq;
import cyclops.data.Vector;
import cyclops.kinds.CompletableFutureKind;
import cyclops.kinds.OptionalKind;
import cyclops.kinds.StreamKind;
import cyclops.reactive.IO;
import cyclops.reactive.ReactiveSeq;
import cyclops.reactive.Spouts;
import cyclops.reactive.collections.immutable.LinkedListX;
import cyclops.reactive.collections.immutable.PersistentQueueX;
import cyclops.reactive.collections.immutable.PersistentSetX;
import cyclops.reactive.collections.immutable.VectorX;
import cyclops.reactive.collections.mutable.DequeX;
import cyclops.reactive.collections.mutable.ListX;
import cyclops.reactive.collections.mutable.QueueX;
import cyclops.reactive.collections.mutable.SetX;

import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import static com.oath.cyclops.data.ReactiveWitness.*;

public interface SemigroupKs{



    public static SemigroupK optionalPresent() {
        return new SemigroupK() {
          @Override
          public  Higher apply(Higher a, Higher b) {
            return  OptionalKind.narrowK(a).isPresent() ? a : b;
          }
        };
    }
    public static SemigroupK listXConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return ListX.narrowK(a).plusAll(ListX.narrowK(b));
        }
      };

    }


    static SemigroupK setXConcat() {
      return new SemigroupK() {
        @Override
        public  Higher apply(Higher a, Higher b) {
          return SetX.narrowK(a).plusAll(SetX.narrowK(b));
        }
      };

    }



    static SemigroupK queueXConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return QueueX.narrowK(a).plusAll(QueueX.narrowK(b));
        }
      };
    }

    /**
     * @return A combiner for DequeX (concatenates two DequeX into a single DequeX)
     */
    static SemigroupK dequeXConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return DequeX.narrowK(a).plusAll(DequeX.narrowK(b));
        }
      };

    }

    /**
     * @return A combiner for LinkedListX (concatenates two LinkedListX into a single LinkedListX)
     */
    static SemigroupK linkedListXConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return LinkedListX.narrowK(a).plusAll(LinkedListX.narrowK(b));
        }
      };

    }
    static SemigroupK lazySeqConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return LazySeq.narrowK(a).plusAll(LazySeq.narrowK(b));
        }
      };

    }
    static SemigroupK vectorConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return Vector.narrowK(a).plusAll(Vector.narrowK(b));
        }
      };

    }
    static SemigroupK seqConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return Seq.narrowK(a).plusAll(Seq.narrowK(b));
        }
      };

    }

    /**
     * @return A combiner for VectorX (concatenates two VectorX into a single VectorX)
     */
    static SemigroupK vectorXConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return VectorX.narrowK(a).plusAll(VectorX.narrowK(b));
        }
      };
    }


    static SemigroupK persistentQueueXConcat() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return PersistentQueueX.narrowK(a).plusAll(PersistentQueueX.narrowK(b));
        }
      };
    }
  static SemigroupK persistentSetXConcat() {
    return new SemigroupK() {

      @Override
      public  Higher apply(Higher a, Higher b) {
        return PersistentSetX.narrowK(a).plusAll(PersistentSetX.narrowK(b));
      }
    };
  }


    static SemigroupK combineIO() {
        return new SemigroupK() {

            @Override
            public  Higher apply(Higher a, Higher b) {
                return  IO.fromPublisher(Spouts.concat(IO.narrowK(a).stream(),IO.narrowK(b).stream()));
            }
        };

    }

    static SemigroupK combineReactiveSeq() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return  ReactiveSeq.narrowK(a).appendStream(ReactiveSeq.narrowK(b));
        }
      };

    }

    static SemigroupK firstNonEmptyReactiveSeq() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return  ReactiveSeq.narrowK(a).onEmptySwitch(()->ReactiveSeq.narrowK(b));
        }
      };

    }
    static SemigroupK ambReactiveSeq() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return Spouts.amb(ReactiveSeq.narrowK(a),ReactiveSeq.narrowK(b));
        }
      };

    }

    static SemigroupK mergeLatestReactiveSeq() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return Spouts.mergeLatest(ReactiveSeq.narrowK(a),ReactiveSeq.narrowK(b));
        }
      };

    }



    /**
     * @return Combination of two Stream's : b is appended to a
     */
    static SemigroupK combineStream() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return StreamKind.widen(Stream.concat(StreamKind.narrow(a), StreamKind.narrow(b)));
        }
      };

    }


    /**
     * @return Combine two CompletableFuture's by taking the first present
     */
    static SemigroupK firstCompleteCompletableFuture() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          CompletableFuture x = CompletableFuture.anyOf(CompletableFutureKind.narrowK(a), CompletableFutureKind.narrowK(b));
          return CompletableFutureKind.widen(x);
        }
      };

    }
    /**
     * @return Combine two Future's by taking the first result
     */
    static  SemigroupK firstCompleteFuture() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return Future.anyOf(Future.narrowK(a),Future.narrowK(b));
        }
      };

    }


    /**
     * @return Combine two Future's by taking the first successful
     */
    static  SemigroupK firstSuccessfulFuture() {
      return new SemigroupK() {

        @Override
        public  Higher apply(Higher a, Higher b) {
          return Future.firstSuccess(Future.narrowK(a),Future.narrowK(b));
        }
      };

    }
    /**
     * @return Combine two Xor's by taking the first right
     */
    static  SemigroupK> firstRightEither() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Either.narrowK(a).isRight() ? a : b;
        }
      };

    }
    /**
     * @return Combine two Xor's by taking the first left
     */
    static  SemigroupK> firstLeftEither() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return  Either.narrowK(a).isLeft() ? a : b;
        }
      };
    }
    /**
     * @return Combine two Xor's by taking the last right
     */
    static  SemigroupK> lastRightEither() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Either.narrowK(b).isRight() ? b : a;
        }
      };

    }
    /**
     * @return Combine two Xor's by taking the last left
     */
    static  SemigroupK> lastLeftEither() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Either.narrowK(b).isLeft() ? b : a;
        }
      };
    }
    /**
     * @return Combine two Try's by taking the first right
     */
    static  SemigroupK> firstTrySuccess() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Try.narrowK(a).isSuccess() ? a : b;
        }
      };

    }
    /**
     * @return Combine two Try's by taking the first left
     */
    static  SemigroupK> firstTryFailure() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Try.narrowK(a).isFailure() ? a : b;
        }
      };

    }
    /**
     * @return Combine two Tryr's by taking the last right
     */
    static SemigroupK> lastTrySuccess() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Try.narrowK(b).isSuccess() ? b : a;
        }
      };

    }
    /**
     * @return Combine two Try's by taking the last left
     */
    static  SemigroupK> lastTryFailure() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Try.narrowK(b).isFailure() ? b : a;
        }
      };

    }
    /**
     * @return Combine two Ior's by taking the first right
     */
    static  SemigroupK> firstPrimaryIor() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Ior.narrowK(a).isRight() ? a : b;
        }
      };

    }
    /**
     * @return Combine two Ior's by taking the first left
     */
    static  SemigroupK> firstSecondaryIor() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Ior.narrowK(a).isLeft() ? a : b;
        }
      };

    }
    /**
     * @return Combine two Ior's by taking the last right
     */
    static  SemigroupK> lastPrimaryIor() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Ior.narrowK(b).isRight() ? b : a;
        }
      };

    }
    /**
     * @return Combine two Ior's by taking the last left
     */
    static  SemigroupK> lastSecondaryIor() {
      return new SemigroupK>() {

        @Override
        public  Higher, T> apply(Higher, T> a, Higher, T> b) {
          return Ior.narrowK(b).isLeft() ? b : a;
        }
      };

    }

    /**
     * @return Combine two Maybe's by taking the first present
     */
    static SemigroupK




© 2015 - 2024 Weber Informatics LLC | Privacy Policy