T
- type data wrapped by the underlying monadpublic interface AnyM<T> extends Unwrapable, ToStream<T>, ApplyM<T>, FlatMapM<T>, ReduceM<T>
Modifier and Type | Method and Description |
---|---|
AnyM<java.util.List<T>> |
aggregate(AnyM<T> next)
Aggregate the contents of this Monad and the supplied Monad
|
<R> AnyM<R> |
applyM(AnyM<java.util.function.Function<? super T,? extends R>> fn)
Apply function/s inside supplied Monad to data in current Monad
e.g.
|
SequenceM<T> |
asSequence()
Wrap this Monad's contents as a Sequence without disaggreating it.
|
<R> AnyM<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.
|
<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
|
<T> AnyM<T> |
empty()
Construct an AnyM wrapping an empty instance of the wrapped type
e.g.
|
AnyM<T> |
filter(java.util.function.Predicate<? super T> p)
Perform a filter operation on the wrapped monad instance e.g.
|
<R> AnyM<R> |
flatMap(java.util.function.Function<? super T,AnyM<? 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.
|
<T1> AnyM<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
<R1,R> AnyM<R> |
forEach2(java.util.function.Function<? super T,? extends AnyM<R1>> monad,
java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends R>> yieldingFunction)
Perform a two level nested internal iteration over this Stream and the supplied monad (allowing null handling, exception handling
etc to be injected, for example)
|
<R1,R> AnyM<R> |
forEach2(java.util.function.Function<? super T,? extends AnyM<R1>> monad,
java.util.function.Function<? super T,java.util.function.Function<? super R1,java.lang.Boolean>> filterFunction,
java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends R>> yieldingFunction)
Perform a two level nested internal iteration over this Stream and the supplied monad (allowing null handling, exception handling
etc to be injected, for example)
|
<R1,R2,R> AnyM<R> |
forEach3(java.util.function.Function<? super T,? extends AnyM<R1>> monad1,
java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends AnyM<R2>>> monad2,
java.util.function.Function<? super T,java.util.function.Function<? super R1,java.util.function.Function<? super R2,? extends R>>> yieldingFunction)
Perform a three level nested internal iteration over this AnyM and the supplied monads
|
<R1,R2,R> AnyM<R> |
forEach3(java.util.function.Function<? super T,? extends AnyM<R1>> monad1,
java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends AnyM<R2>>> monad2,
java.util.function.Function<? super T,java.util.function.Function<? super R1,java.util.function.Function<? super R2,java.lang.Boolean>>> filterFunction,
java.util.function.Function<? super T,java.util.function.Function<? super R1,java.util.function.Function<? super R2,? extends R>>> yieldingFunction)
Perform a three level nested internal iteration over this Stream and the supplied streams
|
static <T> AnyM<T> |
fromArray(T... streamData)
Create an AnyM wrapping a Stream of the supplied data
|
static <T> AnyM<T> |
fromCollection(java.util.Collection<T> collection)
Create an AnyM instance that wraps a Collection
|
static <T> AnyM<T> |
fromCompletableFuture(java.util.concurrent.CompletableFuture<T> future)
Create an AnyM instance that wraps a CompletableFuture
|
static AnyM<java.lang.Double> |
fromDoubleStream(java.util.stream.DoubleStream stream)
Create an AnyM instance that wraps an DoubleStream
|
static AnyM<java.lang.String> |
fromFile(java.io.File file)
Create an AnyM instance that wraps an textual Stream from a file
|
static AnyM<java.lang.Integer> |
fromIntStream(java.util.stream.IntStream stream)
Create an AnyM instance that wraps an IntStream
|
static <T> AnyM<T> |
fromIterable(java.lang.Iterable<T> iterable)
Create an AnyM instance that wraps an Iterable
|
static <T> AnyM<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 AnyM<java.lang.Long> |
fromLongStream(java.util.stream.LongStream stream)
Create an AnyM instance that wraps an LongStream
|
static <T> AnyM<T> |
fromOptional(java.util.Optional<T> optional)
Create an AnyM instance that wraps an Optional
|
static AnyM<java.lang.Double> |
fromOptionalDouble(java.util.OptionalDouble optional)
Create an AnyM instance that wraps an OptionalDouble
|
static AnyM<java.lang.Integer> |
fromOptionalInt(java.util.OptionalInt optional)
Create an AnyM instance that wraps an OptionalInt
|
static AnyM<java.lang.Long> |
fromOptionalLong(java.util.OptionalLong optional)
Create an AnyM instance that wraps an OptionalLong
|
static AnyM<java.lang.Integer> |
fromRange(int start,
int end)
Construct an AnyM instance that wraps a range from start (inclusive) to end (exclusive) provided
The AnyM will contain a SequenceM over the spefied range
|
static AnyM<java.lang.Long> |
fromRangeLong(long start,
long end)
Construct an AnyM instance that wraps a range from start (inclusive) to end (exclusive) provided
The AnyM will contain a SequenceM over the spefied range
|
static <T> AnyM<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> AnyM<T> |
fromStream(java.util.stream.Stream<T> stream)
Create an AnyM instance that wraps a Stream
|
static <T> AnyM<T> |
fromStreamable(ToStream<T> streamable)
Wrap a Streamable inside an AnyM
|
static AnyM<java.lang.String> |
fromURL(java.net.URL url)
Create an AnyM instance that wraps an textual Stream from a URL
|
<R> AnyM<R> |
liftAndBind(java.util.function.Function<? super T,?> fn)
Perform a bind operation (@see #bind) but also lift the return value into a Monad using configured
MonadicConverters
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.
|
static <U,R> java.util.function.Function<AnyM<U>,AnyM<R>> |
liftM(java.util.function.Function<U,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<AnyM<U1>,AnyM<U2>,AnyM<R>> |
liftM2(java.util.function.BiFunction<U1,U2,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<AnyM<U1>,java.util.function.Function<AnyM<U2>,AnyM<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<U1,java.util.function.Function<U2,java.util.function.Function<U3,R>>> fn)
Lift a Curried Function into Monadic form
|
static <U1,U2,U3,R> |
liftM3(org.jooq.lambda.function.Function3<U1,U2,U3,R> fn)
Lift a jOOλ Function3 into Monadic form.
|
static <U1,U2,U3,U4,R> |
liftM4(java.util.function.Function<U1,java.util.function.Function<U2,java.util.function.Function<U3,java.util.function.Function<U4,R>>>> fn)
Lift a Curried Function into Monadic form
|
static <U1,U2,U3,U4,R> |
liftM4(org.jooq.lambda.function.Function4<U1,U2,U3,U4,R> fn)
Lift a jOOλ Function4 into Monadic form.
|
static <U1,U2,U3,U4,U5,R> |
liftM5(java.util.function.Function<U1,java.util.function.Function<U2,java.util.function.Function<U3,java.util.function.Function<U4,java.util.function.Function<U5,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<U1,U2,U3,U4,U5,R> fn)
Lift a jOOλ Function5 (5 parameters) into Monadic form
|
static <T> java.util.List<AnyM<T>> |
listFromCollection(java.lang.Iterable<java.util.Collection<T>> anyM)
Take an iterable containing Streamables and convert them into a List of AnyMs
e.g.
|
static <T> java.util.List<AnyM<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 <T> java.util.List<AnyM<T>> |
listFromIterable(java.lang.Iterable<java.lang.Iterable<T>> anyM)
Take an iterable containing Streamables and convert them into a List of AnyMs
e.g.
|
static <T> java.util.List<AnyM<T>> |
listFromIterator(java.lang.Iterable<java.util.Iterator<T>> anyM)
Take an iterable containing Streamables and convert them into a List of AnyMs
e.g.
|
static <T> java.util.List<AnyM<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> java.util.List<AnyM<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> java.util.List<AnyM<T>> |
listFromStreamable(java.lang.Iterable<Streamable<T>> anyM)
Take an iterable containing Streamables and convert them into a List of AnyMs
e.g.
|
<R> AnyM<R> |
map(java.util.function.Function<? super T,? extends R> fn)
Perform a map operation on the wrapped monad instance e.g.
|
<X> X |
monad() |
static <T> AnyM<T> |
ofConvertable(java.lang.Object monad)
Take the supplied object and always attempt to convert it to a Monad type
|
static <T> AnyM<T> |
ofMonad(java.lang.Object monad)
Take the supplied object and wrap it inside an AnyM - must be a supported monad type already
|
static <T> java.util.List<AnyM<T>> |
ofMonadList(java.lang.Iterable<java.lang.Object> anyM)
Take an iterable containing monads and convert it into a List of AnyMs
Uses ofMonad to take the supplied object and wrap it inside an AnyM - must be a supported monad type already
|
static <T> AnyM<T> |
ofNullable(java.lang.Object nullable)
Generate an AnyM that wraps an Optional from the provided nullable object
|
AnyM<T> |
peek(java.util.function.Consumer<? super T> c)
Perform a peek operation on the wrapped monad e.g.
|
AnyM<T> |
reduceM(Monoid<AnyM<T>> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
AnyM<java.util.List<T>> |
replicateM(int times)
Replicate given Monad
|
static <T1> AnyM<java.util.stream.Stream<T1>> |
sequence(java.util.Collection<AnyM<T1>> seq)
Convert a Collection of Monads to a Monad with a List
|
static <T1> AnyM<java.util.stream.Stream<T1>> |
sequence(java.util.stream.Stream<AnyM<T1>> seq)
Convert a Stream of Monads to a Monad with a List
|
SequenceM<T> |
stream() |
static <T> AnyM<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)
|
default java.util.concurrent.CompletableFuture<java.util.List<T>> |
toCompletableFuture() |
java.util.List<T> |
toList()
Convert this monad into a List
|
default java.util.Optional<java.util.List<T>> |
toOptional() |
<T> SequenceM<T> |
toSequence()
Optional<List<Integer>> into Stream<Integer> |
<NT> SequenceM<NT> |
toSequence(java.util.function.Function<T,java.util.stream.Stream<NT>> fn)
Sequence the contents of a Monad.
|
java.util.Set<T> |
toSet()
Convert this monad into a Set
|
java.lang.String |
toString() |
static <T,R> AnyM<java.util.List<R>> |
traverse(java.util.Collection<AnyM<T>> seq,
java.util.function.Function<T,R> fn)
Convert a Collection of Monads to a Monad with a List applying the supplied function in the process
|
static <T,R> AnyM<java.util.List<R>> |
traverse(java.util.stream.Stream<AnyM<T>> seq,
java.util.function.Function<T,R> fn)
Convert a Stream of Monads to a Monad with a List applying the supplied function in the process
|
<T> AnyM<T> |
unit(T value)
Construct a new instanceof AnyM using the type of the underlying wrapped monad
|
<R> R |
unwrap() |
getStreamable, isEmpty, iterator, reveresedSequenceM, reveresedStream, sequenceM
applyMCompletableFuture, applyMIterable, applyMOptional, applyMStream
flatMapBufferedReader, flatMapCharSequence, flatMapCollection, flatMapCompletableFuture, flatMapFile, flatMapOptional, flatMapSequenceM, flatMapStream, flatMapStreamable, flatMapURL
reduceMCompletableFuture, reduceMIterable, reduceMOptional, reduceMStream, reduceMStreamable
<R> R unwrap()
unwrap
in interface Unwrapable
<X> X monad()
AnyM<T> filter(java.util.function.Predicate<? super T> p)
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]]
p
- Filtering predicate<R> AnyM<R> map(java.util.function.Function<? super T,? extends R> fn)
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]]
fn
- AnyM<T> peek(java.util.function.Consumer<? super T> c)
AnyM.fromCompletableFuture(CompletableFuture.supplyAsync(()->loadData())
.peek(System.out::println)
c
- Consumer to accept current data<R> AnyM<R> bind(java.util.function.Function<? super T,?> fn)
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);
fn
- flatMap function<R> AnyM<R> liftAndBind(java.util.function.Function<? super T,?> fn)
fn
- flatMap function<T1> AnyM<T1> flatten()
AnyM<java.util.List<T>> aggregate(AnyM<T> next)
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)));
next
- Monad to aggregate content with<R1,R> AnyM<R> forEach2(java.util.function.Function<? super T,? extends AnyM<R1>> monad, java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends R>> yieldingFunction)
AnyM.fromArray(1,2,3)
.forEachAnyM2(a->AnyM.fromIntStream(IntStream.range(10,13)),
a->b->a+b);
//AnyM[11,14,12,15,13,16]
monad
- Nested Monad to iterate overyieldingFunction
- Function with pointers to the current element from both Streams that generates the new elements<R1,R> AnyM<R> forEach2(java.util.function.Function<? super T,? extends AnyM<R1>> monad, java.util.function.Function<? super T,java.util.function.Function<? super R1,java.lang.Boolean>> filterFunction, java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends R>> yieldingFunction)
AnyM.fromArray(1,2,3)
.forEach2(a->AnyM.fromIntStream(IntStream.range(10,13)),
a->b-> a<3 && b>10,
a->b->a+b);
//AnyM[14,15]
monad
- Nested Monad 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 monads that generates the new elements<R1,R2,R> AnyM<R> forEach3(java.util.function.Function<? super T,? extends AnyM<R1>> monad1, java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends AnyM<R2>>> monad2, java.util.function.Function<? super T,java.util.function.Function<? super R1,java.util.function.Function<? super R2,java.lang.Boolean>>> filterFunction, java.util.function.Function<? super T,java.util.function.Function<? super R1,java.util.function.Function<? super R2,? extends R>>> yieldingFunction)
AnyM.fromArray(1,2)
.forEach2(a->AnyM.fromIntStream(IntStream.range(10,13)),
(a->b->AnyM.fromArray(""+(a+b),"hello world"),
a->b->c->c+":"a+":"+b);
//AnyM[11:1:2,hello world:1:2,14:1:4,hello world:1:4,12:1:2,hello world:1:2,15:1:5,hello world:1:5]
monad1
- Nested monad to flatMap overstream2
- Nested monad to flatMap overyieldingFunction
- Function with pointers to the current element from both monads that generates the new elements<R1,R2,R> AnyM<R> forEach3(java.util.function.Function<? super T,? extends AnyM<R1>> monad1, java.util.function.Function<? super T,java.util.function.Function<? super R1,? extends AnyM<R2>>> monad2, java.util.function.Function<? super T,java.util.function.Function<? super R1,java.util.function.Function<? super R2,? extends R>>> yieldingFunction)
AnyM.fromArray(1,2,3)
.forEach3(a->AnyM.fromStream(IntStream.range(10,13)),
a->b->AnyM.fromArray(""+(a+b),"hello world"),
a->b->c-> c!=3,
a->b->c->c+":"a+":"+b);
//SequenceM[11:1:2,hello world:1:2,14:1:4,hello world:1:4,12:1:2,hello world:1:2,15:1:5,hello world:1:5]
monad1
- Nested Stream to iterate overmonad2
- Nested Stream 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 Monads that generates the new elements<R> AnyM<R> flatMap(java.util.function.Function<? super T,AnyM<? 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]]
fn
- flatMap function<NT> SequenceM<NT> toSequence(java.util.function.Function<T,java.util.stream.Stream<NT>> fn)
Optional<List<Integer>> into Stream<Integer>
List<Integer> list = anyM(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));
<T> SequenceM<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());
SequenceM<T> asSequence()
Optional<List<Integer>> into Stream<List<Integer>>
If the underlying monad is a Stream it is returned
Otherwise we flatMap the underlying monad to a Stream type<R> AnyM<R> applyM(AnyM<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
- <T> AnyM<T> unit(T value)
AnyM<Integer> ints = AnyM.fromList(Arrays.asList(1,2,3);
AnyM<String> string = ints.unit("hello");
value
- to embed inside the monad wrapped by AnyM<T> AnyM<T> empty()
Any<Integer> ints = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> empty=ints.empty();
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 replicateAnyM<T> reduceM(Monoid<AnyM<T>> reducer)
Monoid<Optional<Integer>> optionalAdd = Monoid.of(AnyM.fromOptional(Optional.of(0)), (a,b)-> AnyM.fromOptional(Optional.of(a.get()+b.get())));
AnyM.fromStream(Stream.of(2,8,3,1)).reduceM(optionalAdd);
//AnyM[Optional(14)];
reducer
- An identity value (approx. a seed) and BiFunction with a single type to reduce this anyMjava.lang.String toString()
toString
in class java.lang.Object
default java.util.Optional<java.util.List<T>> toOptional()
default java.util.concurrent.CompletableFuture<java.util.List<T>> toCompletableFuture()
java.util.List<T> toList()
java.util.Set<T> toSet()
<R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
static AnyM<java.lang.Integer> fromRange(int start, int end)
start
- Inclusive start of the rangeend
- Exclusive end of the rangestatic AnyM<java.lang.Long> fromRangeLong(long start, long end)
start
- Inclusive start of the rangeend
- Exclusive end of the rangestatic <T> AnyM<T> fromStreamable(ToStream<T> streamable)
streamable
- wrapstatic <T> AnyM<T> fromList(java.util.List<T> list)
list
- to wrap inside an AnyMstatic <T> AnyM<T> fromSet(java.util.Set<T> set)
list
- to wrap inside an AnyMstatic <T> AnyM<T> fromArray(T... streamData)
streamData
- values to populate a Streamstatic <T> AnyM<T> streamOf(T... streamData)
streamData
- values to populate a Streamstatic <T> AnyM<T> fromStream(java.util.stream.Stream<T> stream)
stream
- Stream to wrapstatic AnyM<java.lang.Integer> fromIntStream(java.util.stream.IntStream stream)
stream
- IntStream to wrapstatic AnyM<java.lang.Double> fromDoubleStream(java.util.stream.DoubleStream stream)
stream
- DoubleStream to wrapstatic AnyM<java.lang.Long> fromLongStream(java.util.stream.LongStream stream)
stream
- LongStream to wrapstatic <T> AnyM<T> fromOptional(java.util.Optional<T> optional)
stream
- Optional to wrapstatic AnyM<java.lang.Double> fromOptionalDouble(java.util.OptionalDouble optional)
stream
- Optional to wrapstatic AnyM<java.lang.Long> fromOptionalLong(java.util.OptionalLong optional)
stream
- OptionalLong to wrapstatic AnyM<java.lang.Integer> fromOptionalInt(java.util.OptionalInt optional)
stream
- OptionalInt to wrapstatic <T> AnyM<T> fromCompletableFuture(java.util.concurrent.CompletableFuture<T> future)
stream
- CompletableFuture to wrapstatic <T> AnyM<T> fromCollection(java.util.Collection<T> collection)
stream
- Collection to wrapstatic <T> AnyM<T> fromIterable(java.lang.Iterable<T> iterable)
stream
- Iterable to wrapstatic AnyM<java.lang.String> fromFile(java.io.File file)
stream
- File to generate text / line Stream from, and to wrapstatic AnyM<java.lang.String> fromURL(java.net.URL url)
stream
- URL to generate text / line Stream from, and to wrapstatic <T> AnyM<T> ofConvertable(java.lang.Object monad)
monad
- static <T> AnyM<T> ofMonad(java.lang.Object monad)
monad
- to wrapstatic <T> AnyM<T> ofNullable(java.lang.Object nullable)
nullable
- - Nullable object to generate an optional fromstatic <T> java.util.List<AnyM<T>> ofMonadList(java.lang.Iterable<java.lang.Object> anyM)
anyM
- Iterable containing Monadsstatic <T> java.util.List<AnyM<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 <T> java.util.List<AnyM<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> java.util.List<AnyM<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> java.util.List<AnyM<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 <T> java.util.List<AnyM<T>> listFromIterable(java.lang.Iterable<java.lang.Iterable<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 <T> java.util.List<AnyM<T>> listFromCollection(java.lang.Iterable<java.util.Collection<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromStreamable(Arrays.asList(Arrays.asList(1,2,3),Arrays.asList(10,20,30));
//List[AnyM[List[1,2,3],List[10,20,30]]]
anyM
- Iterable containing Collectionsstatic <T> java.util.List<AnyM<T>> listFromIterator(java.lang.Iterable<java.util.Iterator<T>> anyM)
List<AnyM<Integer>> anyMs = AnyM.listFromStreamable(Arrays.asList(Arrays.asList(1,2,3).iterator(),Arrays.asList(10,20,30)).iterator();
//List[AnyM[Stream[1,2,3],Stream[10,20,30]]]
anyM
- Iterable containing Iteratorsstatic <T,R> AnyM<java.util.List<R>> traverse(java.util.Collection<AnyM<T>> seq, java.util.function.Function<T,R> fn)
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 <T,R> AnyM<java.util.List<R>> traverse(java.util.stream.Stream<AnyM<T>> seq, java.util.function.Function<T,R> fn)
Stream<CompletableFuture<Integer>> futures = createFutures();
AnyM<List<String>> futureList = AnyMonads.traverse(AsAnyMList.anyMList(futures), (Integer i) -> "hello" +i);
seq
- Stream of Monadsfn
- Function to applystatic <T1> AnyM<java.util.stream.Stream<T1>> sequence(java.util.Collection<AnyM<T1>> seq)
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 convertfor helper methods to convert a List of Monads / Collections to List of AnyM
static <T1> AnyM<java.util.stream.Stream<T1>> sequence(java.util.stream.Stream<AnyM<T1>> seq)
Stream<CompletableFuture<Integer>> futures = createFutures();
AnyM<List<Integer>> futureList = AnyMonads.sequence(AsAnyMList.anyMList(futures));
//where AnyM wraps CompletableFuture<List<Integer>>
seq
- Stream of monads to convertfor helper methods to convert a List of Monads / Collections to List of AnyM
static <U,R> java.util.function.Function<AnyM<U>,AnyM<R>> liftM(java.util.function.Function<U,R> fn)
fn
- static <U1,U2,R> java.util.function.BiFunction<AnyM<U1>,AnyM<U2>,AnyM<R>> liftM2(java.util.function.BiFunction<U1,U2,R> fn)
BiFunction<AnyM<Integer>,AnyM<Integer>,AnyM<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.fn
- BiFunction to liftstatic <U1,U2,U3,R> org.jooq.lambda.function.Function3<AnyM<U1>,AnyM<U2>,AnyM<U3>,AnyM<R>> liftM3(org.jooq.lambda.function.Function3<U1,U2,U3,R> fn)
Function3 <AnyM<Double>,AnyM<Entity>,AnyM<String>,AnyM<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<AnyM<U1>,AnyM<U2>,AnyM<U3>,AnyM<U4>,AnyM<R>> liftM4(org.jooq.lambda.function.Function4<U1,U2,U3,U4,R> fn)
fn
- Quad funciton to liftstatic <U1,U2,U3,U4,U5,R> org.jooq.lambda.function.Function5<AnyM<U1>,AnyM<U2>,AnyM<U3>,AnyM<U4>,AnyM<U5>,AnyM<R>> liftM5(org.jooq.lambda.function.Function5<U1,U2,U3,U4,U5,R> fn)
fn
- Function to liftstatic <U1,U2,R> java.util.function.Function<AnyM<U1>,java.util.function.Function<AnyM<U2>,AnyM<R>>> liftM2(java.util.function.Function<U1,java.util.function.Function<U2,R>> fn)
fn
- Function to liftstatic <U1,U2,U3,R> java.util.function.Function<AnyM<U1>,java.util.function.Function<AnyM<U2>,java.util.function.Function<AnyM<U3>,AnyM<R>>>> liftM3(java.util.function.Function<U1,java.util.function.Function<U2,java.util.function.Function<U3,R>>> fn)
fn
- Function to liftstatic <U1,U2,U3,U4,R> java.util.function.Function<AnyM<U1>,java.util.function.Function<AnyM<U2>,java.util.function.Function<AnyM<U3>,java.util.function.Function<AnyM<U4>,AnyM<R>>>>> liftM4(java.util.function.Function<U1,java.util.function.Function<U2,java.util.function.Function<U3,java.util.function.Function<U4,R>>>> fn)
fn
- Function to liftstatic <U1,U2,U3,U4,U5,R> java.util.function.Function<AnyM<U1>,java.util.function.Function<AnyM<U2>,java.util.function.Function<AnyM<U3>,java.util.function.Function<AnyM<U4>,java.util.function.Function<AnyM<U5>,AnyM<R>>>>>> liftM5(java.util.function.Function<U1,java.util.function.Function<U2,java.util.function.Function<U3,java.util.function.Function<U4,java.util.function.Function<U5,R>>>>> fn)
(5 levels a->b->c->d->e->fn.apply(a,b,c,d,e) )
into Monadic formfn
- Function to lift