cyclops.companion.vavr.Vectors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cyclops-vavr Show documentation
Show all versions of cyclops-vavr Show documentation
Converters and Comprehenders for Javaslang
The newest version!
package cyclops.companion.vavr;
import com.aol.cyclops.vavr.hkt.ListKind;
import cyclops.control.Maybe;
import cyclops.conversion.vavr.FromCyclopsReact;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.vector;
import cyclops.collections.vavr.VavrVectorX;
import com.aol.cyclops.vavr.hkt.VectorKind;
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.AnyM;
import cyclops.monads.WitnessType;
import cyclops.monads.transformers.ListT;
import cyclops.stream.ReactiveSeq;
import cyclops.typeclasses.Active;
import cyclops.typeclasses.InstanceDefinitions;
import cyclops.typeclasses.Nested;
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.instances.General;
import cyclops.typeclasses.monad.*;
import io.vavr.collection.List;
import io.vavr.collection.Vector;
import lombok.experimental.UtilityClass;
import org.jooq.lambda.tuple.Tuple2;
import java.util.Optional;
import java.util.function.*;
public class Vectors {
public static > ListT liftM(Vector opt, W witness) {
return ListT.ofList(witness.adapter().unit(VavrVectorX.ofAll(opt)));
}
public static AnyMSeq anyM(Vector option) {
return AnyM.ofSeq(option, vector.INSTANCE);
}
/**
* Perform a For Comprehension over a Vector, accepting 3 generating functions.
* This results in a four level nested internal iteration over the provided Publishers.
*
*
* {@code
*
* import static cyclops.Vectors.forEach4;
*
forEach4(IntVector.range(1,10).boxed(),
a-> Vector.iterate(a,i->i+1).limit(10),
(a,b) -> Vector.of(a+b),
(a,b,c) -> Vector.just(a+b+c),
Tuple::tuple)
*
* }
*
*
* @param value1 top level Vector
* @param value2 Nested Vector
* @param value3 Nested Vector
* @param value4 Nested Vector
* @param yieldingFunction Generates a result per combination
* @return Vector with an element per combination of nested publishers generated by the yielding function
*/
public static Vector forEach4(Vector extends T1> value1,
Function super T1, ? extends Vector> value2,
BiFunction super T1, ? super R1, ? extends Vector> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends Vector> value4,
Fn4 super T1, ? super R1, ? super R2, ? super R3, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Vector a = value2.apply(in);
return a.flatMap(ina -> {
Vector b = value3.apply(in,ina);
return b.flatMap(inb -> {
Vector c = value4.apply(in,ina,inb);
return c.map(in2 -> yieldingFunction.apply(in, ina, inb, in2));
});
});
});
}
/**
* Perform a For Comprehension over a Vector, accepting 3 generating function.
* This results in a four level nested internal iteration over the provided Publishers.
*
* {@code
*
* import static com.aol.cyclops2.reactor.Vectores.forEach4;
*
* forEach4(IntVector.range(1,10).boxed(),
a-> Vector.iterate(a,i->i+1).limit(10),
(a,b) -> Vector.just(a+b),
(a,b,c) -> Vector.just(a+b+c),
(a,b,c,d) -> a+b+c+d <100,
Tuple::tuple);
*
* }
*
*
* @param value1 top level Vector
* @param value2 Nested Vector
* @param value3 Nested Vector
* @param value4 Nested Vector
* @param filterFunction A filtering function, keeps values where the predicate holds
* @param yieldingFunction Generates a result per combination
* @return Vector with an element per combination of nested publishers generated by the yielding function
*/
public static Vector forEach4(Vector extends T1> value1,
Function super T1, ? extends Vector> value2,
BiFunction super T1, ? super R1, ? extends Vector> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends Vector> value4,
Fn4 super T1, ? super R1, ? super R2, ? super R3, Boolean> filterFunction,
Fn4 super T1, ? super R1, ? super R2, ? super R3, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Vector a = value2.apply(in);
return a.flatMap(ina -> {
Vector b = value3.apply(in,ina);
return b.flatMap(inb -> {
Vector 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 Vector, accepting 2 generating function.
* This results in a three level nested internal iteration over the provided Publishers.
*
*
* {@code
*
* import static Vectors.forEach3;
*
* forEach(IntVector.range(1,10).boxed(),
a-> Vector.iterate(a,i->i+1).limit(10),
(a,b) -> Vector.of(a+b),
Tuple::tuple);
*
* }
*
*
*
* @param value1 top level Vector
* @param value2 Nested Vector
* @param value3 Nested Vector
* @param yieldingFunction Generates a result per combination
* @return Vector with an element per combination of nested publishers generated by the yielding function
*/
public static Vector forEach3(Vector extends T1> value1,
Function super T1, ? extends Vector> value2,
BiFunction super T1, ? super R1, ? extends Vector> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Vector a = value2.apply(in);
return a.flatMap(ina -> {
Vector b = value3.apply(in,ina);
return b.map(in2 -> yieldingFunction.apply(in, ina, in2));
});
});
}
/**
* Perform a For Comprehension over a Vector, accepting 2 generating function.
* This results in a three level nested internal iteration over the provided Publishers.
*
* {@code
*
* import static Vectors.forEach;
*
* forEach(IntVector.range(1,10).boxed(),
a-> Vector.iterate(a,i->i+1).limit(10),
(a,b) -> Vector.of(a+b),
(a,b,c) ->a+b+c<10,
Tuple::tuple)
.toVectorX();
* }
*
*
* @param value1 top level Vector
* @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 Vector forEach3(Vector extends T1> value1,
Function super T1, ? extends Vector> value2,
BiFunction super T1, ? super R1, ? extends Vector> value3,
Fn3 super T1, ? super R1, ? super R2, Boolean> filterFunction,
Fn3 super T1, ? super R1, ? super R2, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Vector a = value2.apply(in);
return a.flatMap(ina -> {
Vector 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 Vector, accepting an additonal generating function.
* This results in a two level nested internal iteration over the provided Publishers.
*
*
* {@code
*
* import static Vectors.forEach2;
* forEach(IntVector.range(1, 10).boxed(),
* i -> Vector.range(i, 10), Tuple::tuple)
.forEach(System.out::println);
//(1, 1)
(1, 2)
(1, 3)
(1, 4)
...
*
* }
*
* @param value1 top level Vector
* @param value2 Nested publisher
* @param yieldingFunction Generates a result per combination
* @return
*/
public static Vector forEach2(Vector extends T> value1,
Function super T, Vector> value2,
BiFunction super T, ? super R1, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Vector a = value2.apply(in);
return a.map(in2 -> yieldingFunction.apply(in, in2));
});
}
/**
*
*
* {@code
*
* import static Vectors.forEach2;
*
* forEach(IntVector.range(1, 10).boxed(),
* i -> Vector.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 Vector
* @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 Vector forEach2(Vector extends T> value1,
Function super T, ? extends Vector> value2,
BiFunction super T, ? super R1, Boolean> filterFunction,
BiFunction super T, ? super R1, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Vector a = value2.apply(in);
return a.filter(in2->filterFunction.apply(in,in2))
.map(in2 -> yieldingFunction.apply(in, in2));
});
}
public static Active allTypeclasses(Vector array){
return Active.of(VectorKind.widen(array), Vectors.Instances.definitions());
}
public static Nested mapM(Vector array, Function super T,? extends Higher> fn, InstanceDefinitions defs){
Vector> e = array.map(fn);
VectorKind> lk = VectorKind.widen(e);
return Nested.of(lk, Vectors.Instances.definitions(), defs);
}
/**
* Companion class for creating Type Class instances for working with Vectors
*
*/
@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
* VectorKind list = Vectors.functor().map(i->i*2, VectorKind.widen(Arrays.asVector(1,2,3));
*
* //[2,4,6]
*
*
* }
*
*
* An example fluent api working with Vectors
*
* {@code
* VectorKind list = Vectors.unit()
.unit("hello")
.then(h->Vectors.functor().map((String v) ->v.length(), h))
.convert(VectorKind::narrowK);
*
* }
*
*
*
* @return A functor for Vectors
*/
public static Functor functor(){
BiFunction,Function super T, ? extends R>,VectorKind> map = Instances::map;
return General.functor(map);
}
/**
*
* {@code
* VectorKind list = Vectors.unit()
.unit("hello")
.convert(VectorKind::narrowK);
//Arrays.asVector("hello"))
*
* }
*
*
*
* @return A factory for Vectors
*/
public static Pure unit(){
return General.unit(VectorKind::of);
}
/**
*
*
* {@code
* import static com.aol.cyclops.hkt.jdk.VectorKind.widen;
* import static com.aol.cyclops.util.function.Lambda.l1;
* import static java.util.Arrays.asVector;
*
Vectors.zippingApplicative()
.ap(widen(asVector(l1(this::multiplyByTwo))),widen(asVector(1,2,3)));
*
* //[2,4,6]
* }
*
*
*
* Example fluent API
*
* {@code
* VectorKind> listFn =Vectors.unit()
* .unit(Lambda.l1((Integer i) ->i*2))
* .convert(VectorKind::narrowK);
VectorKind list = Vectors.unit()
.unit("hello")
.then(h->Vectors.functor().map((String v) ->v.length(), h))
.then(h->Vectors.zippingApplicative().ap(listFn, h))
.convert(VectorKind::narrowK);
//Arrays.asVector("hello".length()*2))
*
* }
*
*
*
* @return A zipper for Vectors
*/
public static Applicative zippingApplicative(){
BiFunction>,VectorKind,VectorKind> ap = Instances::ap;
return General.applicative(functor(), unit(), ap);
}
/**
*
*
* {@code
* import static com.aol.cyclops.hkt.jdk.VectorKind.widen;
* VectorKind list = Vectors.monad()
.flatMap(i->widen(VectorX.range(0,i)), widen(Arrays.asVector(1,2,3)))
.convert(VectorKind::narrowK);
* }
*
*
* Example fluent API
*
* {@code
* VectorKind list = Vectors.unit()
.unit("hello")
.then(h->Vectors.monad().flatMap((String v) ->Vectors.unit().unit(v.length()), h))
.convert(VectorKind::narrowK);
//Arrays.asVector("hello".length())
*
* }
*
*
* @return Type class with monad functions for Vectors
*/
public static Monad monad(){
BiFunction,Function super T, ? extends Higher>,Higher> flatMap = Instances::flatMap;
return General.monad(zippingApplicative(), flatMap);
}
/**
*
*
* {@code
* VectorKind list = Vectors.unit()
.unit("hello")
.then(h->Vectors.monadZero().filter((String t)->t.startsWith("he"), h))
.convert(VectorKind::narrowK);
//Arrays.asVector("hello"));
*
* }
*
*
*
* @return A filterable monad (with default value)
*/
public static MonadZero monadZero(){
return General.monadZero(monad(), VectorKind.widen(Vector.empty()));
}
/**
*
* {@code
* VectorKind list = Vectors.monadPlus()
.plus(VectorKind.widen(Arrays.asVector()), VectorKind.widen(Arrays.asVector(10)))
.convert(VectorKind::narrowK);
//Arrays.asVector(10))
*
* }
*
* @return Type class for combining Vectors by concatenation
*/
public static MonadPlus monadPlus(){
Monoid> m = Monoid.of(VectorKind.widen(Vector.empty()), Instances::concat);
Monoid> m2= (Monoid)m;
return General.monadPlus(monadZero(),m2);
}
/**
*
*
* {@code
* Monoid> m = Monoid.of(VectorKind.widen(Arrays.asVector()), (a,b)->a.isEmpty() ? b : a);
VectorKind list = Vectors.monadPlus(m)
.plus(VectorKind.widen(Arrays.asVector(5)), VectorKind.widen(Arrays.asVector(10)))
.convert(VectorKind::narrowK);
//Arrays.asVector(5))
*
* }
*
*
* @param m Monoid to use for combining Vectors
* @return Type class for combining Vectors
*/
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,VectorKind>,Higher>> sequenceFn = (ap, list) -> {
Higher> identity = ap.unit(VectorKind.widen(Vector.empty()));
BiFunction>,Higher,Higher>> combineToVector = (acc, next) -> ap.apBiFn(ap.unit((a, b) -> VectorKind.widen(VectorKind.narrow(a).append(b))),
acc,next);
BinaryOperator>> combineVectors = (a, b)-> ap.apBiFn(ap.unit((l1, l2)-> VectorKind.widen(VectorKind.narrow(l1).appendAll(l2.narrow()))),a,b); ;
return ReactiveSeq.fromIterable(VectorKind.narrow(list))
.reduce(identity,
combineToVector,
combineVectors);
};
BiFunction,Higher>,Higher>> sequenceNarrow =
(a,b) -> VectorKind.widen2(sequenceFn.apply(a, VectorKind.narrowK(b)));
return General.traverse(zippingApplicative(), sequenceNarrow);
}
/**
*
*
* {@code
* int sum = Vectors.foldable()
.foldLeft(0, (a,b)->a+b, VectorKind.widen(Arrays.asVector(1,2,3,4)));
//10
*
* }
*
*
*
* @return Type class for folding / reduction operations
*/
public static Foldable foldable(){
BiFunction,Higher,T> foldRightFn = (m, l)-> ReactiveSeq.fromIterable(VectorKind.narrow(l)).foldRight(m);
BiFunction,Higher,T> foldLeftFn = (m, l)-> ReactiveSeq.fromIterable(VectorKind.narrow(l)).reduce(m);
return General.foldable(foldRightFn, foldLeftFn);
}
private static VectorKind concat(VectorKind l1, VectorKind l2){
return VectorKind.widen(l1.appendAll(VectorKind.narrow(l2)));
}
private static VectorKind ap(VectorKind> lt, VectorKind list){
return VectorKind.widen(FromCyclopsReact.fromStream(ReactiveSeq.fromIterable(lt.narrow()).zip(list.narrow(), (a, b)->a.apply(b))).toVector());
}
private static Higher flatMap(Higher lt, Function super T, ? extends Higher> fn){
return VectorKind.widen(VectorKind.narrow(lt).flatMap(fn.andThen(VectorKind::narrow)));
}
private static VectorKind map(VectorKind lt, Function super T, ? extends R> fn){
return VectorKind.widen(VectorKind.narrow(lt).map(in->fn.apply(in)));
}
public static Unfoldable unfoldable(){
return new Unfoldable() {
@Override
public Higher unfold(T b, Function super T, Optional>> fn) {
return VectorKind.widen(ReactiveSeq.unfold(b,fn).collect(Vector.collector()));
}
};
}
}
}