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

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

package cyclops.companion.vavr;

import cyclops.control.*;
import cyclops.monads.VavrWitness.queue;
import io.vavr.Lazy;
import io.vavr.collection.*;
import io.vavr.collection.Stream;
import io.vavr.concurrent.Future;
import io.vavr.control.*;
import com.aol.cyclops.vavr.hkt.*;
import cyclops.companion.CompletableFutures;
import cyclops.companion.Optionals;
import cyclops.monads.*;
import cyclops.monads.VavrWitness.*;
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.Witness.*;
import cyclops.stream.ReactiveSeq;
import cyclops.typeclasses.*;
import com.aol.cyclops.vavr.hkt.HashSetKind;

import cyclops.control.Maybe;
import cyclops.control.Xor;
import cyclops.monads.AnyM;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.hashSet;
import cyclops.monads.WitnessType;

import cyclops.monads.XorM;
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.collection.HashSet;
import io.vavr.collection.Set;
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.HashSetKind.widen;


public class HashSets {

    public static   Coproduct coproduct(HashSet type, InstanceDefinitions def1){
        return Coproduct.of(Xor.primary(widen(type)),def1, Instances.definitions());
    }
    public static   Coproduct coproduct(InstanceDefinitions def1,T... values){
        return Coproduct.of(Xor.primary(HashSetKind.just(values)),def1, Instances.definitions());
    }
    public static  ,T> XorM xorM(HashSet type){
        return XorM.right(anyM(type));
    }
    public static  ,T> XorM xorM(T... values){
        return xorM(HashSet.of(values));
    }
    public static  AnyMSeq anyM(HashSet option) {
        return AnyM.ofSeq(option, hashSet.INSTANCE);
    }
    /**
     * Perform a For Comprehension over a Set, accepting 3 generating functions.
     * This results in a four level nested internal iteration over the provided Publishers.
     *
     *  
     * {@code
     *
     *   import static cyclops.HashSets.forEach4;
     *
    forEach4(IntSet.range(1,10).boxed(),
    a-> Set.iterate(a,i->i+1).limit(10),
    (a,b) -> Set.of(a+b),
    (a,b,c) -> Set.just(a+b+c),
    Tuple::tuple)
     *
     * }
     * 
* * @param value1 top level Set * @param value2 Nested Set * @param value3 Nested Set * @param value4 Nested Set * @param yieldingFunction Generates a result per combination * @return Set with an element per combination of nested publishers generated by the yielding function */ public static Set forEach4(Set value1, Function> value2, BiFunction> value3, Fn3> value4, Fn4 yieldingFunction) { return value1.flatMap(in -> { Set a = value2.apply(in); return a.flatMap(ina -> { Set b = value3.apply(in,ina); return b.flatMap(inb -> { Set c = value4.apply(in,ina,inb); return c.map(in2 -> yieldingFunction.apply(in, ina, inb, in2)); }); }); }); } /** * Perform a For Comprehension over a Set, accepting 3 generating function. * This results in a four level nested internal iteration over the provided Publishers. *
     * {@code
     *
     *  import static com.aol.cyclops2.reactor.Setes.forEach4;
     *
     *  forEach4(IntSet.range(1,10).boxed(),
    a-> Set.iterate(a,i->i+1).limit(10),
    (a,b) -> Set.just(a+b),
    (a,b,c) -> Set.just(a+b+c),
    (a,b,c,d) -> a+b+c+d <100,
    Tuple::tuple);
     *
     * }
     * 
* * @param value1 top level Set * @param value2 Nested Set * @param value3 Nested Set * @param value4 Nested Set * @param filterFunction A filtering function, keeps values where the predicate holds * @param yieldingFunction Generates a result per combination * @return Set with an element per combination of nested publishers generated by the yielding function */ public static Set forEach4(Set value1, Function> value2, BiFunction> value3, Fn3> value4, Fn4 filterFunction, Fn4 yieldingFunction) { return value1.flatMap(in -> { Set a = value2.apply(in); return a.flatMap(ina -> { Set b = value3.apply(in,ina); return b.flatMap(inb -> { Set 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 Set, accepting 2 generating function. * This results in a three level nested internal iteration over the provided Publishers. * *
     * {@code
     *
     * import static HashSets.forEach3;
     *
     * forEach(IntSet.range(1,10).boxed(),
    a-> Set.iterate(a,i->i+1).limit(10),
    (a,b) -> Set.of(a+b),
    Tuple::tuple);
     *
     * }
     * 
* * * @param value1 top level Set * @param value2 Nested Set * @param value3 Nested Set * @param yieldingFunction Generates a result per combination * @return Set with an element per combination of nested publishers generated by the yielding function */ public static Set forEach3(Set value1, Function> value2, BiFunction> value3, Fn3 yieldingFunction) { return value1.flatMap(in -> { Set a = value2.apply(in); return a.flatMap(ina -> { Set b = value3.apply(in,ina); return b.map(in2 -> yieldingFunction.apply(in, ina, in2)); }); }); } /** * Perform a For Comprehension over a Set, accepting 2 generating function. * This results in a three level nested internal iteration over the provided Publishers. *
     * {@code
     *
     * import static HashSets.forEach;
     *
     * forEach(IntSet.range(1,10).boxed(),
    a-> Set.iterate(a,i->i+1).limit(10),
    (a,b) -> Set.of(a+b),
    (a,b,c) ->a+b+c<10,
    Tuple::tuple)
    .toSetX();
     * }
     * 
* * @param value1 top level Set * @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 Set forEach3(Set value1, Function> value2, BiFunction> value3, Fn3 filterFunction, Fn3 yieldingFunction) { return value1.flatMap(in -> { Set a = value2.apply(in); return a.flatMap(ina -> { Set 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 Set, accepting an additonal generating function. * This results in a two level nested internal iteration over the provided Publishers. * *
     * {@code
     *
     *  import static HashSets.forEach2;
     *  forEach(IntSet.range(1, 10).boxed(),
     *          i -> Set.range(i, 10), Tuple::tuple)
    .forEach(System.out::println);

    //(1, 1)
    (1, 2)
    (1, 3)
    (1, 4)
    ...
     *
     * }
* * @param value1 top level Set * @param value2 Nested publisher * @param yieldingFunction Generates a result per combination * @return */ public static Set forEach2(Set value1, Function> value2, BiFunction yieldingFunction) { return value1.flatMap(in -> { Set a = value2.apply(in); return a.map(in2 -> yieldingFunction.apply(in, in2)); }); } /** * *
     * {@code
     *
     *   import static HashSets.forEach2;
     *
     *   forEach(IntSet.range(1, 10).boxed(),
     *           i -> Set.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 Set * @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 Set forEach2(Set value1, Function> value2, BiFunction filterFunction, BiFunction yieldingFunction) { return value1.flatMap(in -> { Set a = value2.apply(in); return a.filter(in2->filterFunction.apply(in,in2)) .map(in2 -> yieldingFunction.apply(in, in2)); }); } public static Active allTypeclasses(HashSet array){ return Active.of(widen(array), HashSets.Instances.definitions()); } public static Nested mapM(HashSet array, Function> fn, InstanceDefinitions defs){ HashSet> e = array.map(fn); HashSetKind> lk = widen(e); return Nested.of(lk, HashSets.Instances.definitions(), defs); } /** * Companion class for creating Type Class instances for working with HashSets * */ @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 hashSet, mulitplying every element by 2 * *
         * {@code
         *  HashSetKind hashSet = HashSets.functor().map(i->i*2, HashSetKind.widen(Set.of(1,2,3));
         *
         *  //[2,4,6]
         *
         *
         * }
         * 
* * An example fluent api working with HashSets *
         * {@code
         *   HashSetKind hashSet = HashSets.unit()
        .unit("hello")
        .then(h->HashSets.functor().map((String v) ->v.length(), h))
        .convert(HashSetKind::narrowK);
         *
         * }
         * 
* * * @return A functor for HashSets */ public static Functor functor(){ BiFunction,Function,HashSetKind> map = Instances::map; return General.functor(map); } /** *
         * {@code
         * HashSetKind hashSet = HashSets.unit()
        .unit("hello")
        .convert(HashSetKind::narrowK);

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

        HashSetKind hashSet = HashSets.unit()
        .unit("hello")
        .then(h->HashSets.functor().map((String v) ->v.length(), h))
        .then(h->HashSets.zippingApplicative().ap(setFn, h))
        .convert(HashSetKind::narrowK);

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

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

        //Set.of("hello"));
         *
         * }
         * 
* * * @return A filterable monad (with default value) */ public static MonadZero monadZero(){ BiFunction,Predicate,Higher> filter = Instances::filter; Supplier> zero = ()-> widen(HashSet.empty()); return General.monadZero(monad(), zero,filter); } /** *
         * {@code
         *  HashSetKind hashSet = HashSets.monadPlus()
        .plus(HashSetKind.widen(Set.of()), HashSetKind.widen(Set.of(10)))
        .convert(HashSetKind::narrowK);
        //Set.of(10))
         *
         * }
         * 
* @return Type class for combining HashSets by concatenation */ public static MonadPlus monadPlus(){ Monoid> m = Monoid.of(widen(HashSet.empty()), Instances::concat); Monoid> m2= (Monoid)m; return General.monadPlus(monadZero(),m2); } /** * *
         * {@code
         *  Monoid> m = Monoid.of(HashSetKind.widen(Set.of()), (a,b)->a.isEmpty() ? b : a);
        HashSetKind hashSet = HashSets.monadPlus(m)
        .plus(HashSetKind.widen(Set.of(5)), HashSetKind.widen(Set.of(10)))
        .convert(HashSetKind::narrowK);
        //Set.of(5))
         *
         * }
         * 
* * @param m Monoid to use for combining HashSets * @return Type class for combining HashSets */ 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,HashSetKind>,Higher>> sequenceFn = (ap, set) -> { Higher> identity = ap.unit(widen(HashSet.empty())); BiFunction>,Higher,Higher>> combineToSet = (acc, next) -> ap.apBiFn(ap.unit((a, b) -> concat(a, HashSetKind.just(b))),acc,next); BinaryOperator>> combineSets = (a, b)-> ap.apBiFn(ap.unit((l1, l2)-> { return concat(l1,l2);}),a,b); ; return ReactiveSeq.fromIterable(set).reduce(identity, combineToSet, combineSets); }; BiFunction,Higher>,Higher>> sequenceNarrow = (a,b) -> HashSetKind.widen2(sequenceFn.apply(a, HashSetKind.narrowK(b))); return General.traverse(zippingApplicative(), sequenceNarrow); } /** * *
         * {@code
         * int sum  = HashSets.foldable()
        .foldLeft(0, (a,b)->a+b, HashSetKind.widen(Set.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(HashSetKind.narrow(l)).foldRight(m); BiFunction,Higher,T> foldLeftFn = (m, l)-> ReactiveSeq.fromIterable(HashSetKind.narrow(l)).reduce(m); return General.foldable(foldRightFn, foldLeftFn); } private static HashSetKind concat(HashSetKind l1, HashSetKind l2){ return widen(l1.addAll(l2)); } private HashSetKind of(T value){ return widen(HashSet.of(value)); } private static HashSetKind ap(HashSetKind> lt, HashSetKind set){ return widen(lt.toReactiveSeq().zip(set,(a, b)->a.apply(b))); } private static Higher flatMap(Higher lt, Function> fn){ return widen(HashSetKind.narrowK(lt).flatMap(fn.andThen(HashSetKind::narrowK))); } private static HashSetKind map(HashSetKind lt, Function fn){ return widen(lt.map(fn)); } private static HashSetKind filter(Higher lt, Predicate fn){ return widen(HashSetKind.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(HashSet.collector())); } }; } } public static interface SetNested{ public static Nested lazy(HashSet> type){ return Nested.of(widen(type.map(LazyKind::widen)),Instances.definitions(),Lazys.Instances.definitions()); } public static Nested setTry(HashSet> type){ return Nested.of(widen(type.map(TryKind::widen)),Instances.definitions(),Trys.Instances.definitions()); } public static Nested future(HashSet> type){ return Nested.of(widen(type.map(FutureKind::widen)),Instances.definitions(),Futures.Instances.definitions()); } public static Nested queue(HashSet> nested){ return Nested.of(widen(nested.map(QueueKind::widen)),Instances.definitions(),Queues.Instances.definitions()); } public static Nested, R> either(HashSet> nested){ return Nested.of(widen(nested.map(EitherKind::widen)),Instances.definitions(),Eithers.Instances.definitions()); } public static Nested stream(HashSet> nested){ return Nested.of(widen(nested.map(StreamKind::widen)),Instances.definitions(),Streams.Instances.definitions()); } public static Nested list(HashSet> nested){ return Nested.of(widen(nested.map(ListKind::widen)), Instances.definitions(),Lists.Instances.definitions()); } public static Nested array(HashSet> nested){ return Nested.of(widen(nested.map(ArrayKind::widen)),Instances.definitions(),Arrays.Instances.definitions()); } public static Nested vector(HashSet> nested){ return Nested.of(widen(nested.map(VectorKind::widen)),Instances.definitions(),Vectors.Instances.definitions()); } public static Nested set(HashSet> nested){ return Nested.of(widen(nested.map(HashSetKind::widen)),Instances.definitions(), HashSets.Instances.definitions()); } public static Nested reactiveSeq(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),ReactiveSeq.Instances.definitions()); } public static Nested maybe(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),Maybe.Instances.definitions()); } public static Nested eval(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),Eval.Instances.definitions()); } public static Nested cyclopsFuture(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),cyclops.async.Future.Instances.definitions()); } public static Nested, P> xor(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind, P>> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),Xor.Instances.definitions()); } public static Nested, T> reader(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind, T>> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),Reader.Instances.definitions()); } public static Nested, P> cyclopsTry(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind, P>> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(),cyclops.control.Try.Instances.definitions()); } public static Nested setal(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(), Optionals.Instances.definitions()); } public static Nested completableSet(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(), CompletableFutures.Instances.definitions()); } public static Nested javaStream(HashSet> nested){ HashSetKind> x = widen(nested); HashSetKind> y = (HashSetKind)x; return Nested.of(y,Instances.definitions(), cyclops.companion.Streams.Instances.definitions()); } } public static interface NestedSet{ public static Nested reactiveSeq(ReactiveSeq> nested){ ReactiveSeq> x = nested.map(HashSetKind::widenK); return Nested.of(x,ReactiveSeq.Instances.definitions(),Instances.definitions()); } public static Nested maybe(Maybe> nested){ Maybe> x = nested.map(HashSetKind::widenK); return Nested.of(x,Maybe.Instances.definitions(),Instances.definitions()); } public static Nested eval(Eval> nested){ Eval> x = nested.map(HashSetKind::widenK); return Nested.of(x,Eval.Instances.definitions(),Instances.definitions()); } public static Nested cyclopsFuture(cyclops.async.Future> nested){ cyclops.async.Future> x = nested.map(HashSetKind::widenK); return Nested.of(x,cyclops.async.Future.Instances.definitions(),Instances.definitions()); } public static Nested,hashSet, P> xor(Xor> nested){ Xor> x = nested.map(HashSetKind::widenK); return Nested.of(x,Xor.Instances.definitions(),Instances.definitions()); } public static Nested,hashSet, T> reader(Reader> nested){ Reader> x = nested.map(HashSetKind::widenK); return Nested.of(x,Reader.Instances.definitions(),Instances.definitions()); } public static Nested,hashSet, P> cyclopsTry(cyclops.control.Try, S> nested){ cyclops.control.Try, S> x = nested.map(HashSetKind::widenK); return Nested.of(x,cyclops.control.Try.Instances.definitions(),Instances.definitions()); } public static Nested setal(Optional> nested){ Optional> x = nested.map(HashSetKind::widenK); return Nested.of(Optionals.OptionalKind.widen(x), Optionals.Instances.definitions(), Instances.definitions()); } public static Nested completableSet(CompletableFuture> nested){ CompletableFuture> x = nested.thenApply(HashSetKind::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(HashSetKind::widenK); return Nested.of(cyclops.companion.Streams.StreamKind.widen(x), cyclops.companion.Streams.Instances.definitions(),Instances.definitions()); } } }