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

cyclops.companion.vavr.Lists Maven / Gradle / Ivy

The newest version!
package cyclops.companion.vavr;

import com.aol.cyclops.vavr.hkt.*;
import cyclops.companion.CompletableFutures;
import cyclops.companion.Optionals;
import cyclops.control.Eval;
import cyclops.control.Maybe;
import cyclops.control.Reader;
import cyclops.control.Xor;
import cyclops.monads.*;
import cyclops.monads.VavrWitness.*;
import cyclops.collections.vavr.VavrListX;
import com.aol.cyclops2.hkt.Higher;
import com.aol.cyclops2.types.anyM.AnyMSeq;
import cyclops.function.Fn3;
import cyclops.function.Fn4;
import cyclops.function.Monoid;
import cyclops.monads.VavrWitness.either;
import cyclops.monads.VavrWitness.future;
import cyclops.monads.VavrWitness.list;
import cyclops.monads.VavrWitness.tryType;
import cyclops.monads.Witness.*;
import cyclops.monads.transformers.ListT;
import cyclops.stream.ReactiveSeq;
import cyclops.typeclasses.*;
import cyclops.typeclasses.comonad.Comonad;
import cyclops.typeclasses.foldable.Foldable;
import cyclops.typeclasses.foldable.Unfoldable;
import cyclops.typeclasses.functor.Functor;
import cyclops.typeclasses.instances.General;
import cyclops.typeclasses.monad.*;
import io.vavr.Lazy;
import io.vavr.collection.*;
import io.vavr.concurrent.Future;
import io.vavr.control.Either;
import io.vavr.control.Option;
import io.vavr.control.Try;
import lombok.experimental.UtilityClass;
import org.jooq.lambda.tuple.Tuple2;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.*;

import static com.aol.cyclops.vavr.hkt.ListKind.widen;


public class Lists {
    public static > ListT liftM(List opt, W witness) {
        return ListT.ofList(witness.adapter().unit(VavrListX.ofAll(opt)));
    }
    public static  ,T> XorM xorM(List type){
        return XorM.right(anyM(type));
    }
    public static  ,T> XorM xorM(T... values){
        return xorM(List.of(values));
    }

    public static  AnyMSeq anyM(List option) {
        return AnyM.ofSeq(option, list.INSTANCE);
    }
    /**
     * Perform a For Comprehension over a List, accepting 3 generating functions.
     * This results in a four level nested internal iteration over the provided Publishers.
     *
     *  
     * {@code
     *
     *   import static cyclops.Lists.forEach4;
     *
    forEach4(IntList.range(1,10).boxed(),
    a-> List.iterate(a,i->i+1).limit(10),
    (a,b) -> List.of(a+b),
    (a,b,c) -> List.just(a+b+c),
    Tuple::tuple)
     *
     * }
     * 
* * @param value1 top level List * @param value2 Nested List * @param value3 Nested List * @param value4 Nested List * @param yieldingFunction Generates a result per combination * @return List with an element per combination of nested publishers generated by the yielding function */ public static List forEach4(List value1, Function> value2, BiFunction> value3, Fn3> value4, Fn4 yieldingFunction) { return value1.flatMap(in -> { List a = value2.apply(in); return a.flatMap(ina -> { List b = value3.apply(in,ina); return b.flatMap(inb -> { List c = value4.apply(in,ina,inb); return c.map(in2 -> yieldingFunction.apply(in, ina, inb, in2)); }); }); }); } /** * Perform a For Comprehension over a List, accepting 3 generating function. * This results in a four level nested internal iteration over the provided Publishers. *
     * {@code
     *
     *  import static com.aol.cyclops2.reactor.Listes.forEach4;
     *
     *  forEach4(IntList.range(1,10).boxed(),
    a-> List.iterate(a,i->i+1).limit(10),
    (a,b) -> List.just(a+b),
    (a,b,c) -> List.just(a+b+c),
    (a,b,c,d) -> a+b+c+d <100,
    Tuple::tuple);
     *
     * }
     * 
* * @param value1 top level List * @param value2 Nested List * @param value3 Nested List * @param value4 Nested List * @param filterFunction A filtering function, keeps values where the predicate holds * @param yieldingFunction Generates a result per combination * @return List with an element per combination of nested publishers generated by the yielding function */ public static List forEach4(List value1, Function> value2, BiFunction> value3, Fn3> value4, Fn4 filterFunction, Fn4 yieldingFunction) { return value1.flatMap(in -> { List a = value2.apply(in); return a.flatMap(ina -> { List b = value3.apply(in,ina); return b.flatMap(inb -> { List c = value4.apply(in,ina,inb); return c.filter(in2->filterFunction.apply(in,ina,inb,in2)) .map(in2 -> yieldingFunction.apply(in, ina, inb, in2)); }); }); }); } /** * Perform a For Comprehension over a List, accepting 2 generating function. * This results in a three level nested internal iteration over the provided Publishers. * *
     * {@code
     *
     * import static Lists.forEach3;
     *
     * forEach(IntList.range(1,10).boxed(),
    a-> List.iterate(a,i->i+1).limit(10),
    (a,b) -> List.of(a+b),
    Tuple::tuple);
     *
     * }
     * 
* * * @param value1 top level List * @param value2 Nested List * @param value3 Nested List * @param yieldingFunction Generates a result per combination * @return List with an element per combination of nested publishers generated by the yielding function */ public static List forEach3(List value1, Function> value2, BiFunction> value3, Fn3 yieldingFunction) { return value1.flatMap(in -> { List a = value2.apply(in); return a.flatMap(ina -> { List b = value3.apply(in,ina); return b.map(in2 -> yieldingFunction.apply(in, ina, in2)); }); }); } /** * Perform a For Comprehension over a List, accepting 2 generating function. * This results in a three level nested internal iteration over the provided Publishers. *
     * {@code
     *
     * import static Lists.forEach;
     *
     * forEach(IntList.range(1,10).boxed(),
    a-> List.iterate(a,i->i+1).limit(10),
    (a,b) -> List.of(a+b),
    (a,b,c) ->a+b+c<10,
    Tuple::tuple)
    .toListX();
     * }
     * 
* * @param value1 top level List * @param value2 Nested publisher * @param value3 Nested publisher * @param filterFunction A filtering function, keeps values where the predicate holds * @param yieldingFunction Generates a result per combination * @return */ public static List forEach3(List value1, Function> value2, BiFunction> value3, Fn3 filterFunction, Fn3 yieldingFunction) { return value1.flatMap(in -> { List a = value2.apply(in); return a.flatMap(ina -> { List b = value3.apply(in,ina); return b.filter(in2->filterFunction.apply(in,ina,in2)) .map(in2 -> yieldingFunction.apply(in, ina, in2)); }); }); } /** * Perform a For Comprehension over a List, accepting an additonal generating function. * This results in a two level nested internal iteration over the provided Publishers. * *
     * {@code
     *
     *  import static Lists.forEach2;
     *  forEach(IntList.range(1, 10).boxed(),
     *          i -> List.range(i, 10), Tuple::tuple)
    .forEach(System.out::println);

    //(1, 1)
    (1, 2)
    (1, 3)
    (1, 4)
    ...
     *
     * }
* * @param value1 top level List * @param value2 Nested publisher * @param yieldingFunction Generates a result per combination * @return */ public static List forEach2(List value1, Function> value2, BiFunction yieldingFunction) { return value1.flatMap(in -> { List a = value2.apply(in); return a.map(in2 -> yieldingFunction.apply(in, in2)); }); } /** * *
     * {@code
     *
     *   import static Lists.forEach2;
     *
     *   forEach(IntList.range(1, 10).boxed(),
     *           i -> List.range(i, 10),
     *           (a,b) -> a>2 && b<10,
     *           Tuple::tuple)
    .forEach(System.out::println);

    //(3, 3)
    (3, 4)
    (3, 5)
    (3, 6)
    (3, 7)
    (3, 8)
    (3, 9)
    ...

     *
     * }
* * * @param value1 top level List * @param value2 Nested publisher * @param filterFunction A filtering function, keeps values where the predicate holds * @param yieldingFunction Generates a result per combination * @return */ public static List forEach2(List value1, Function> value2, BiFunction filterFunction, BiFunction yieldingFunction) { return value1.flatMap(in -> { List a = value2.apply(in); return a.filter(in2->filterFunction.apply(in,in2)) .map(in2 -> yieldingFunction.apply(in, in2)); }); } public static Active allTypeclasses(List array){ return Active.of(widen(array), Lists.Instances.definitions()); } public static Nested mapM(List array, Function> fn, InstanceDefinitions defs){ List> e = array.map(fn); ListKind> lk = widen(e); return Nested.of(lk, Lists.Instances.definitions(), defs); } /** * Companion class for creating Type Class instances for working with Lists * */ @UtilityClass public static class Instances { public static InstanceDefinitions definitions() { return new InstanceDefinitions() { @Override public Functor functor() { return Instances.functor(); } @Override public Pure unit() { return Instances.unit(); } @Override public Applicative applicative() { return Instances.zippingApplicative(); } @Override public Monad monad() { return Instances.monad(); } @Override public Maybe> monadZero() { return Maybe.just(Instances.monadZero()); } @Override public Maybe> monadPlus() { return Maybe.just(Instances.monadPlus()); } @Override public Maybe> monadPlus(Monoid> m) { return Maybe.just(Instances.monadPlus(m)); } @Override public Maybe> traverse() { return Maybe.just(Instances.traverse()); } @Override public Maybe> foldable() { return Maybe.just(Instances.foldable()); } @Override public Maybe> comonad() { return Maybe.none(); } @Override public Maybe> unfoldable() { return Maybe.just(Instances.unfoldable()); } }; } /** * * Transform a list, mulitplying every element by 2 * *
         * {@code
         *  ListKind list = Lists.functor().map(i->i*2, ListKind.widen(List.of(1,2,3));
         *
         *  //[2,4,6]
         *
         *
         * }
         * 
* * An example fluent api working with Lists *
         * {@code
         *   ListKind list = Lists.unit()
        .unit("hello")
        .then(h->Lists.functor().map((String v) ->v.length(), h))
        .convert(ListKind::narrowK);
         *
         * }
         * 
* * * @return A functor for Lists */ public static Functor functor(){ BiFunction,Function,ListKind> map = Instances::map; return General.functor(map); } /** *
         * {@code
         * ListKind list = Lists.unit()
        .unit("hello")
        .convert(ListKind::narrowK);

        //List.of("hello"))
         *
         * }
         * 
* * * @return A factory for Lists */ public static Pure unit(){ return General.unit(Instances::of); } /** * *
         * {@code
         * import static com.aol.cyclops.hkt.jdk.ListKind.widen;
         * import static com.aol.cyclops.util.function.Lambda.l1;
         *
        Lists.zippingApplicative()
        .ap(widen(List.of(l1(this::multiplyByTwo))),widen(List.of(1,2,3)));
         *
         * //[2,4,6]
         * }
         * 
* * * Example fluent API *
         * {@code
         * ListKind> listFn =Lists.unit()
         *                                                  .unit(Lambda.l1((Integer i) ->i*2))
         *                                                  .convert(ListKind::narrowK);

        ListKind list = Lists.unit()
        .unit("hello")
        .then(h->Lists.functor().map((String v) ->v.length(), h))
        .then(h->Lists.zippingApplicative().ap(listFn, h))
        .convert(ListKind::narrowK);

        //List.of("hello".length()*2))
         *
         * }
         * 
* * * @return A zipper for Lists */ public static Applicative zippingApplicative(){ BiFunction>,ListKind,ListKind> ap = Instances::ap; return General.applicative(functor(), unit(), ap); } /** * *
         * {@code
         * import static com.aol.cyclops.hkt.jdk.ListKind.widen;
         * ListKind list  = Lists.monad()
        .flatMap(i->widen(ListX.range(0,i)), widen(List.of(1,2,3)))
        .convert(ListKind::narrowK);
         * }
         * 
* * Example fluent API *
         * {@code
         *    ListKind list = Lists.unit()
        .unit("hello")
        .then(h->Lists.monad().flatMap((String v) ->Lists.unit().unit(v.length()), h))
        .convert(ListKind::narrowK);

        //List.of("hello".length())
         *
         * }
         * 
* * @return Type class with monad functions for Lists */ public static Monad monad(){ BiFunction,Function>,Higher> flatMap = Instances::flatMap; return General.monad(zippingApplicative(), flatMap); } /** * *
         * {@code
         *  ListKind list = Lists.unit()
        .unit("hello")
        .then(h->Lists.monadZero().filter((String t)->t.startsWith("he"), h))
        .convert(ListKind::narrowK);

        //List.of("hello"));
         *
         * }
         * 
* * * @return A filterable monad (with default value) */ public static MonadZero monadZero(){ BiFunction,Predicate,Higher> filter = Instances::filter; Supplier> zero = ()-> widen(List.empty()); return General.monadZero(monad(), zero,filter); } /** *
         * {@code
         *  ListKind list = Lists.monadPlus()
        .plus(ListKind.widen(List.of()), ListKind.widen(List.of(10)))
        .convert(ListKind::narrowK);
        //List.of(10))
         *
         * }
         * 
* @return Type class for combining Lists by concatenation */ public static MonadPlus monadPlus(){ Monoid> m = Monoid.of(widen(List.empty()), Instances::concat); Monoid> m2= (Monoid)m; return General.monadPlus(monadZero(),m2); } /** * *
         * {@code
         *  Monoid> m = Monoid.of(ListKind.widen(List.of()), (a,b)->a.isEmpty() ? b : a);
        ListKind list = Lists.monadPlus(m)
        .plus(ListKind.widen(List.of(5)), ListKind.widen(List.of(10)))
        .convert(ListKind::narrowK);
        //List.of(5))
         *
         * }
         * 
* * @param m Monoid to use for combining Lists * @return Type class for combining Lists */ public static MonadPlus monadPlus(Monoid> m){ Monoid> m2= (Monoid)m; return General.monadPlus(monadZero(),m2); } public static MonadPlus monadPlusK(Monoid> m){ Monoid> m2= (Monoid)m; return General.monadPlus(monadZero(),m2); } /** * @return Type class for traversables with traverse / sequence operations */ public static Traverse traverse(){ BiFunction,ListKind>,Higher>> sequenceFn = (ap, list) -> { Higher> identity = ap.unit(widen(List.empty())); BiFunction>,Higher,Higher>> combineToList = (acc, next) -> ap.apBiFn(ap.unit((a, b) -> concat(a, ListKind.just(b))),acc,next); BinaryOperator>> combineLists = (a, b)-> ap.apBiFn(ap.unit((l1, l2)-> { return concat(l1,l2);}),a,b); ; return ReactiveSeq.fromIterable(list).reduce(identity, combineToList, combineLists); }; BiFunction,Higher>,Higher>> sequenceNarrow = (a,b) -> ListKind.widen2(sequenceFn.apply(a, ListKind.narrowK(b))); return General.traverse(zippingApplicative(), sequenceNarrow); } /** * *
         * {@code
         * int sum  = Lists.foldable()
        .foldLeft(0, (a,b)->a+b, ListKind.widen(List.of(1,2,3,4)));

        //10
         *
         * }
         * 
* * * @return Type class for folding / reduction operations */ public static Foldable foldable(){ BiFunction,Higher,T> foldRightFn = (m, l)-> ReactiveSeq.fromIterable(ListKind.narrow(l)).foldRight(m); BiFunction,Higher,T> foldLeftFn = (m, l)-> ReactiveSeq.fromIterable(ListKind.narrow(l)).reduce(m); return General.foldable(foldRightFn, foldLeftFn); } private static ListKind concat(ListKind l1, ListKind l2){ return widen(l1.appendAll(l2)); } private ListKind of(T value){ return widen(List.of(value)); } private static ListKind ap(ListKind> lt, ListKind list){ return widen(lt.toReactiveSeq().zip(list,(a, b)->a.apply(b))); } private static Higher flatMap(Higher lt, Function> fn){ return widen(ListKind.narrowK(lt).flatMap(fn.andThen(ListKind::narrowK))); } private static ListKind map(ListKind lt, Function fn){ return widen(lt.map(fn)); } private static ListKind filter(Higher lt, Predicate fn){ return widen(ListKind.narrow(lt).filter(fn)); } public static Unfoldable unfoldable(){ return new Unfoldable() { @Override public Higher unfold(T b, Function>> fn) { return widen(ReactiveSeq.unfold(b,fn).collect(List.collector())); } }; } } public static Coproduct coproduct(List list, InstanceDefinitions def1){ return Coproduct.of(Xor.primary(ListKind.widen(list)),def1, Instances.definitions()); } public static Coproduct coproduct(InstanceDefinitions def1,T... values){ return Coproduct.of(Xor.primary(ListKind.just(values)),def1, Instances.definitions()); } public static interface ListNested{ public static Nested option(List> type){ return Nested.of(widen(type.map(OptionKind::widen)),Instances.definitions(),Options.Instances.definitions()); } public static Nested listTry(List> type){ return Nested.of(widen(type.map(TryKind::widen)),Instances.definitions(),Trys.Instances.definitions()); } public static Nested future(List> type){ return Nested.of(widen(type.map(FutureKind::widen)),Instances.definitions(),Futures.Instances.definitions()); } public static Nested lazy(List> nested){ return Nested.of(widen(nested.map(LazyKind::widen)),Instances.definitions(),Lazys.Instances.definitions()); } public static Nested, R> either(List> nested){ return Nested.of(widen(nested.map(EitherKind::widen)),Instances.definitions(),Eithers.Instances.definitions()); } public static Nested stream(List> nested){ return Nested.of(widen(nested.map(StreamKind::widen)),Instances.definitions(),Streams.Instances.definitions()); } public static Nested queue(List> nested){ return Nested.of(widen(nested.map(QueueKind::widen)),Instances.definitions(),Queues.Instances.definitions()); } public static Nested list(List> nested){ return Nested.of(widen(nested.map(ListKind::widen)),Instances.definitions(),Lists.Instances.definitions()); } public static Nested array(List> nested){ return Nested.of(widen(nested.map(ArrayKind::widen)),Instances.definitions(),Arrays.Instances.definitions()); } public static Nested vector(List> nested){ return Nested.of(widen(nested.map(VectorKind::widen)),Instances.definitions(),Vectors.Instances.definitions()); } public static Nested set(List> nested){ return Nested.of(widen(nested.map(HashSetKind::widen)),Instances.definitions(), HashSets.Instances.definitions()); } public static Nested reactiveSeq(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(),ReactiveSeq.Instances.definitions()); } public static Nested maybe(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(),Maybe.Instances.definitions()); } public static Nested eval(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(),Eval.Instances.definitions()); } public static Nested cyclopsFuture(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(),cyclops.async.Future.Instances.definitions()); } public static Nested, P> xor(List> nested){ ListKind> x = widen(nested); ListKind, P>> y = (ListKind)x; return Nested.of(y,Instances.definitions(),Xor.Instances.definitions()); } public static Nested, T> reader(List> nested){ ListKind> x = widen(nested); ListKind, T>> y = (ListKind)x; return Nested.of(y,Instances.definitions(),Reader.Instances.definitions()); } public static Nested, P> cyclopsTry(List> nested){ ListKind> x = widen(nested); ListKind, P>> y = (ListKind)x; return Nested.of(y,Instances.definitions(),cyclops.control.Try.Instances.definitions()); } public static Nested optional(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(), Optionals.Instances.definitions()); } public static Nested completableFuture(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(), CompletableFutures.Instances.definitions()); } public static Nested javaStream(List> nested){ ListKind> x = widen(nested); ListKind> y = (ListKind)x; return Nested.of(y,Instances.definitions(), cyclops.companion.Streams.Instances.definitions()); } } public static interface NestedList{ public static Nested reactiveSeq(ReactiveSeq> nested){ ReactiveSeq> x = nested.map(ListKind::widenK); return Nested.of(x,ReactiveSeq.Instances.definitions(),Instances.definitions()); } public static Nested maybe(Maybe> nested){ Maybe> x = nested.map(ListKind::widenK); return Nested.of(x,Maybe.Instances.definitions(),Instances.definitions()); } public static Nested eval(Eval> nested){ Eval> x = nested.map(ListKind::widenK); return Nested.of(x,Eval.Instances.definitions(),Instances.definitions()); } public static Nested cyclopsFuture(cyclops.async.Future> nested){ cyclops.async.Future> x = nested.map(ListKind::widenK); return Nested.of(x,cyclops.async.Future.Instances.definitions(),Instances.definitions()); } public static Nested,list, P> xor(Xor> nested){ Xor> x = nested.map(ListKind::widenK); return Nested.of(x,Xor.Instances.definitions(),Instances.definitions()); } public static Nested,list, T> reader(Reader> nested){ Reader> x = nested.map(ListKind::widenK); return Nested.of(x,Reader.Instances.definitions(),Instances.definitions()); } public static Nested,list, P> cyclopsTry(cyclops.control.Try, S> nested){ cyclops.control.Try, S> x = nested.map(ListKind::widenK); return Nested.of(x,cyclops.control.Try.Instances.definitions(),Instances.definitions()); } public static Nested optional(Optional> nested){ Optional> x = nested.map(ListKind::widenK); return Nested.of(Optionals.OptionalKind.widen(x), Optionals.Instances.definitions(), Instances.definitions()); } public static Nested completableFuture(CompletableFuture> nested){ CompletableFuture> x = nested.thenApply(ListKind::widenK); return Nested.of(CompletableFutures.CompletableFutureKind.widen(x), CompletableFutures.Instances.definitions(),Instances.definitions()); } public static Nested javaStream(java.util.stream.Stream> nested){ java.util.stream.Stream> x = nested.map(ListKind::widenK); return Nested.of(cyclops.companion.Streams.StreamKind.widen(x), cyclops.companion.Streams.Instances.definitions(),Instances.definitions()); } } }