cyclops.companion.vavr.Streams 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 cyclops.monads.VavrWitness.queue;
import cyclops.monads.VavrWitness.tryType;
import io.vavr.Lazy;
import io.vavr.collection.*;
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.control.Eval;
import cyclops.control.Maybe;
import cyclops.control.Reader;
import cyclops.control.Xor;
import cyclops.conversion.vavr.FromCyclopsReact;
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.ListKind;
import cyclops.monads.VavrWitness;
import cyclops.monads.VavrWitness.stream;
import com.aol.cyclops.vavr.hkt.StreamKind;
import cyclops.monads.AnyM;
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.List;
import io.vavr.collection.Stream;
import lombok.experimental.UtilityClass;
import org.jooq.lambda.tuple.Tuple2;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import static com.aol.cyclops.vavr.hkt.StreamKind.widen;
public class Streams {
public static Coproduct coproduct(Stream type, InstanceDefinitions def1){
return Coproduct.of(Xor.primary(widen(type)),def1, Instances.definitions());
}
public static Coproduct coproduct(InstanceDefinitions def1,T... values){
return coproduct(Stream.of(values),def1);
}
public static ,T> XorM xorM(Stream type){
return XorM.right(anyM(type));
}
public static ,T> XorM xorM(T... values){
return xorM(Stream.of(values));
}
public static AnyMSeq anyM(Stream option) {
return AnyM.ofSeq(option, stream.INSTANCE);
}
/**
* Perform a For Comprehension over a Stream, accepting 3 generating functions.
* This results in a four level nested internal iteration over the provided Publishers.
*
*
* {@code
*
* import static cyclops.Streams.forEach4;
*
forEach4(IntStream.range(1,10).boxed(),
a-> Stream.iterate(a,i->i+1).limit(10),
(a,b) -> Stream.of(a+b),
(a,b,c) -> Stream.just(a+b+c),
Tuple::tuple)
*
* }
*
*
* @param value1 top level Stream
* @param value2 Nested Stream
* @param value3 Nested Stream
* @param value4 Nested Stream
* @param yieldingFunction Generates a result per combination
* @return Stream with an element per combination of nested publishers generated by the yielding function
*/
public static Stream forEach4(Stream extends T1> value1,
Function super T1, ? extends Stream> value2,
BiFunction super T1, ? super R1, ? extends Stream> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends Stream> value4,
Fn4 super T1, ? super R1, ? super R2, ? super R3, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Stream a = value2.apply(in);
return a.flatMap(ina -> {
Stream b = value3.apply(in,ina);
return b.flatMap(inb -> {
Stream c = value4.apply(in,ina,inb);
return c.map(in2 -> yieldingFunction.apply(in, ina, inb, in2));
});
});
});
}
/**
* Perform a For Comprehension over a Stream, accepting 3 generating function.
* This results in a four level nested internal iteration over the provided Publishers.
*
* {@code
*
* import static com.aol.cyclops2.reactor.Streames.forEach4;
*
* forEach4(IntStream.range(1,10).boxed(),
a-> Stream.iterate(a,i->i+1).limit(10),
(a,b) -> Stream.just(a+b),
(a,b,c) -> Stream.just(a+b+c),
(a,b,c,d) -> a+b+c+d <100,
Tuple::tuple);
*
* }
*
*
* @param value1 top level Stream
* @param value2 Nested Stream
* @param value3 Nested Stream
* @param value4 Nested Stream
* @param filterFunction A filtering function, keeps values where the predicate holds
* @param yieldingFunction Generates a result per combination
* @return Stream with an element per combination of nested publishers generated by the yielding function
*/
public static Stream forEach4(Stream extends T1> value1,
Function super T1, ? extends Stream> value2,
BiFunction super T1, ? super R1, ? extends Stream> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends Stream> 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 -> {
Stream a = value2.apply(in);
return a.flatMap(ina -> {
Stream b = value3.apply(in,ina);
return b.flatMap(inb -> {
Stream 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 Stream, accepting 2 generating function.
* This results in a three level nested internal iteration over the provided Publishers.
*
*
* {@code
*
* import static Streams.forEach3;
*
* forEach(IntStream.range(1,10).boxed(),
a-> Stream.iterate(a,i->i+1).limit(10),
(a,b) -> Stream.of(a+b),
Tuple::tuple);
*
* }
*
*
*
* @param value1 top level Stream
* @param value2 Nested Stream
* @param value3 Nested Stream
* @param yieldingFunction Generates a result per combination
* @return Stream with an element per combination of nested publishers generated by the yielding function
*/
public static Stream forEach3(Stream extends T1> value1,
Function super T1, ? extends Stream> value2,
BiFunction super T1, ? super R1, ? extends Stream> value3,
Fn3 super T1, ? super R1, ? super R2, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Stream a = value2.apply(in);
return a.flatMap(ina -> {
Stream b = value3.apply(in,ina);
return b.map(in2 -> yieldingFunction.apply(in, ina, in2));
});
});
}
/**
* Perform a For Comprehension over a Stream, accepting 2 generating function.
* This results in a three level nested internal iteration over the provided Publishers.
*
* {@code
*
* import static Streams.forEach;
*
* forEach(IntStream.range(1,10).boxed(),
a-> Stream.iterate(a,i->i+1).limit(10),
(a,b) -> Stream.of(a+b),
(a,b,c) ->a+b+c<10,
Tuple::tuple)
.toStreamX();
* }
*
*
* @param value1 top level Stream
* @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 Stream forEach3(Stream extends T1> value1,
Function super T1, ? extends Stream> value2,
BiFunction super T1, ? super R1, ? extends Stream> value3,
Fn3 super T1, ? super R1, ? super R2, Boolean> filterFunction,
Fn3 super T1, ? super R1, ? super R2, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Stream a = value2.apply(in);
return a.flatMap(ina -> {
Stream 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 Stream, accepting an additonal generating function.
* This results in a two level nested internal iteration over the provided Publishers.
*
*
* {@code
*
* import static Streams.forEach2;
* forEach(IntStream.range(1, 10).boxed(),
* i -> Stream.range(i, 10), Tuple::tuple)
.forEach(System.out::println);
//(1, 1)
(1, 2)
(1, 3)
(1, 4)
...
*
* }
*
* @param value1 top level Stream
* @param value2 Nested publisher
* @param yieldingFunction Generates a result per combination
* @return
*/
public static Stream forEach2(Stream extends T> value1,
Function super T, Stream> value2,
BiFunction super T, ? super R1, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Stream a = value2.apply(in);
return a.map(in2 -> yieldingFunction.apply(in, in2));
});
}
/**
*
*
* {@code
*
* import static Streams.forEach2;
*
* forEach(IntStream.range(1, 10).boxed(),
* i -> Stream.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 Stream
* @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 Stream forEach2(Stream extends T> value1,
Function super T, ? extends Stream> value2,
BiFunction super T, ? super R1, Boolean> filterFunction,
BiFunction super T, ? super R1, ? extends R> yieldingFunction) {
return value1.flatMap(in -> {
Stream a = value2.apply(in);
return a.filter(in2->filterFunction.apply(in,in2))
.map(in2 -> yieldingFunction.apply(in, in2));
});
}
public static Active allTypeclasses(Stream array){
return Active.of(widen(array), Streams.Instances.definitions());
}
public static Nested mapM(Stream array, Function super T,? extends Higher> fn, InstanceDefinitions defs){
Stream> e = array.map(fn);
StreamKind> lk = widen(e);
return Nested.of(lk, Streams.Instances.definitions(), defs);
}
/**
* Companion class for creating Type Class instances for working with Streams
* @author johnmcclean
*
*/
@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
* StreamKind list = Streams.functor().map(i->i*2, StreamKind.widen(Arrays.asStream(1,2,3));
*
* //[2,4,6]
*
*
* }
*
*
* An example fluent api working with Streams
*
* {@code
* StreamKind list = Streams.unit()
.unit("hello")
.then(h->Streams.functor().map((String v) ->v.length(), h))
.convert(StreamKind::narrowK);
*
* }
*
*
*
* @return A functor for Streams
*/
public static Functor functor(){
BiFunction,Function super T, ? extends R>,StreamKind> map = Instances::map;
return General.functor(map);
}
/**
*
* {@code
* StreamKind list = Streams.unit()
.unit("hello")
.convert(StreamKind::narrowK);
//Arrays.asStream("hello"))
*
* }
*
*
*
* @return A factory for Streams
*/
public static Pure unit(){
return General.unit(StreamKind::just);
}
/**
*
*
* {@code
* import static com.aol.cyclops.hkt.jdk.StreamKind.widen;
* import static com.aol.cyclops.util.function.Lambda.l1;
* import static java.util.Arrays.asStream;
*
Streams.zippingApplicative()
.ap(widen(asStream(l1(this::multiplyByTwo))),widen(asStream(1,2,3)));
*
* //[2,4,6]
* }
*
*
*
* Example fluent API
*
* {@code
* StreamKind> listFn =Streams.unit()
* .unit(Lambda.l1((Integer i) ->i*2))
* .convert(StreamKind::narrowK);
StreamKind list = Streams.unit()
.unit("hello")
.then(h->Streams.functor().map((String v) ->v.length(), h))
.then(h->Streams.zippingApplicative().ap(listFn, h))
.convert(StreamKind::narrowK);
//Arrays.asStream("hello".length()*2))
*
* }
*
*
*
* @return A zipper for Streams
*/
public static Applicative zippingApplicative(){
BiFunction>,StreamKind,StreamKind> ap = Instances::ap;
return General.applicative(functor(), unit(), ap);
}
/**
*
*
* {@code
* import static com.aol.cyclops.hkt.jdk.StreamKind.widen;
* StreamKind list = Streams.monad()
.flatMap(i->widen(StreamX.range(0,i)), widen(Arrays.asStream(1,2,3)))
.convert(StreamKind::narrowK);
* }
*
*
* Example fluent API
*
* {@code
* StreamKind list = Streams.unit()
.unit("hello")
.then(h->Streams.monad().flatMap((String v) ->Streams.unit().unit(v.length()), h))
.convert(StreamKind::narrowK);
//Arrays.asStream("hello".length())
*
* }
*
*
* @return Type class with monad functions for Streams
*/
public static Monad monad(){
BiFunction,Function super T, ? extends Higher>,Higher> flatMap = Instances::flatMap;
return General.monad(zippingApplicative(), flatMap);
}
/**
*
*
* {@code
* StreamKind list = Streams.unit()
.unit("hello")
.then(h->Streams.monadZero().filter((String t)->t.startsWith("he"), h))
.convert(StreamKind::narrowK);
//Arrays.asStream("hello"));
*
* }
*
*
*
* @return A filterable monad (with default value)
*/
public static MonadZero monadZero(){
return General.monadZero(monad(), widen(Stream.empty()));
}
/**
*
* {@code
* StreamKind list = Streams.monadPlus()
.plus(StreamKind.widen(Arrays.asStream()), StreamKind.widen(Arrays.asStream(10)))
.convert(StreamKind::narrowK);
//Arrays.asStream(10))
*
* }
*
* @return Type class for combining Streams by concatenation
*/
public static MonadPlus monadPlus(){
Monoid> m = Monoid.of(widen(Stream.empty()), Instances::concat);
Monoid> m2= (Monoid)m;
return General.monadPlus(monadZero(),m2);
}
/**
*
*
* {@code
* Monoid> m = Monoid.of(StreamKind.widen(Arrays.asStream()), (a,b)->a.isEmpty() ? b : a);
StreamKind list = Streams.monadPlus(m)
.plus(StreamKind.widen(Arrays.asStream(5)), StreamKind.widen(Arrays.asStream(10)))
.convert(StreamKind::narrowK);
//Arrays.asStream(5))
*
* }
*
*
* @param m Monoid to use for combining Streams
* @return Type class for combining Streams
*/
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,StreamKind>,Higher>> sequenceFn = (ap, list) -> {
Higher> identity = ap.unit(widen(Stream.empty()));
BiFunction>,Higher,Higher>> combineToStream = (acc, next) -> ap.apBiFn(ap.unit((a, b) -> widen(StreamKind.narrow(a).append(b))),
acc,next);
BinaryOperator>> combineStreams = (a, b)-> ap.apBiFn(ap.unit((l1, l2)-> widen(StreamKind.narrow(l1).appendAll(l2))),a,b); ;
return ReactiveSeq.fromIterable(StreamKind.narrow(list))
.reduce(identity,
combineToStream,
combineStreams);
};
BiFunction,Higher>,Higher>> sequenceNarrow =
(a,b) -> StreamKind.widen2(sequenceFn.apply(a, StreamKind.narrowK(b)));
return General.traverse(zippingApplicative(), sequenceNarrow);
}
/**
*
*
* {@code
* int sum = Streams.foldable()
.foldLeft(0, (a,b)->a+b, StreamKind.widen(Arrays.asStream(1,2,3,4)));
//10
*
* }
*
*
*
* @return Type class for folding / reduction operations
*/
public static Foldable foldable(){
BiFunction,Higher,T> foldRightFn = (m, l)-> ReactiveSeq.fromIterable(StreamKind.narrow(l)).foldRight(m);
BiFunction,Higher,T> foldLeftFn = (m, l)-> ReactiveSeq.fromIterable(StreamKind.narrow(l)).reduce(m);
return General.foldable(foldRightFn, foldLeftFn);
}
private static StreamKind concat(StreamKind l1, StreamKind l2){
return widen(l1.appendAll(StreamKind.narrow(l2)));
}
private static StreamKind ap(StreamKind> lt, StreamKind list){
return widen(FromCyclopsReact.fromStream(ReactiveSeq.fromIterable(lt).zip(list, (a, b)->a.apply(b))).toStream());
}
private static Higher flatMap(Higher lt, Function super T, ? extends Higher> fn){
return widen(StreamKind.narrow(lt).flatMap(fn.andThen(StreamKind::narrow)));
}
private static StreamKind map(StreamKind lt, Function super T, ? extends R> fn){
return widen(StreamKind.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 widen(Stream.ofAll((Iterable)ReactiveSeq.unfold(b,fn)));
}
};
}
}
public static interface StreamNested{
public static Nested lazy(Stream> type){
return Nested.of(widen(type.map(LazyKind::widen)),Instances.definitions(),Lazys.Instances.definitions());
}
public static Nested streamTry(Stream> type){
return Nested.of(widen(type.map(TryKind::widen)),Instances.definitions(),Trys.Instances.definitions());
}
public static Nested future(Stream> type){
return Nested.of(widen(type.map(FutureKind::widen)),Instances.definitions(),Futures.Instances.definitions());
}
public static Nested queue(Stream> nested){
return Nested.of(widen(nested.map(QueueKind::widen)),Instances.definitions(),Queues.Instances.definitions());
}
public static Nested, R> either(Stream> nested){
return Nested.of(widen(nested.map(EitherKind::widen)),Instances.definitions(),Eithers.Instances.definitions());
}
public static Nested stream(Stream> nested){
return Nested.of(widen(nested.map(StreamKind::widen)),Instances.definitions(),Streams.Instances.definitions());
}
public static Nested list(Stream> nested){
return Nested.of(widen(nested.map(ListKind::widen)), Instances.definitions(),Lists.Instances.definitions());
}
public static Nested array(Stream> nested){
return Nested.of(widen(nested.map(ArrayKind::widen)),Instances.definitions(),Arrays.Instances.definitions());
}
public static Nested vector(Stream> nested){
return Nested.of(widen(nested.map(VectorKind::widen)),Instances.definitions(),Vectors.Instances.definitions());
}
public static Nested set(Stream> nested){
return Nested.of(widen(nested.map(HashSetKind::widen)),Instances.definitions(), HashSets.Instances.definitions());
}
public static Nested reactiveSeq(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),ReactiveSeq.Instances.definitions());
}
public static Nested maybe(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),Maybe.Instances.definitions());
}
public static Nested eval(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),Eval.Instances.definitions());
}
public static Nested cyclopsFuture(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),cyclops.async.Future.Instances.definitions());
}
public static Nested, P> xor(Stream> nested){
StreamKind> x = widen(nested);
StreamKind, P>> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),Xor.Instances.definitions());
}
public static Nested, T> reader(Stream> nested){
StreamKind> x = widen(nested);
StreamKind, T>> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),Reader.Instances.definitions());
}
public static Nested, P> cyclopsTry(Stream> nested){
StreamKind> x = widen(nested);
StreamKind, P>> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(),cyclops.control.Try.Instances.definitions());
}
public static Nested streamal(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(), Optionals.Instances.definitions());
}
public static Nested completableStream(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(), CompletableFutures.Instances.definitions());
}
public static Nested javaStream(Stream> nested){
StreamKind> x = widen(nested);
StreamKind> y = (StreamKind)x;
return Nested.of(y,Instances.definitions(), cyclops.companion.Streams.Instances.definitions());
}
}
public static interface NestedStream{
public static Nested reactiveSeq(ReactiveSeq> nested){
ReactiveSeq> x = nested.map(StreamKind::widenK);
return Nested.of(x,ReactiveSeq.Instances.definitions(),Instances.definitions());
}
public static Nested maybe(Maybe> nested){
Maybe> x = nested.map(StreamKind::widenK);
return Nested.of(x,Maybe.Instances.definitions(),Instances.definitions());
}
public static Nested eval(Eval> nested){
Eval> x = nested.map(StreamKind::widenK);
return Nested.of(x,Eval.Instances.definitions(),Instances.definitions());
}
public static Nested cyclopsFuture(cyclops.async.Future> nested){
cyclops.async.Future