T
- Data types of elements managed by wrapped scalar Monad.public interface AnyMValue<T> extends AnyM<T>, Value<T>, Filterable<T>, Combiner<T>, ApplicativeFunctor<T>, MonadicValue<T>, Matchable.ValueAndOptionalMatcher<T>
AnyM.AnyMFactory
ApplicativeFunctor.Applicatives, ApplicativeFunctor.ApplyFunctions<T>
Value.ValueImpl<T>
Convertable.SupplierToConvertable<T>
Modifier and Type | Method and Description |
---|---|
AnyMValue<java.util.List<T>> |
aggregate(AnyM<T> next)
Aggregate the contents of this Monad and the supplied Monad
|
<R> AnyMValue<R> |
applyM(AnyMValue<java.util.function.Function<? super T,? extends R>> fn)
Apply function/s inside supplied Monad to data in current Monad
e.g.
|
<R> AnyMValue<R> |
bind(java.util.function.Function<? super T,?> fn)
Perform a looser typed flatMap / bind operation
The return type can be another type other than the host type
Note the modified javaslang monad laws are not applied during the looser typed bind operation
The modification being used to work around the limits of the Java type system.
|
default <U> AnyMValue<U> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException . |
default <R> AnyMValue<R> |
coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
Perform a coflatMap operation.
|
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 AnyMValue<T> |
combine(java.util.function.BinaryOperator<Combiner<T>> combiner,
Combiner<T> app)
Combine two applicatives together using the provided BinaryOperator (Semigroup, Monoid and Reducer all
extend BinaryOperator - checkout Semigroups and Monoids for a large number of canned combinations).
|
default AnyMValue<T> |
combine(Monoid<T> monoid,
AnyMValue<? extends T> v2) |
default <T2,R> AnyMValue<R> |
combine(Value<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Lazily combine this ApplicativeFunctor with the supplied value via the supplied BiFunction
Example
|
<T> AnyMValue<T> |
empty()
Construct an AnyM wrapping a new empty instance of the wrapped type
e.g.
|
<T> AnyMValue<T> |
emptyUnit() |
default boolean |
eqv(AnyMValue<T> t)
Equivalence test, returns true if this Monad is equivalent to the supplied monad
e.g.
|
AnyMValue<T> |
filter(java.util.function.Predicate<? super T> p)
Perform a filter operation on the wrapped monad instance e.g.
|
default AnyMValue<T> |
filterNot(java.util.function.Predicate<? super T> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
<R> AnyMValue<R> |
flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
flatMap operation
AnyM follows the javaslang modified 'monad' laws https://gist.github.com/danieldietrich/71be006b355d6fbc0584
In particular left-identity becomes
Left identity: unit(a).flatMap(f) ≡ select(f.apply(a))
Or in plain English, if your flatMap function returns multiple values (such as flatMap by Stream) but the current Monad only can only hold one value,
only the first value is accepted.
|
<R> AnyMValue<R> |
flatMapFirst(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> fn)
Perform a flatMap operation that will only work as normal for AnyMSeq types, but for AnyMValue (which can only hold a single value)
will take the first value from the Iterable returned.
|
<R> AnyMValue<R> |
flatMapFirstPublisher(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> fn)
Perform a flatMap operation that will only work as normal for AnyMSeq types, but for AnyMValue (which can only hold a single value)
will take the first value from the Publisher returned.
|
<T1> AnyMValue<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
default <R1,R> AnyMValue<R> |
forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
Perform a two level nested internal iteration over this MonadicValue and the
supplied MonadicValue
|
default <R1,R> MonadicValue<R> |
forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,java.lang.Boolean> filterFunction,
java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
Perform a two level nested internal iteration over this MonadicValue and the
supplied stream
|
default <T2,R1,R2,R> |
forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
TriFunction<? super T,? super R1,? super R2,? extends R> yieldingFunction)
Perform a three level nested internal iteration over this MonadicValue and the
supplied MonadicValues
|
default <T2,R1,R2,R> |
forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
TriFunction<? super T,? super R1,? super R2,java.lang.Boolean> filterFunction,
TriFunction<? super T,? super R1,? super R2,? extends R> yieldingFunction)
Perform a three level nested internal iteration over this MonadicValue and the
supplied MonadicValues
|
default <T2,R1,R2,R3,R> |
forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3,
QuadFunction<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
Perform a four level nested internal iteration over this MonadicValue and the
supplied MonadicValues
|
default <T2,R1,R2,R3,R> |
forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1,
java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2,
TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3,
QuadFunction<? super T,? super R1,? super R2,? super R3,java.lang.Boolean> filterFunction,
QuadFunction<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
Perform a four level nested internal iteration over this MonadicValue and the
supplied MonadicValues
Maybe.of(3)
.forEach4(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->Maybe.none(),
(a,b,c,d)->a+b+c<100,
(a,b,c,d)->a+b+c+d);
//Maybe.none
|
default java.util.Iterator<T> |
iterator() |
static <U,R> java.util.function.Function<AnyMValue<U>,AnyMValue<R>> |
liftM(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 <U1,U2,R> java.util.function.BiFunction<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<R>> |
liftM2(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 <U1,U2,R> java.util.function.Function<AnyMValue<U1>,java.util.function.Function<AnyMValue<U2>,AnyMValue<R>>> |
liftM2(java.util.function.Function<U1,java.util.function.Function<U2,R>> fn)
Lift a Curried Function into Monadic form
|
static <U1,U2,U3,R> |
liftM3(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 <U1,U2,U3,R> |
liftM3(org.jooq.lambda.function.Function3<? super U1,? super U2,? super U3,? extends R> fn)
Lift a jOOλ Function3 into Monadic form.
|
static <U1,U2,U3,R> |
liftM3Cyclops(TriFunction<? super U1,? super U2,? super U3,? extends R> fn)
Lift a TriFunction into Monadic form.
|
static <U1,U2,U3,U4,R> |
liftM4(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 <U1,U2,U3,U4,R> |
liftM4(org.jooq.lambda.function.Function4<? super U1,? super U2,? super U3,? super U4,? extends R> fn)
Lift a jOOλ Function4 into Monadic form.
|
static <U1,U2,U3,U4,R> |
liftM4Cyclops(QuadFunction<? super U1,? super U2,? super U3,? super U4,? extends R> fn)
Lift a QuadFunction into Monadic form.
|
static <U1,U2,U3,U4,U5,R> |
liftM5(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 <U1,U2,U3,U4,U5,R> |
liftM5(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
|
static <U1,U2,U3,U4,U5,R> |
liftM5Cyclops(QuintFunction<? super U1,? super U2,? super U3,? super U4,? super U5,? extends R> fn)
Lift a QuintFunction (5 parameters) into Monadic form
|
<R> AnyMValue<R> |
map(java.util.function.Function<? super T,? extends R> fn)
Perform a map operation on the wrapped monad instance e.g.
|
default java.lang.String |
mkString()
Returns the class name and the name of the subclass, if there is any value, the value is showed between square brackets.
|
default AnyMValue<MonadicValue<T>> |
nest()
cojoin pattern.
|
default AnyMValue<T> |
notNull()
Filter elements retaining only values which are not null
|
default <U> AnyMValue<U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a stream that are of a given type.
|
default <R> AnyMValue<R> |
patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1,
java.util.function.Supplier<? extends R> otherwise)
Transform the elements of this Stream with a Pattern Matching case and default value
|
AnyMValue<T> |
peek(java.util.function.Consumer<? super T> c)
Perform a peek operation on the wrapped monad e.g.
|
AnyM<java.util.List<T>> |
replicateM(int times)
Replicate given Monad
|
static <T1> AnyMValue<ListX<T1>> |
sequence(java.util.Collection<? extends AnyMValue<T1>> seq)
Convert a Collection of Monads to a Monad with a List
|
static <T> AnyMValue<java.util.stream.Stream<T>> |
sequence(java.util.stream.Stream<AnyMValue<T>> source,
java.util.function.Supplier<AnyMValue<java.util.stream.Stream<T>>> unitEmpty)
Convert a Stream of Monads to a Monad with a Stream
|
ReactiveSeq<T> |
stream() |
default Value<T> |
toFirstValue() |
<NT> ReactiveSeq<NT> |
toReactiveSeq(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends NT>> fn)
Sequence the contents of a Monad.
|
<T> ReactiveSeq<T> |
toSequence()
Optional<List<Integer>> into Stream<Integer> |
default <R> AnyMValue<R> |
trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Performs a map operation that can call a recursive method without running out of stack space
|
static <T,R> AnyMValue<ListX<R>> |
traverse(java.util.Collection<? extends AnyMValue<T>> seq,
java.util.function.Function<? super T,? extends R> fn)
Convert a Stream of Monads to a Monad with a List applying the supplied function in the process
|
static <T,R> AnyMValue<java.util.stream.Stream<R>> |
traverse(java.util.stream.Stream<AnyMValue<T>> source,
java.util.function.Supplier<AnyMValue<java.util.stream.Stream<T>>> unitEmpty,
java.util.function.Function<? super T,? extends R> fn)
Convert a Stream of Monads to a Monad with a Stream applying the supplied function in the process
|
<T> AnyMValue<T> |
unit(T value)
Construct a new instanceof AnyM using the type of the underlying wrapped monad
|
default <T2,R> AnyMValue<R> |
zip(java.util.function.BiFunction<? super T,? super T2,? extends R> fn,
org.reactivestreams.Publisher<? extends T2> app)
Zip (combine) this Zippable with the supplied Publisher, using the supplied combining function
|
default <T2,R> AnyMValue<R> |
zip(java.lang.Iterable<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zip (combine) this Zippable with the supplied Iterable using the supplied combining function
|
eqv, fromArray, fromCompletableFuture, fromCompletableFutureTSeq, fromCompletableFutureTValue, fromDoubleStream, fromEval, fromEvalTSeq, fromEvalTValue, fromFeatureToggle, fromFutureW, fromFutureWTSeq, fromFutureWTValue, fromIntStream, fromIor, fromIterable, fromIterableValue, fromList, fromListT, fromLongStream, fromMaybe, fromMaybeTSeq, fromMaybeTValue, fromOptional, fromOptionalDouble, fromOptionalInt, fromOptionalLong, fromOptionalTSeq, fromOptionalTValue, fromPublisher, fromRange, fromRangeLong, fromSet, fromSetT, fromStream, fromStreamable, fromStreamableT, fromStreamT, fromTry, fromTryTSeq, fromTryTValue, fromXor, fromXorTSeq, fromXorTValue, liftM3, liftM4, liftM5, listFromCompletableFuture, listFromEval, listFromFutureW, listFromIor, listFromIterable, listFromIterator, listFromMaybe, listFromOptional, listFromStream, listFromStreamable, listFromXor, matchable, ofConvertableSeq, ofConvertableValue, ofNullable, ofSeq, ofValue, reduceMSeq, reduceMValue, sequence, sequence, streamOf, toString, traverse, traverse, unwrap
futureStream, getStreamable, isEmpty, jdkStream, reactiveSeq, reveresedJDKStream, reveresedStream
applyFunctions
fold, fold, generate, iterate, mapReduce, newSubscriber, of, subscribe, test, toDequeX, toEvalAlways, toEvalLater, toEvalNow, toFeatureToggle, toFutureStream, toFutureStream, toIor, toLazyImmutable, toList, toListX, toMaybe, toMutable, toPBagX, toPOrderedSetX, toPQueueX, toPSetX, toPStackX, toPVectorX, toQueueX, toSetX, toSimpleReact, toSimpleReact, toSortedSetX, toTry, toTry, toTry, toXor, toXor, unapply
endsWith, endsWithIterable, findAny, findFirst, firstValue, foldable, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, validate, xMatch
collect, fromSupplier, get, isPresent, orElse, orElseGet, orElseThrow, toAtomicReference, toCompletableFuture, toCompletableFutureAsync, toCompletableFutureAsync, toFutureW, toFutureWAsync, toFutureWAsync, toOptional, toOptionalAtomicReference, toStream, visit
anyM, combineEager, flatMapIterable, flatMapPublisher
toOptional, visit
matches
default boolean eqv(AnyMValue<T> t)
{code Optional.of(1) and CompletableFuture.completedFuture(1) are equivalent }
t
- Monad to compare todefault AnyMValue<T> combine(java.util.function.BinaryOperator<Combiner<T>> combiner, Combiner<T> app)
Combiner
combine
in interface Combiner<T>
Semigroup
,
Semigroups
,
Monoid
,
To lift any Semigroup (or monoid) up to handling Applicatives use the combineApplicatives operator in Semigroups
{@see com.aol.cyclops.Semigroups#combineApplicatives(BiFunction) } or Monoids
{ {@see com.aol.cyclops.Monoids#combineApplicatives(java.util.function.Function, com.aol.cyclops.Monoid)
}
{@code
BinaryOperator> sumMaybes = Semigroups.combineScalarFunctors(Semigroups.intSum);
Maybe.just(1)
.combine(sumMaybes, Maybe.just(5))
//Maybe.just(6));
}
default <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
AnyM
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());
default <T2,R> AnyMValue<R> combine(Value<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Combiner
Maybe<Integer> some = Maybe.just(10);
just.combine(Eval.now(20), this::add);
//Some[30]
Maybe<Integer> none = Maybe.none();
none.combine(Eval.now(20), this::add);
//None
default <T2,R> AnyMValue<R> zip(java.lang.Iterable<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zippable
default <T2,R> AnyMValue<R> zip(java.util.function.BiFunction<? super T,? super T2,? extends R> fn, org.reactivestreams.Publisher<? extends T2> app)
Zippable
<R> AnyMValue<R> flatMapFirst(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> fn)
AnyM
flatMapFirst
in interface AnyM<T>
fn
- FlatMaping function<R> AnyMValue<R> flatMapFirstPublisher(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> fn)
AnyM
flatMapFirstPublisher
in interface AnyM<T>
fn
- FlatMaping functiondefault <R> AnyMValue<R> coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
MonadicValue
Maybe.none().coflatMap(m -> m.isPresent() ? m.get() : 10);
//Maybe[10]
coflatMap
in interface MonadicValue<T>
mapper
- Mapping / transformation functiondefault AnyMValue<MonadicValue<T>> nest()
MonadicValue
nest
in interface MonadicValue<T>
default java.lang.String mkString()
Value
default <U> AnyMValue<U> ofType(java.lang.Class<? extends U> type)
Filterable
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
ofType
in interface Filterable<T>
default AnyMValue<T> filterNot(java.util.function.Predicate<? super T> fn)
Filterable
of(1,2,3).filter(i->i>2);
//[1,2]
filterNot
in interface Filterable<T>
fn
- to filter elements by, retaining matchesdefault AnyMValue<T> notNull()
Filterable
of(1,2,null,4).nonNull();
//[1,2,4]
notNull
in interface Filterable<T>
default <U> AnyMValue<U> cast(java.lang.Class<? extends U> type)
Functor
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)default <R> AnyMValue<R> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Functor
ReactiveSeq.of(10,20,30,40)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
Trampoline<Long> fibonacci(int i){
return fibonacci(i,1,0);
}
Trampoline<Long> fibonacci(int n, long a, long b) {
return n == 0 ? Trampoline.done(b) : Trampoline.more( ()->fibonacci(n-1, a+b, a));
}
55
6765
832040
102334155
ReactiveSeq.of(10_000,200_000,3_000_000,40_000_000)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
completes successfully
trampoline
in interface Functor<T>
mapper
- TCO Transformation functiondefault <R> AnyMValue<R> patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1, java.util.function.Supplier<? extends R> otherwise)
Functor
List<String> result = CollectionX.of(1,2,3,4)
.patternMatch(
c->c.valuesWhere(i->"even", (Integer i)->i%2==0 )
)
// CollectionX["odd","even","odd","even"]
patternMatch
in interface Functor<T>
case1
- Function to generate a case (or chain of cases as a single case)otherwise
- Value if supplied case doesn't match<T> AnyMValue<T> emptyUnit()
AnyM<java.util.List<T>> replicateM(int times)
AnyM<Optional<Integer>> applied =AnyM.fromOptional(Optional.of(2)).replicateM(5);
//AnyM[Optional[List(2,2,2,2,2)]]
times
- number of times to replicateReactiveSeq<T> stream()
AnyMValue<T> filter(java.util.function.Predicate<? super T> p)
AnyM
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]]
filter
in interface AnyM<T>
filter
in interface Filterable<T>
filter
in interface MonadicValue<T>
p
- Filtering predicate<R> AnyMValue<R> map(java.util.function.Function<? super T,? extends R> fn)
AnyM
AnyM.fromIterable(Try.runWithCatch(this::loadData))
.map(data->transform(data))
AnyM.fromStream(Stream.of(1,2,3))
.map(i->i+2);
//AnyM[Stream[3,4,5]]
AnyMValue<T> peek(java.util.function.Consumer<? super T> c)
AnyM
AnyM.fromCompletableFuture(CompletableFuture.supplyAsync(()->loadData())
.peek(System.out::println)
<R> AnyMValue<R> bind(java.util.function.Function<? super T,?> fn)
AnyM
AnyM<List<Integer>> m = AnyM.fromStream(Stream.of(Arrays.asList(1,2,3),Arrays.asList(1,2,3)));
AnyM<Integer> intM = m.bind(Collection::stream);
<T1> AnyMValue<T1> flatten()
AnyM
AnyMValue<java.util.List<T>> aggregate(AnyM<T> next)
AnyM
AnyM.fromStream(Stream.of(1,2,3,4))
.aggregate(anyM(Optional.of(5)))
AnyM[Stream[List[1,2,3,4,5]]
List<Integer> result = AnyM.fromStream(Stream.of(1,2,3,4))
.aggregate(anyM(Optional.of(5)))
.toSequence()
.flatten()
.toList();
assertThat(result,equalTo(Arrays.asList(1,2,3,4,5)));
static <T,R> AnyMValue<ListX<R>> traverse(java.util.Collection<? extends AnyMValue<T>> seq, java.util.function.Function<? super T,? extends R> fn)
Stream<CompletableFuture<Integer>> futures = createFutures();
AnyMValue<List<String>> futureList = AnyMonads.traverse(AsAnyMList.anyMList(futures), (Integer i) -> "hello" +i);
seq
- Stream of Monadsfn
- Function to applystatic <T,R> AnyMValue<java.util.stream.Stream<R>> traverse(java.util.stream.Stream<AnyMValue<T>> source, java.util.function.Supplier<AnyMValue<java.util.stream.Stream<T>>> unitEmpty, java.util.function.Function<? super T,? extends R> fn)
static <T> AnyMValue<java.util.stream.Stream<T>> sequence(java.util.stream.Stream<AnyMValue<T>> source, java.util.function.Supplier<AnyMValue<java.util.stream.Stream<T>>> unitEmpty)
static <T1> AnyMValue<ListX<T1>> sequence(java.util.Collection<? extends AnyMValue<T1>> seq)
List<CompletableFuture<Integer>> futures = createFutures();
AnyMValue<List<Integer>> futureList = AnyMonads.sequence(AsAnyMList.anyMList(futures));
//where AnyM wraps CompletableFuture<List<Integer>>
seq
- Collection of monads to convertdefault <R1,R> AnyMValue<R> forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach2(a->Maybe.none(),
(a,b)->a+b);
//Maybe.none()
forEach2
in interface MonadicValue<T>
value1
- Nested Monadic Type to iterate overyieldingFunction
- Function with pointers to the current element from both
monad types that generates the new elementsdefault <R1,R> MonadicValue<R> forEach2(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,java.lang.Boolean> filterFunction, java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach2(a->Maybe.none(),
a->b-> a<3 && b>10,
(a,b)->a+b);
//Maybe.none()
forEach2
in interface MonadicValue<T>
filterFunction
- Filter to apply over elements before passing non-filtered
values to the yielding functionyieldingFunction
- Function with pointers to the current element from both
Streams that generates the new elementsdefault <T2,R1,R2,R> AnyMValue<R> forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, TriFunction<? super T,? super R1,? super R2,java.lang.Boolean> filterFunction, TriFunction<? super T,? super R1,? super R2,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach3(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->a+b+c<100,
(a,b,c)->a+b+c);
//Maybe[32]
forEach3
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overfilterFunction
- Filter to apply over elements before passing non-filtered
values to the yielding functionyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <T2,R1,R2,R> AnyMValue<R> forEach3(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, TriFunction<? super T,? super R1,? super R2,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach3(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->a+b+c<100,
(a,b,c)->a+b+c);
//Maybe[32]
forEach3
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <T2,R1,R2,R3,R> AnyMValue<R> forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3, QuadFunction<? super T,? super R1,? super R2,? super R3,java.lang.Boolean> filterFunction, QuadFunction<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach4(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->Maybe.none(),
(a,b,c,d)->a+b+c<100,
(a,b,c,d)->a+b+c+d);
//Maybe.none
forEach4
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overvalue3
- Nested MonadicValue to iterate overfilterFunction
- Filter to apply over elements before passing non-filtered
values to the yielding functionyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elementsdefault <T2,R1,R2,R3,R> MonadicValue<R> forEach4(java.util.function.Function<? super T,? extends MonadicValue<R1>> value1, java.util.function.BiFunction<? super T,? super R1,? extends MonadicValue<R2>> value2, TriFunction<? super T,? super R1,? super R2,? extends MonadicValue<R3>> value3, QuadFunction<? super T,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
MonadicValue
Maybe.of(3)
.forEach4(a->Maybe.just(a+10),
(a,b)->Maybe.just(a+b),
(a,b,c)->Maybe.none(),
(a,b,c,d)->a+b+c+d);
//Maybe.none
forEach4
in interface MonadicValue<T>
value1
- Nested MonadicValue to iterate overvalue2
- Nested MonadicValue to iterate overvalue3
- Nested MonadicValue to iterate overyieldingFunction
- Function with pointers to the current element from both
MonadicValue that generates the new elements<R> AnyMValue<R> flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i->AnyM.fromArray(i+1,i+2));
//AnyM[Stream[2,3,3,4,4,5]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMap(i->AnyM.fromArray(i+1,i+2));
//AnyM[Optional[2]]
flatMap
in interface MonadicValue<T>
fn
- flatMap function<R> AnyMValue<R> applyM(AnyMValue<java.util.function.Function<? super T,? extends R>> fn)
AnyM<Integer> applied =AnyM.fromStream(Stream.of(1,2,3))
.applyM(AnyM.fromStreamable(Streamable.of( (Integer a)->a+1 ,(Integer a) -> a*2)));
assertThat(applied.toList(),equalTo(Arrays.asList(2, 2, 3, 4, 4, 6)));
with Optionals
Any<Integer> applied =AnyM.fromOptional(Optional.of(2)).applyM(AnyM.fromOptional(Optional.of( (Integer a)->a+1)) );
assertThat(applied.toList(),equalTo(Arrays.asList(3)));
fn
- <NT> ReactiveSeq<NT> toReactiveSeq(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends NT>> fn)
Optional<List<Integer>> into Stream<Integer>
List<Integer> list = AnyM.fromOptional(Optional.of(Arrays.asList(1,2,3,4,5,6)))
.<Integer>toSequence(c->c.stream())
.collect(Collectors.toList());
assertThat(list,hasItems(1,2,3,4,5,6));
toReactiveSeq
in interface AnyM<T>
fn
- Function used to convert contents to Stream<T> ReactiveSeq<T> toSequence()
Optional<List<Integer>> into Stream<Integer>
Less type safe equivalent, but may be more accessible than toSequence(fn) i.e.
toSequence(Function<T,Stream<NT>> fn)
List<Integer> list = anyM(Optional.of(Arrays.asList(1,2,3,4,5,6)))
.<Integer>toSequence()
.collect(Collectors.toList());
<T> AnyMValue<T> unit(T value)
AnyM
AnyM<Integer> ints = AnyM.fromList(Arrays.asList(1,2,3);
AnyM<String> string = ints.unit("hello");
<T> AnyMValue<T> empty()
AnyM
Any<Integer> ints = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> empty=ints.empty();
default java.util.Iterator<T> iterator()
iterator
in interface Convertable<T>
iterator
in interface java.lang.Iterable<T>
iterator
in interface Matchable.MatchableOptional<T>
iterator
in interface Matchable.ValueAndOptionalMatcher<T>
iterator
in interface ToStream<T>
iterator
in interface Value<T>
static <U,R> java.util.function.Function<AnyMValue<U>,AnyMValue<R>> liftM(java.util.function.Function<? super U,? extends R> fn)
static <U1,U2,R> java.util.function.BiFunction<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<R>> liftM2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
BiFunction<AnyMValue<Integer>,AnyMValue<Integer>,AnyMValue<Integer>> add = Monads.liftM2(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.static <U1,U2,U3,R> org.jooq.lambda.function.Function3<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<U3>,AnyMValue<R>> liftM3(org.jooq.lambda.function.Function3<? super U1,? super U2,? super U3,? extends R> fn)
Function3 <AnyMValue<Double>,AnyMValue<Entity>,AnyMValue<String>,AnyMValue<Integer>> fn = liftM3(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 <U1,U2,U3,R> TriFunction<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<U3>,AnyMValue<R>> liftM3Cyclops(TriFunction<? super U1,? super U2,? super U3,? extends R> fn)
TriFunction<AnyMValue<Double>,AnyMValue<Entity>,AnyMValue<String>,AnyMValue<Integer>> fn = liftM3(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 <U1,U2,U3,U4,R> org.jooq.lambda.function.Function4<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<U3>,AnyMValue<U4>,AnyMValue<R>> liftM4(org.jooq.lambda.function.Function4<? super U1,? super U2,? super U3,? super U4,? extends R> fn)
fn
- Quad funciton to liftstatic <U1,U2,U3,U4,R> QuadFunction<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<U3>,AnyMValue<U4>,AnyMValue<R>> liftM4Cyclops(QuadFunction<? super U1,? super U2,? super U3,? super U4,? extends R> fn)
fn
- Quad funciton to liftstatic <U1,U2,U3,U4,U5,R> org.jooq.lambda.function.Function5<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<U3>,AnyMValue<U4>,AnyMValue<U5>,AnyMValue<R>> liftM5(org.jooq.lambda.function.Function5<? super U1,? super U2,? super U3,? super U4,? super U5,? extends R> fn)
fn
- Function to liftstatic <U1,U2,U3,U4,U5,R> QuintFunction<AnyMValue<U1>,AnyMValue<U2>,AnyMValue<U3>,AnyMValue<U4>,AnyMValue<U5>,AnyMValue<R>> liftM5Cyclops(QuintFunction<? super U1,? super U2,? super U3,? super U4,? super U5,? extends R> fn)
fn
- Function to liftstatic <U1,U2,R> java.util.function.Function<AnyMValue<U1>,java.util.function.Function<AnyMValue<U2>,AnyMValue<R>>> liftM2(java.util.function.Function<U1,java.util.function.Function<U2,R>> fn)
static <U1,U2,U3,R> java.util.function.Function<AnyMValue<U1>,java.util.function.Function<AnyMValue<U2>,java.util.function.Function<AnyMValue<U3>,AnyMValue<R>>>> liftM3(java.util.function.Function<? super U1,java.util.function.Function<? super U2,java.util.function.Function<? super U3,? extends R>>> fn)
static <U1,U2,U3,U4,R> java.util.function.Function<AnyMValue<U1>,java.util.function.Function<AnyMValue<U2>,java.util.function.Function<AnyMValue<U3>,java.util.function.Function<AnyMValue<U4>,AnyMValue<R>>>>> liftM4(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)
static <U1,U2,U3,U4,U5,R> java.util.function.Function<AnyMValue<U1>,java.util.function.Function<AnyMValue<U2>,java.util.function.Function<AnyMValue<U3>,java.util.function.Function<AnyMValue<U4>,java.util.function.Function<AnyMValue<U5>,AnyMValue<R>>>>>> liftM5(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 form