T
- type data wrapped by the underlying monadpublic interface AnyM<W extends WitnessType<W>,T> extends Unwrapable, To<AnyM<W,T>>, EmptyUnit<T>, Unit<T>, Folds<T>, Transformable<T>, ToStream<T>, Zippable<T>, org.reactivestreams.Publisher<T>
AnyMValue
and @see AnyMSeq
.
AnyMValue is used to represent Monads that wrap a single value such as Optional
, CompletableFuture
, Maybe
, Eval
, Xor
, Try
, Ior
, FeatureToggle
AnyMSeq is used to represent Monads that wrap an aggregation of values such as Stream
, FutureStream
, List
, Set
, Streamable
Use AnyM to create your monad wrapper.
AnyM.fromXXXX methods can create the appropriate AnyM type for a range of known monad types.
AnyMValue<String> monad1 = AnyM.fromOptional(Optional.of("hello"));
AnyMSeq<String> monad2 = AnyM.fromStream(Stream.of("hello","world"));
Wrapped monads can be unwrapped via the unwrap method, or converted to the desired type via toXXXX methodsModifier and Type | Interface and Description |
---|---|
static class |
AnyM.AnyMFactory |
Modifier and Type | Method and Description |
---|---|
FunctionalAdapter<W> |
adapter() |
default AnyM<W,java.util.List<T>> |
aggregate(AnyM<W,T> next)
Aggregate the contents of this Monad and the supplied Monad
|
static <W extends WitnessType<W>,T,R> |
ap(AnyM<W,java.util.function.Function<T,R>> fn)
Applicative 'ap' method to use fluently
|
static <W extends WitnessType<W>,T,T2,R> |
ap2(AnyM<W,java.util.function.Function<T,java.util.function.Function<T2,R>>> fn)
Applicative ap2 method to use fluently to apply to a curried function
|
default <R> AnyM<W,R> |
coflatMapA(java.util.function.Function<? super AnyM<W,T>,R> mapper)
1.
|
default <R,A> R |
collect(java.util.stream.Collector<? super T,A,R> collector)
Collect the contents of the monad wrapped by this AnyM into supplied collector
A mutable reduction operation equivalent to Stream#collect
|
default <T> AnyM<W,T> |
empty()
Construct an AnyM wrapping a new empty instance of the wrapped type
e.g.
|
default <T> Unit<T> |
emptyUnit() |
default boolean |
eqv(AnyM<?,T> t)
Tests for equivalency between two AnyM types
|
default AnyM<W,T> |
filter(java.util.function.Predicate<? super T> fn)
Perform a filter operation on the wrapped monad instance e.g.
|
default <R> AnyM<W,R> |
flatMapA(java.util.function.Function<? super T,? extends AnyM<W,? extends R>> fn) |
<R> AnyM<W,R> |
flatMapI(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> fn) |
<R> AnyM<W,R> |
flatMapP(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> fn) |
<R> AnyM<W,R> |
flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> fn) |
static <W extends WitnessType<W>,T1> |
flatten(AnyM<W,? extends AnyM<W,T1>> nested)
join / flatten one level of a nested hierarchy
|
static <W extends WitnessType<W>,T1> |
flattenI(AnyM<W,? extends java.lang.Iterable<T1>> nested) |
static <T> AnyMSeq<Witness.stream,T> |
fromArray(T... streamData)
Create an AnyM wrapping a Stream of the supplied data
|
static <W extends Witness.CollectionXWitness<W>,T> |
fromCollectionX(CollectionX<T> collection,
W witness) |
static <T> AnyMValue<Witness.completableFuture,T> |
fromCompletableFuture(java.util.concurrent.CompletableFuture<T> future)
Create an AnyM instance that wraps a CompletableFuture
|
static AnyMSeq<Witness.stream,java.lang.Double> |
fromDoubleStream(java.util.stream.DoubleStream stream)
Create an AnyM instance that wraps an DoubleStream
|
static <LT1,T> AnyMValue<Witness.either,T> |
fromEither(Either<LT1,T> xor)
Create an AnyMValue instance that wraps an Either3
|
static <LT1,LT2,T> |
fromEither3(Either3<LT1,LT2,T> xor)
Create an AnyMValue instance that wraps an Either3
|
static <LT1,LT2,LT3,T> |
fromEither4(Either4<LT1,LT2,LT3,T> xor)
Create an AnyMValue instance that wraps an Either4
|
static <LT1,LT2,LT3,LT4,T> |
fromEither5(Either5<LT1,LT2,LT3,LT4,T> xor)
Create an AnyMValue instance that wraps an Either4
|
static <T> AnyMValue<Witness.eval,T> |
fromEval(Eval<T> eval)
Create an AnyMValue instance that wraps an Eval
|
static <T> AnyMValue<Witness.future,T> |
fromFutureW(Future<T> future)
Create an AnyMValue instance that wraps a Future
|
static AnyMSeq<Witness.stream,java.lang.Integer> |
fromIntStream(java.util.stream.IntStream stream)
Create an AnyM instance that wraps an IntStream
|
static <T> AnyMValue<Witness.ior,T> |
fromIor(Ior<?,T> ior)
Create an AnyMValue instance that wraps an Ior
|
default <T> AnyM<W,T> |
fromIterable(java.lang.Iterable<T> t) |
static <T> AnyMSeq<Witness.list,T> |
fromList(java.util.List<T> list)
Create an AnyM from a List
This AnyM will convert the List to a Stream under the covers, but will rematerialize the Stream as List
if wrap() is called
|
static AnyMSeq<Witness.stream,java.lang.Long> |
fromLongStream(java.util.stream.LongStream stream)
Create an AnyM instance that wraps an LongStream
|
static <T> AnyMValue<Witness.maybe,T> |
fromMaybe(Maybe<T> maybe)
Create an AnyMValue instance that wraps a
Maybe |
static <W extends Witness.MonadicValueWitness<W>,T> |
fromMonadicValue(MonadicValue<T> eval,
W witness) |
static <T> AnyMValue<Witness.optional,T> |
fromOptional(java.util.Optional<T> opt)
Create an AnyM instance that wraps an Optional
|
static AnyMValue<Witness.optional,java.lang.Double> |
fromOptionalDouble(java.util.OptionalDouble optional)
Create an AnyM instance that wraps an OptionalDouble
|
static AnyMValue<Witness.optional,java.lang.Integer> |
fromOptionalInt(java.util.OptionalInt optional)
Create an AnyM instance that wraps an OptionalInt
|
static AnyMValue<Witness.optional,java.lang.Long> |
fromOptionalLong(java.util.OptionalLong optional)
Create an AnyM instance that wraps an OptionalLong
|
static <T> AnyMSeq<Witness.stream,T> |
fromPublisher(org.reactivestreams.Publisher<T> publisher)
Construct an AnyM that wraps a reactive-streams Publisher.
|
static AnyMSeq<Witness.reactiveSeq,java.lang.Integer> |
fromRange(int start,
int end)
Construct an AnyM instance that wraps a range from skip (inclusive) to take (exclusive) provided
The AnyM will contain a SequenceM over the spefied range
|
static AnyMSeq<Witness.reactiveSeq,java.lang.Long> |
fromRangeLong(long start,
long end)
Construct an AnyM instance that wraps a range from skip (inclusive) to take (exclusive) provided
The AnyM will contain a SequenceM over the spefied range
|
static <T> AnyMSeq<Witness.set,T> |
fromSet(java.util.Set<T> set)
Create an AnyM from a Set
This AnyM will convert the Set to a Stream under the covers, but will rematerialize the Stream as Set
if wrap() is called
|
static <T> AnyMSeq<Witness.reactiveSeq,T> |
fromStream(ReactiveSeq<T> stream)
Create an AnyM instance that wraps a Stream
|
static <T> AnyMSeq<Witness.stream,T> |
fromStream(java.util.stream.Stream<T> stream)
Create an AnyM instance that wraps a Stream
|
static <W extends Witness.StreamWitness<W>,T> |
fromStream(java.util.stream.Stream<T> stream,
W witness) |
static <T> AnyMSeq<Witness.streamable,T> |
fromStreamable(ToStream<T> streamable)
Wrap a Streamable inside an AnyM
|
static <T> AnyMValue<Witness.tryType,T> |
fromTry(Try<T,?> trySomething)
Create an AnyMValue instance that wraps a Try
|
static <T> AnyMValue<Witness.xor,T> |
fromXor(Xor<?,T> xor)
Create an AnyMValue instance that wraps an Xor
|
default java.util.Iterator<T> |
iterator() |
static <W extends WitnessType<W>,U,R> |
liftF(java.util.function.Function<? super U,? extends R> fn)
Lift a function so it accepts an AnyM and returns an AnyM (any monad)
AnyM view simplifies type related challenges.
|
static <W extends WitnessType<W>,U1,U2,R> |
liftF2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
Lift a function so it accepts a Monad and returns a Monad (simplex view of a wrapped Monad)
AnyM view simplifies type related challenges.
|
static <W extends WitnessType<W>,U1,U2,R> |
liftF2(java.util.function.Function<U1,java.util.function.Function<U2,R>> fn)
Lift a Curried Function into Monadic form
|
static <W extends WitnessType<W>,U1,U2,U3,R> |
liftF3(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,? extends R>>> fn)
Lift a Curried Function into Monadic form
|
static <W extends WitnessType<W>,U1,U2,U3,R> |
liftF3(org.jooq.lambda.function.Function3<? super U1,? super U2,? super U3,? extends R> fn)
Lift a TriFunction into Monadic form.
|
static <W extends WitnessType<W>,U1,U2,U3,U4,R> |
liftF4(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,java.util.function.Function<? super U4,? extends R>>>> fn)
Lift a Curried Function into Monadic form
|
static <W extends WitnessType<W>,U1,U2,U3,U4,R> |
liftF4(org.jooq.lambda.function.Function4<? super U1,? super U2,? super U3,? super U4,? extends R> fn)
Lift a QuadFunction into Monadic form.
|
static <W extends WitnessType<W>,U1,U2,U3,U4,U5,R> |
liftF5(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,java.util.function.Function<? super U4,java.util.function.Function<? super U5,? extends R>>>>> fn)
Lift a Curried Function
(5 levels a->b->c->d->e->fn.apply(a,b,c,d,e) ) into Monadic form |
static <W extends WitnessType<W>,U1,U2,U3,U4,U5,R> |
liftF5(org.jooq.lambda.function.Function5<? super U1,? super U2,? super U3,? super U4,? super U5,? extends R> fn)
Lift a jOOλ Function5 (5 parameters) into Monadic form
|
default FutureT<W,T> |
liftMFuture() |
default FutureT<W,T> |
liftMFuture(java.util.function.Function<? super T,? extends Future<T>> lift) |
default ListT<W,T> |
liftMList(java.util.function.Function<? super T,? extends FluentSequenceX<T>> lift) |
default ListT<W,T> |
liftMListX() |
static <T> ListX<AnyMValue<Witness.completableFuture,T>> |
listFromCompletableFuture(java.lang.Iterable<java.util.concurrent.CompletableFuture<T>> anyM)
Take an iterable containing CompletableFutures and convert them into a List of AnyMs
e.g.
|
static <ST,T> ListX<AnyMValue<Witness.either,T>> |
listFromEither(java.lang.Iterable<Either<ST,T>> anyM)
Take an iterable containing Either3s and convert them into a List of AnyMs
e.g.
|
static <ST,LT2,T> ListX<AnyMValue<Witness.either3,T>> |
listFromEither3(java.lang.Iterable<Either3<ST,LT2,T>> anyM)
Take an iterable containing Either3s and convert them into a List of AnyMs
e.g.
|
static <ST,LT2,LT3,T> |
listFromEither4(java.lang.Iterable<Either4<ST,LT2,LT3,T>> anyM)
Take an iterable containing Either3s and convert them into a List of AnyMs
e.g.
|
static <ST,LT2,LT3,LT4,T> |
listFromEither5(java.lang.Iterable<Either5<ST,LT2,LT3,LT4,T>> anyM)
Take an iterable containing Either3s and convert them into a List of AnyMs
e.g.
|
static <T> ListX<AnyMValue<Witness.eval,T>> |
listFromEval(java.lang.Iterable<Eval<T>> anyM)
Take an iterable containing Evals and convert them into a List of AnyMs
e.g.
|
static <T> ListX<AnyMValue<Witness.future,T>> |
listFromFutureW(java.lang.Iterable<Future<T>> anyM)
Take an iterable containing Future and convert them into a List of AnyMs
e.g.
|
static <ST,T> ListX<AnyMValue<Witness.ior,T>> |
listFromIor(java.lang.Iterable<Ior<ST,T>> anyM)
Take an iterable containing Iors and convert them into a List of AnyMs
e.g.
|
static <T> ListX<AnyMValue<Witness.maybe,T>> |
listFromMaybe(java.lang.Iterable<Maybe<T>> anyM)
Take an iterable containing Maybes and convert them into a List of AnyMs
e.g.
|
static <T> ListX<AnyMValue<Witness.optional,T>> |
listFromOptional(java.lang.Iterable<java.util.Optional<T>> anyM)
Take an iterable containing Optionals and convert them into a List of AnyMs
e.g.
|
static <T> ListX<AnyMSeq<Witness.stream,T>> |
listFromStream(java.lang.Iterable<java.util.stream.Stream<T>> anyM)
Take an iterable containing Streams and convert them into a List of AnyMs
e.g.
|
static <T> ListX<AnyMSeq<Witness.streamable,T>> |
listFromStreamable(java.lang.Iterable<Streamable<T>> anyM)
Take an iterable containing Streamables and convert them into a List of AnyMs
e.g.
|
static <ST,T> ListX<AnyMValue<Witness.xor,T>> |
listFromXor(java.lang.Iterable<Xor<ST,T>> anyM)
Take an iterable containing Xors and convert them into a List of AnyMs
e.g.
|
static <T> AnyMSeq<Witness.list,T> |
listOf(T... elements) |
default <R> AnyM<W,R> |
map(java.util.function.Function<? super T,? extends R> fn)
Transform this functor using the supplied transformation function
|
Xor<AnyMValue<W,T>,AnyMSeq<W,T>> |
matchable()
Allows structural matching on the value / seq nature of this AnyM.
|
static <W extends WitnessType<W>,T> |
narrow(AnyM<W,? extends T> anyM) |
default AnyM<W,AnyM<W,T>> |
nestA() |
static <T> AnyMValue<Witness.optional,T> |
ofNullable(java.lang.Object nullable)
Generate an AnyM that wraps an Optional from the provided nullable object
|
static <W extends WitnessType<W>,T> |
ofSeq(java.lang.Object monad,
W witness)
Create an AnyMSeq that wraps the untyped monad
|
static <W extends WitnessType<W>,T> |
ofValue(java.lang.Object monad,
FunctionalAdapter<?> adapter) |
static <W extends WitnessType<W>,T> |
ofValue(java.lang.Object monad,
W witness)
Create an AnyMValue that wraps the untyped monad
|
default AnyM<W,T> |
peek(java.util.function.Consumer<? super T> c)
Perform a peek operation on the wrapped monad e.g.
|
default ReactiveSeq<T> |
reactiveSeq() |
default <R> AnyM<W,R> |
retry(java.util.function.Function<? super T,? extends R> fn)
Retry a transformation if it fails.
|
default <R> AnyM<W,R> |
retry(java.util.function.Function<? super T,? extends R> fn,
int retries,
long delay,
java.util.concurrent.TimeUnit timeUnit)
Retry a transformation if it fails.
|
static <W extends WitnessType<W>,T1> |
sequence(java.util.Collection<? extends AnyM<W,T1>> seq,
W w)
Convert a Collection of Monads to a Monad with a List
|
static <W extends WitnessType<W>,T> |
sequence(java.util.stream.Stream<? extends AnyM<W,T>> stream,
W witness) |
default ReactiveSeq<T> |
stream() |
static <T> AnyMSeq<Witness.stream,T> |
streamOf(T... streamData)
Create an AnyM wrapping a Stream of the supplied data
Identical to fromArray, exists as it may appear functionally more obvious to users than fromArray (which fits the convention)
|
java.lang.String |
toString() |
static <W extends WitnessType<W>,T,R> |
traverse(java.util.Collection<? extends AnyM<W,T>> seq,
java.util.function.Function<? super T,? extends R> fn,
W w)
Convert a Collection of Monads to a Monad with a List applying the supplied function in the process
|
static <W extends WitnessType<W>,T,R> |
traverse(java.util.function.Function<T,R> fn,
java.util.stream.Stream<AnyM<W,T>> stream,
W witness) |
default <T> AnyM<W,T> |
unit(T t)
Construct a new instanceof AnyM using the type of the underlying wrapped monad
|
default <U> AnyMSeq<W,U> |
unitIterator(java.util.Iterator<U> U) |
default <S,U> AnyM<W,org.jooq.lambda.tuple.Tuple3<T,S,U>> |
zip3(java.lang.Iterable<? extends S> second,
java.lang.Iterable<? extends U> third)
zip 3 Streams into one
|
default <S,U,R> AnyM<W,R> |
zip3(java.lang.Iterable<? extends S> second,
java.lang.Iterable<? extends U> third,
Fn3<? super T,? super S,? super U,? extends R> fn3) |
default <T2,T3,T4> AnyM<W,org.jooq.lambda.tuple.Tuple4<T,T2,T3,T4>> |
zip4(java.lang.Iterable<? extends T2> second,
java.lang.Iterable<? extends T3> third,
java.lang.Iterable<? extends T4> fourth)
zip 4 Streams into 1
|
default <T2,T3,T4,R> |
zip4(java.lang.Iterable<? extends T2> second,
java.lang.Iterable<? extends T3> third,
java.lang.Iterable<? extends T4> fourth,
Fn4<? super T,? super T2,? super T3,? super T4,? extends R> fn) |
default <U> AnyM<W,org.jooq.lambda.tuple.Tuple2<T,U>> |
zipP(org.reactivestreams.Publisher<? extends U> other) |
default <R> AnyM<W,R> |
zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn) |
default <R> AnyM<W,R> |
zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn) |
default <R> AnyM<W,R> |
zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn) |
unwrap
endsWith, endsWithIterable, findAny, findFirst, firstValue, foldLeft, foldLeft, foldLeft, foldLeft, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, xMatch
cast, trampoline
default ReactiveSeq<T> reactiveSeq()
reactiveSeq
in interface ConvertableToReactiveSeq<T>
reactiveSeq
in interface ToStream<T>
default <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
AnyM<Integer> monad1 = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> monad2 = AnyM.fromOptional(Optional.of(1));
List<Integer> list1 = monad1.collect(Collectors.toList());
List<Integer> list2 = monad2.collect(Collectors.toList());
collector
- JDK collector to perform mutable reductiondefault java.util.Iterator<T> iterator()
iterator
in interface java.lang.Iterable<T>
<R> AnyM<W,R> flatMapI(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> fn)
<R> AnyM<W,R> flatMapP(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> fn)
<R> AnyM<W,R> flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> fn)
default <R> AnyM<W,R> flatMapA(java.util.function.Function<? super T,? extends AnyM<W,? extends R>> fn)
default <R> AnyM<W,R> map(java.util.function.Function<? super T,? extends R> fn)
Transformable
of(1,2,3).map(i->i*2)
//[2,4,6]
map
in interface Transformable<T>
fn
- Transformation functiondefault <R> AnyM<W,R> zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn)
default <R> AnyM<W,R> zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn)
default <R> AnyM<W,R> zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn)
default <R> AnyM<W,R> retry(java.util.function.Function<? super T,? extends R> fn)
Transformable
given(serviceMock.apply(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock)
.firstValue();
//result = 42
retry
in interface Transformable<T>
fn
- Function to retry if failsdefault <U> AnyM<W,org.jooq.lambda.tuple.Tuple2<T,U>> zipP(org.reactivestreams.Publisher<? extends U> other)
default <R> AnyM<W,R> retry(java.util.function.Function<? super T,? extends R> fn, int retries, long delay, java.util.concurrent.TimeUnit timeUnit)
Transformable
given(serviceMock.apply(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock, 7, 2, TimeUnit.SECONDS)
.firstValue();
//result = 42
retry
in interface Transformable<T>
fn
- Function to retry if failsretries
- Number of retriesdelay
- Delay in TimeUnitstimeUnit
- TimeUnit to use for delaydefault <S,U> AnyM<W,org.jooq.lambda.tuple.Tuple3<T,S,U>> zip3(java.lang.Iterable<? extends S> second, java.lang.Iterable<? extends U> third)
Zippable
List<Tuple3<Integer, Integer, Character>> list = of(1, 2, 3, 4, 5, 6).zip3(of(100, 200, 300, 400), of('a', 'b', 'c')).collect(Collectors.toList());
// [[1,100,'a'],[2,200,'b'],[3,300,'c']]
default <S,U,R> AnyM<W,R> zip3(java.lang.Iterable<? extends S> second, java.lang.Iterable<? extends U> third, Fn3<? super T,? super S,? super U,? extends R> fn3)
default <T2,T3,T4> AnyM<W,org.jooq.lambda.tuple.Tuple4<T,T2,T3,T4>> zip4(java.lang.Iterable<? extends T2> second, java.lang.Iterable<? extends T3> third, java.lang.Iterable<? extends T4> fourth)
Zippable
List<Tuple4<Integer, Integer, Character, String>> list = of(1, 2, 3, 4, 5, 6).zip4(of(100, 200, 300, 400), of('a', 'b', 'c'), of("hello", "world"))
.collect(Collectors.toList());
// [[1,100,'a',"hello"],[2,200,'b',"world"]]
default <T2,T3,T4,R> AnyM<W,R> zip4(java.lang.Iterable<? extends T2> second, java.lang.Iterable<? extends T3> third, java.lang.Iterable<? extends T4> fourth, Fn4<? super T,? super T2,? super T3,? super T4,? extends R> fn)
default <T> AnyM<W,T> unit(T t)
AnyM<Integer> ints = AnyM.fromList(Arrays.asList(1,2,3);
AnyM<String> string = ints.unit("hello");
static <W extends WitnessType<W>,T,R> java.util.function.Function<AnyM<W,T>,AnyM<W,R>> ap(AnyM<W,java.util.function.Function<T,R>> fn)
AnyM<optional,Function<Integer,Integer>> add = AnyM.fromNullable(this::add2);
add.to(AnyM::ap)
.apply(AnyM.ofNullable(10));
//AnyM[12] //add 2
fn
- Function inside an Applicativestatic <W extends WitnessType<W>,T,T2,R> java.util.function.BiFunction<AnyM<W,T>,AnyM<W,T2>,AnyM<W,R>> ap2(AnyM<W,java.util.function.Function<T,java.util.function.Function<T2,R>>> fn)
AnyM<optional,Function<Integer,Function<Integer,Integer>>> add = AnyM.fromNullable(Curry.curry2(this::add));
add.to(AnyM::ap2)
.apply(AnyM.ofNullable(10),AnyM.ofNullable(20));
//AnyM[30] //add together
fn
- Curried function inside an Applicativedefault AnyM<W,T> filter(java.util.function.Predicate<? super T> fn)
AnyM.fromOptional(Optional.of(10)).filter(i->i<10);
//AnyM[Optional.empty()]
AnyM.fromStream(Stream.of(5,10)).filter(i->i<10);
//AnyM[Stream[5]]
fn
- Filtering predicatedefault <R> AnyM<W,R> coflatMapA(java.util.function.Function<? super AnyM<W,T>,R> mapper)
default <T> Unit<T> emptyUnit()
default boolean eqv(AnyM<?,T> t)
boolean eqv = AnyM.fromOptional(Optional.of(1)).eqv(AnyM.fromStream(Stream.of(1)));
//true
boolean eqv = AnyM.fromOptional(Optional.of(1)).eqv(AnyM.fromStream(Stream.of(1,2)));
//false
t
- AnyM to check for equivalence with this AnyMXor<AnyMValue<W,T>,AnyMSeq<W,T>> matchable()
AnyM<String> monad;
monad.matchable().visit(v->handleValue(v.get()),s->handleSequence(s.toList()));
default ReactiveSeq<T> stream()
default AnyM<W,T> peek(java.util.function.Consumer<? super T> c)
AnyM.fromCompletableFuture(CompletableFuture.supplyAsync(()->loadData())
.peek(System.out::println)
peek
in interface Transformable<T>
c
- Consumer to accept current datastatic <W extends WitnessType<W>,T1> AnyM<W,T1> flatten(AnyM<W,? extends AnyM<W,T1>> nested)
static <W extends WitnessType<W>,T1> AnyM<W,T1> flattenI(AnyM<W,? extends java.lang.Iterable<T1>> nested)
default AnyM<W,java.util.List<T>> aggregate(AnyM<W,T> next)
AnyM.fromStream(Stream.of(1,2,3,4))
.aggregate(fromEither5(Optional.of(5)))
AnyM[Stream[List[1,2,3,4,5]]
List<Integer> result = AnyM.fromStream(Stream.of(1,2,3,4))
.aggregate(fromEither5(Optional.of(5)))
.toSequence()
.flatten()
.toList();
assertThat(result,equalTo(Arrays.asList(1,2,3,4,5)));
next
- Monad to aggregate content withdefault <T> AnyM<W,T> empty()
Any<Integer> ints = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> empty=ints.empty();
java.lang.String toString()
toString
in class java.lang.Object
static AnyMSeq<Witness.reactiveSeq,java.lang.Integer> fromRange(int start, int end)
start
- Inclusive skip of the rangeend
- Exclusive take of the rangestatic AnyMSeq<Witness.reactiveSeq,java.lang.Long> fromRangeLong(long start, long end)
start
- Inclusive skip of the rangeend
- Exclusive take of the rangestatic <T> AnyMSeq<Witness.streamable,T> fromStreamable(ToStream<T> streamable)
streamable
- wrapstatic <T> AnyMSeq<Witness.list,T> fromList(java.util.List<T> list)
list
- to wrap inside an AnyMstatic <W extends Witness.CollectionXWitness<W>,T> AnyMSeq<W,T> fromCollectionX(CollectionX<T> collection, W witness)
static <T> AnyMSeq<Witness.set,T> fromSet(java.util.Set<T> set)
set
- to wrap inside an AnyMstatic <T> AnyMSeq<Witness.stream,T> fromArray(T... streamData)
streamData
- values to populate a Streamstatic <T> AnyMSeq<Witness.stream,T> streamOf(T... streamData)
streamData
- values to populate a Streamstatic <T> AnyMSeq<Witness.list,T> listOf(T... elements)
static <T> AnyMSeq<Witness.stream,T> fromPublisher(org.reactivestreams.Publisher<T> publisher)
AnyMSeq<Integer> flux = AnyM.fromPublisher(Flux.just(10,20,30));
//with cyclops2-reactor
//AnyM[Flux[Integer]]]
//without cyclops2-reactor
//AnyM[ReactiveSeq[Integer]]]
It is generally safer to define a Comprehender and use a non-converting call to generate the wrapped AnyM
(e.g. Reactor.Flux in cyclops2-reactor for Pivotal Reactor Publishers)publisher
- Publisher to wrap inside an AnyMstatic <T> AnyMSeq<Witness.reactiveSeq,T> fromStream(ReactiveSeq<T> stream)
stream
- Stream to wrapstatic <W extends Witness.StreamWitness<W>,T> AnyMSeq<W,T> fromStream(java.util.stream.Stream<T> stream, W witness)
static <T> AnyMSeq<Witness.stream,T> fromStream(java.util.stream.Stream<T> stream)
stream
- Stream to wrapstatic AnyMSeq<Witness.stream,java.lang.Integer> fromIntStream(java.util.stream.IntStream stream)
stream
- IntStream to wrapstatic AnyMSeq<Witness.stream,java.lang.Double> fromDoubleStream(java.util.stream.DoubleStream stream)
stream
- DoubleStream to wrapstatic AnyMSeq<Witness.stream,java.lang.Long> fromLongStream(java.util.stream.LongStream stream)
stream
- LongStream to wrapstatic <T> AnyMValue<Witness.optional,T> fromOptional(java.util.Optional<T> opt)
opt
- Optional to wrapstatic AnyMValue<Witness.optional,java.lang.Double> fromOptionalDouble(java.util.OptionalDouble optional)
optional
- Optional to wrapstatic AnyMValue<Witness.optional,java.lang.Long> fromOptionalLong(java.util.OptionalLong optional)
optional
- OptionalLong to wrapstatic AnyMValue<Witness.optional,java.lang.Integer> fromOptionalInt(java.util.OptionalInt optional)
optional
- OptionalInt to wrapstatic <T> AnyMValue<Witness.completableFuture,T> fromCompletableFuture(java.util.concurrent.CompletableFuture<T> future)
future
- CompletableFuture to wrapstatic <T> AnyMValue<Witness.xor,T> fromXor(Xor<?,T> xor)
xor
- Xor to wrap inside an AnyMstatic <LT1,T> AnyMValue<Witness.either,T> fromEither(Either<LT1,T> xor)
xor
- Xor to wrap inside an AnyMstatic <LT1,LT2,T> AnyMValue<Witness.either3,T> fromEither3(Either3<LT1,LT2,T> xor)
xor
- Xor to wrap inside an AnyMstatic <LT1,LT2,LT3,T> AnyMValue<Witness.either4,T> fromEither4(Either4<LT1,LT2,LT3,T> xor)
xor
- Either4 to wrap inside an AnyMstatic <LT1,LT2,LT3,LT4,T> AnyMValue<Witness.either5,T> fromEither5(Either5<LT1,LT2,LT3,LT4,T> xor)
xor
- Either4 to wrap inside an AnyMstatic <T> AnyMValue<Witness.tryType,T> fromTry(Try<T,?> trySomething)
trySomething
- to wrap inside an AnyMstatic <T> AnyMValue<Witness.ior,T> fromIor(Ior<?,T> ior)
ior
- to wrap inside an AnyMstatic <T> AnyMValue<Witness.eval,T> fromEval(Eval<T> eval)
eval
- to wrap inside an AnyMstatic <W extends Witness.MonadicValueWitness<W>,T> AnyMValue<W,T> fromMonadicValue(MonadicValue<T> eval, W witness)
static <T> AnyMValue<Witness.future,T> fromFutureW(Future<T> future)
future
- to wrap inside an AnyMstatic <T> AnyMValue<Witness.maybe,T> fromMaybe(Maybe<T> maybe)
Maybe
maybe
- to wrap inside an AnyMstatic <W extends WitnessType<W>,T> AnyMValue<W,T> ofValue(java.lang.Object monad, W witness)
monad
- to wrap inside an AnyMstatic <W extends WitnessType<W>,T> AnyMValue<W,T> ofValue(java.lang.Object monad, FunctionalAdapter<?> adapter)
static <W extends WitnessType<W>,T> AnyMSeq<W,T> ofSeq(java.lang.Object monad, W witness)
monad
- to wrap inside an AnyMstatic <T> AnyMValue<Witness.optional,T> ofNullable(java.lang.Object nullable)
nullable
- - Nullable object to generate an optional fromstatic <T> ListX<AnyMSeq<Witness.streamable,T>> listFromStreamable(java.lang.Iterable<Streamable<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromStreamable(Arrays.asList(Streamable.of(1,2,3),Streamable.of(10,20,30));
//List[AnyM[Streamable[1,2,3],Streamable[10,20,30]]]
anyM
- Iterable containing Streamablesstatic <ST,LT2,LT3,LT4,T> ListX<AnyMValue<Witness.either5,T>> listFromEither5(java.lang.Iterable<Either5<ST,LT2,LT3,LT4,T>> anyM)
List<AnyM<Integer>> anyMs = anyMList(Arrays.asList(Either4.right(1),Either4.left(10));
//List[AnyM[Either4:right[1],Either4:left[10]]]
anyM
- Iterable containing Eithersstatic <ST,LT2,LT3,T> ListX<AnyMValue<Witness.either4,T>> listFromEither4(java.lang.Iterable<Either4<ST,LT2,LT3,T>> anyM)
List<AnyM<Integer>> anyMs = anyMList(Arrays.asList(Either4.right(1),Either4.left(10));
//List[AnyM[Either4:right[1],Either4:left[10]]]
anyM
- Iterable containing Eithersstatic <ST,LT2,T> ListX<AnyMValue<Witness.either3,T>> listFromEither3(java.lang.Iterable<Either3<ST,LT2,T>> anyM)
List<AnyM<Integer>> anyMs = listFromEither3(Arrays.asList(Either3.right(1),Either3.left(10));
//List[AnyM[Either3:right[1],Either3:left[10]]]
anyM
- Iterable containing Eithersstatic <ST,T> ListX<AnyMValue<Witness.either,T>> listFromEither(java.lang.Iterable<Either<ST,T>> anyM)
List<AnyM<Integer>> anyMs = anyMList(Arrays.asList(Either.right(1),Either.left(10));
//List[AnyM[Either.right[1],Either:left[10]]]
anyM
- Iterable containing Eithersstatic <T> ListX<AnyMSeq<Witness.stream,T>> listFromStream(java.lang.Iterable<java.util.stream.Stream<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromStream(Arrays.asList(Stream.of(1,2,3),Stream.of(10,20,30));
//List[AnyM[Stream[1,2,3],Stream[10,20,30]]]
anyM
- Iterable containing Streamsstatic <T> ListX<AnyMValue<Witness.optional,T>> listFromOptional(java.lang.Iterable<java.util.Optional<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromStreamable(Arrays.asList(Optional.of(1),Optional.of(10));
//List[AnyM[Optional[1],Optional[10]]]
anyM
- Iterable containing Optionalstatic <T> ListX<AnyMValue<Witness.completableFuture,T>> listFromCompletableFuture(java.lang.Iterable<java.util.concurrent.CompletableFuture<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromStreamable(Arrays.asList(CompletableFuture.completedFuture(1),CompletableFuture.supplyAsync(()->10));
//List[AnyM[CompletableFuture[1],CompleteableFuture[10]]]
anyM
- Iterable containing CompletableFuturestatic <ST,T> ListX<AnyMValue<Witness.xor,T>> listFromXor(java.lang.Iterable<Xor<ST,T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromXor(Arrays.asList(Xor.primary(1),Xor.secondary(10));
//List[AnyM[Xor:primary[1],Xor:secondaary[10]]]
anyM
- Iterable containing Xorsstatic <ST,T> ListX<AnyMValue<Witness.ior,T>> listFromIor(java.lang.Iterable<Ior<ST,T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromXor(Arrays.asList(Ior.primary(1),Ior.secondary(10));
//List[AnyM[Ior:primary[1],Ior:secondaary[10]]]
anyM
- Iterable containing Iorsstatic <T> ListX<AnyMValue<Witness.maybe,T>> listFromMaybe(java.lang.Iterable<Maybe<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromXor(Arrays.asList(Maybe.just(1),Maybe.just(10));
//List[AnyM[Maybe[1],Maybe[10]]]
anyM
- Iterable containing Maybesstatic <T> ListX<AnyMValue<Witness.eval,T>> listFromEval(java.lang.Iterable<Eval<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromXor(Arrays.asList(Eval.now(1),Eval.now(10));
//List[AnyM[Eval[1],Eval[10]]]
anyM
- Iterable containing Maybesstatic <T> ListX<AnyMValue<Witness.future,T>> listFromFutureW(java.lang.Iterable<Future<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromXor(Arrays.asList(Future.ofResult(1),Future.ofResult(10));
//List[AnyM[Future[1],Future[10]]]
anyM
- Iterable containing Maybesstatic <W extends WitnessType<W>,T1> AnyM<W,ListX<T1>> sequence(java.util.Collection<? extends AnyM<W,T1>> seq, W w)
List<CompletableFuture<Integer>> futures = createFutures();
AnyM<List<Integer>> futureList = AnyMonads.sequence(AsAnyMList.anyMList(futures));
//where AnyM wraps CompletableFuture<List<Integer>>
seq
- Collection of monads to convertstatic <W extends WitnessType<W>,T,R> AnyM<W,ListX<R>> traverse(java.util.Collection<? extends AnyM<W,T>> seq, java.util.function.Function<? super T,? extends R> fn, W w)
List<CompletableFuture<Integer>> futures = createFutures();
AnyM<List<String>> futureList = AnyMonads.traverse(AsAnyMList.anyMList(futures), (Integer i) -> "hello" +i);
seq
- Collection of Monadsfn
- Function to applystatic <W extends WitnessType<W>,T> AnyM<W,java.util.stream.Stream<T>> sequence(java.util.stream.Stream<? extends AnyM<W,T>> stream, W witness)
static <W extends WitnessType<W>,T,R> AnyM<W,java.util.stream.Stream<R>> traverse(java.util.function.Function<T,R> fn, java.util.stream.Stream<AnyM<W,T>> stream, W witness)
FunctionalAdapter<W> adapter()
static <W extends WitnessType<W>,T> AnyM<W,T> narrow(AnyM<W,? extends T> anyM)
static <W extends WitnessType<W>,U,R> AnyMFn1<W,U,R> liftF(java.util.function.Function<? super U,? extends R> fn)
fn
- static <W extends WitnessType<W>,U1,U2,R> AnyMFn2<W,U1,U2,R> liftF2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
BiFunction<AnyM<Integer>,AnyM<Integer>,AnyM<Integer>> add = Monads.liftF2(this::add);
Optional<Integer> result = add.apply(getBase(),getIncrease());
private Integer add(Integer a, Integer b){
return a+b;
}
The add method has no null handling, but we can lift the method to Monadic form, and use Optionals to automatically handle null / empty value cases.fn
- BiFunction to liftstatic <W extends WitnessType<W>,U1,U2,U3,R> Fn3<AnyM<W,U1>,AnyM<W,U2>,AnyM<W,U3>,AnyM<W,R>> liftF3(org.jooq.lambda.function.Function3<? super U1,? super U2,? super U3,? extends R> fn)
TriFunction<AnyM<Double>,AnyM<Entity>,AnyM<W,String>,AnyM<Integer>> fn = liftF3(this::myMethod);
Now we can execute the Method with Streams, Optional, Futures, Try's etc to transparently inject iteration, null handling, async execution and / or error handlingfn
- Function to liftstatic <W extends WitnessType<W>,U1,U2,U3,U4,R> Fn4<AnyM<W,U1>,AnyM<W,U2>,AnyM<W,U3>,AnyM<W,U4>,AnyM<W,R>> liftF4(org.jooq.lambda.function.Function4<? super U1,? super U2,? super U3,? super U4,? extends R> fn)
fn
- Quad funciton to liftstatic <W extends WitnessType<W>,U1,U2,U3,U4,U5,R> Fn5<AnyM<W,U1>,AnyM<W,U2>,AnyM<W,U3>,AnyM<W,U4>,AnyM<W,U5>,AnyM<W,R>> liftF5(org.jooq.lambda.function.Function5<? super U1,? super U2,? super U3,? super U4,? super U5,? extends R> fn)
fn
- Function to liftstatic <W extends WitnessType<W>,U1,U2,R> java.util.function.Function<AnyM<W,U1>,java.util.function.Function<AnyM<W,U2>,AnyM<W,R>>> liftF2(java.util.function.Function<U1,java.util.function.Function<U2,R>> fn)
fn
- Function to liftstatic <W extends WitnessType<W>,U1,U2,U3,R> java.util.function.Function<AnyM<W,U1>,java.util.function.Function<AnyM<W,U2>,java.util.function.Function<AnyM<W,U3>,AnyM<W,R>>>> liftF3(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,? extends R>>> fn)
fn
- Function to liftstatic <W extends WitnessType<W>,U1,U2,U3,U4,R> java.util.function.Function<AnyM<W,U1>,java.util.function.Function<AnyM<W,U2>,java.util.function.Function<AnyM<W,U3>,java.util.function.Function<AnyM<W,U4>,AnyM<W,R>>>>> liftF4(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,java.util.function.Function<? super U4,? extends R>>>> fn)
fn
- Function to liftstatic <W extends WitnessType<W>,U1,U2,U3,U4,U5,R> java.util.function.Function<AnyM<W,U1>,java.util.function.Function<AnyM<W,U2>,java.util.function.Function<AnyM<W,U3>,java.util.function.Function<AnyM<W,U4>,java.util.function.Function<AnyM<W,U5>,AnyM<W,R>>>>>> liftF5(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,java.util.function.Function<? super U4,java.util.function.Function<? super U5,? extends R>>>>> fn)
(5 levels a->b->c->d->e->fn.apply(a,b,c,d,e) )
into Monadic formfn
- Function to liftdefault FutureT<W,T> liftMFuture(java.util.function.Function<? super T,? extends Future<T>> lift)
default ListT<W,T> liftMList(java.util.function.Function<? super T,? extends FluentSequenceX<T>> lift)