cyclops.companion.vavr.Lists 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.*;
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 extends T1> value1,
Function super T1, ? extends List> value2,
BiFunction super T1, ? super R1, ? extends List> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends List> value4,
Fn4 super T1, ? super R1, ? super R2, ? super R3, ? extends R> 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 extends T1> value1,
Function super T1, ? extends List> value2,
BiFunction super T1, ? super R1, ? extends List> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends List> 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 -> {
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 extends T1> value1,
Function super T1, ? extends List> value2,
BiFunction super T1, ? super R1, ? extends List> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends R> 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 extends T1> value1,
Function super T1, ? extends List> value2,
BiFunction super T1, ? super R1, ? extends List> value3,
Fn3 super T1, ? super R1, ? super R2, Boolean> filterFunction,
Fn3 super T1, ? super R1, ? super R2, ? extends R> 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 extends T> value1,
Function super T, List> value2,
BiFunction super T, ? super R1, ? extends R> 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 extends T> value1,
Function super T, ? extends List> value2,
BiFunction super T, ? super R1, Boolean> filterFunction,
BiFunction super T, ? super R1, ? extends R> 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 super T,? extends Higher> 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 super T, ? extends R>,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 super T, ? extends Higher>,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 super T>,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 super T, ? extends Higher> fn){
return widen(ListKind.narrowK(lt).flatMap(fn.andThen(ListKind::narrowK)));
}
private static ListKind map(ListKind lt, Function super T, ? extends R> fn){
return widen(lt.map(fn));
}
private static ListKind filter(Higher lt, Predicate super T> fn){
return widen(ListKind.narrow(lt).filter(fn));
}
public static Unfoldable unfoldable(){
return new Unfoldable() {
@Override
public Higher unfold(T b, Function super T, Optional>> 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