T - public class AnyMImpl<T> extends java.lang.Object implements AnyM<T>
| Constructor and Description |
|---|
AnyMImpl() |
| 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<java.util.List<R>> |
aggregateUntyped(AnyM<?> next) |
<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.
|
<R> AnyM<R> |
applyMCompletableFuture(java.util.concurrent.CompletableFuture<java.util.function.Function<? super T,? extends R>> fn)
Apply function/s inside supplied Monad to data in current Monad
|
<R> AnyM<R> |
applyMOptional(java.util.Optional<java.util.function.Function<? super T,? extends R>> fn)
Apply function/s inside supplied Monad to data in current Monad
with Optionals
|
<R> AnyM<R> |
applyMStream(java.util.stream.Stream<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
|
<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> fn)
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<java.lang.String> |
flatMapBufferedReader(java.util.function.Function<? super T,java.io.BufferedReader> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied BufferedReaders
|
AnyM<java.lang.Character> |
flatMapCharSequence(java.util.function.Function<? super T,java.lang.CharSequence> fn)
Perform a flatMap operation where the result will be a flattened stream of Characters
from the CharSequence returned by the supplied function.
|
<R> AnyM<R> |
flatMapCollection(java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
flatMapping to a Stream will result in the Stream being converted to a List, if the host Monad
type is not a Stream (or Stream like type).
|
<R> AnyM<R> |
flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn) |
AnyM<java.lang.String> |
flatMapFile(java.util.function.Function<? super T,java.io.File> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied files.
|
<R> AnyM<R> |
flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Optional
|
<R> AnyM<R> |
flatMapSequenceM(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Stream.
|
<R> AnyM<R> |
flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
Convenience method to allow method reference support, when flatMap return type is a Stream
|
<R> AnyM<R> |
flatMapStreamable(java.util.function.Function<? super T,Streamable<R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Streamable
|
AnyM<java.lang.String> |
flatMapURL(java.util.function.Function<? super T,java.net.URL> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied URLs
|
<T1> AnyM<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
void |
forEach(java.util.function.Consumer<? super T> action) |
<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
|
java.util.Iterator<T> |
iterator() |
<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
|
<R> AnyM<R> |
map(java.util.function.Function<? super T,? extends R> fn)
Perform a map operation on the wrapped monad instance e.g.
|
Monad |
monad() |
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<T> |
reduceMCompletableFuture(Monoid<java.util.concurrent.CompletableFuture<T>> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
AnyM<T> |
reduceMIterable(Monoid<java.lang.Iterable<T>> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
AnyM<T> |
reduceMOptional(Monoid<java.util.Optional<T>> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
AnyM<T> |
reduceMStream(Monoid<java.util.stream.Stream<T>> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
AnyM<T> |
reduceMStreamable(Monoid<Streamable<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
|
SequenceM<T> |
stream() |
java.util.List<T> |
toList()
Convert this monad into a List
|
<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() |
<T> AnyM<T> |
unit(T value)
Construct a new instanceof AnyM using the type of the underlying wrapped monad
|
<R> R |
unwrap() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitfromArray, fromCollection, fromCompletableFuture, fromDoubleStream, fromFile, fromIntStream, fromIterable, fromList, fromLongStream, fromOptional, fromOptionalDouble, fromOptionalInt, fromOptionalLong, fromRange, fromRangeLong, fromSet, fromStream, fromStreamable, fromURL, liftM, liftM2, liftM2, liftM3, liftM3, liftM4, liftM4, liftM5, liftM5, listFromCollection, listFromCompletableFuture, listFromIterable, listFromIterator, listFromOptional, listFromStream, listFromStreamable, ofConvertable, ofMonad, ofMonadList, ofNullable, sequence, sequence, streamOf, toCompletableFuture, toOptional, traverse, traversegetStreamable, isEmpty, reveresedSequenceM, reveresedStream, sequenceMapplyMIterablepublic final <R> R unwrap()
unwrap in interface AnyM<T>unwrap in interface Unwrapablepublic final <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
public final AnyM<T> filter(java.util.function.Predicate<? super T> fn)
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]]
public final <R> AnyM<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]]
public final AnyM<T> peek(java.util.function.Consumer<? super T> c)
AnyM
AnyM.fromCompletableFuture(CompletableFuture.supplyAsync(()->loadData())
.peek(System.out::println)
public final <R> AnyM<R> bind(java.util.function.Function<? super T,?> fn)
public final <R> AnyM<R> liftAndBind(java.util.function.Function<? super T,?> fn)
liftAndBind in interface AnyM<T>fn - flatMap functionpublic final AnyM<java.lang.Character> flatMapCharSequence(java.util.function.Function<? super T,java.lang.CharSequence> fn)
List<Character> result = anyM("input.file")
.liftAndBindCharSequence(i->"hello world")
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList('h','e','l','l','o',' ','w','o','r','l','d')));
flatMapCharSequence in interface FlatMapM<T>fn - public final AnyM<java.lang.String> flatMapFile(java.util.function.Function<? super T,java.io.File> fn)
List<String> result = anyM("input.file")
.map(getClass().getClassLoader()::getResource)
.peek(System.out::println)
.map(URL::getFile)
.liftAndBindFile(File::new)
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
flatMapFile in interface FlatMapM<T>fn - public final AnyM<java.lang.String> flatMapURL(java.util.function.Function<? super T,java.net.URL> fn)
List<String> result = anyM("input.file")
.liftAndBindURL(getClass().getClassLoader()::getResource)
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
flatMapURL in interface FlatMapM<T>fn - public final AnyM<java.lang.String> flatMapBufferedReader(java.util.function.Function<? super T,java.io.BufferedReader> fn)
List<String> result = anyM("input.file")
.map(getClass().getClassLoader()::getResourceAsStream)
.map(InputStreamReader::new)
.liftAndBindBufferedReader(BufferedReader::new)
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
flatMapBufferedReader in interface FlatMapM<T>fn - public final <T1> AnyM<T1> flatten()
public final AnyM<java.util.List<T>> aggregate(AnyM<T> next)
List<Integer> result = anyM(Stream.of(1,2,3,4))
.aggregate(anyM(Optional.of(5)))
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList(1,2,3,4,5)));
public void forEach(java.util.function.Consumer<? super T> action)
forEach in interface java.lang.Iterable<T>public final <R> AnyM<R> flatMap(java.util.function.Function<? super T,AnyM<? extends R>> fn)
public final <R> AnyM<R> flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
flatMapStream in interface FlatMapM<T>fn - public final <R> AnyM<R> flatMapStreamable(java.util.function.Function<? super T,Streamable<R>> fn)
flatMapStreamable in interface FlatMapM<T>fn - public final <R> AnyM<R> flatMapCollection(java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
AnyM<Integer> opt = anyM(Optional.of(20));
Optional<List<Integer>> optionalList = opt.flatMap( i -> anyM(Stream.of(1,2,i))).unwrap();
//Optional [1,2,20]
In such cases using Arrays.asList would be more performant
AnyM<Integer> opt = anyM(Optional.of(20));
Optional<List<Integer>> optionalList = opt.flatMapCollection( i -> asList(1,2,i))).unwrap();
//Optional [1,2,20]
flatMapCollection in interface FlatMapM<T>fn - public final <R> AnyM<R> flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
flatMapOptional in interface FlatMapM<T>fn - public final <R> AnyM<R> flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
flatMapCompletableFuture in interface FlatMapM<T>public final <R> AnyM<R> flatMapSequenceM(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
FlatMapM
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i->SequenceM.of(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->SequenceM.of(i+1,i+2));
//AnyM[Optional[2]]
flatMapSequenceM in interface FlatMapM<T>fn - flatMap functionpublic final <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));
toSequence in interface AnyM<T>public final <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());
toSequence in interface AnyM<T>public final 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 typeasSequence in interface AnyM<T>public final <R> AnyM<R> applyM(AnyM<java.util.function.Function<? super T,? extends R>> fn)
AnyM
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)));
public <T> AnyM<T> unit(T value)
AnyM
AnyM<Integer> ints = AnyM.fromList(Arrays.asList(1,2,3);
AnyM<String> string = ints.unit("hello");
public <T> AnyM<T> empty()
AnyM
Any<Integer> ints = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> empty=ints.empty();
public final AnyM<java.util.List<T>> replicateM(int times)
AnyM
AnyM<Optional<Integer>> applied =AnyM.fromOptional(Optional.of(2)).replicateM(5);
//AnyM[Optional[List(2,2,2,2,2)]]
replicateM in interface AnyM<T>times - number of times to replicatepublic final AnyM<T> reduceMOptional(Monoid<java.util.Optional<T>> reducer)
ReduceM
Monoid<Optional<Integer>> optionalAdd = Monoid.of(Optional.of(0), (a,b)-> Optional.of(a.get()+b.get()));
AnyM.fromStream(Stream.of(2,8,3,1)).reduceM(optionalAdd);
//AnyM[Optional(14)];
reduceMOptional in interface ReduceM<T>reducer - An identity value (approx. a seed) and BiFunction with a single type to reduce this anyMpublic final AnyM<T> reduceMStream(Monoid<java.util.stream.Stream<T>> reducer)
ReduceM
Monoid<Optional<Integer>> streamableAdd = Monoid.of(Stream.of(0), (a,b)-> Stream.of(a.get()+b.get()));
AnyM.fromStream(Stream.of(2,8,3,1)).reduceM(streamableAdd);
//AnyM[Optional(14)];
reduceMStream in interface ReduceM<T>reducer - An identity value (approx. a seed) and BiFunction with a single type to reduce this anyMpublic final AnyM<T> reduceMStreamable(Monoid<Streamable<T>> reducer)
ReduceM
Monoid<Optional<Integer>> streamableAdd = Monoid.of(Streamable.of(0), (a,b)-> Streamable.of(a.get()+b.get()));
AnyM.fromStream(Stream.of(2,8,3,1)).reduceM(streamableAdd);
//AnyM[Optional(14)];
reduceMStreamable in interface ReduceM<T>reducer - An identity value (approx. a seed) and BiFunction with a single type to reduce this anyMpublic final AnyM<T> reduceMIterable(Monoid<java.lang.Iterable<T>> reducer)
ReduceM
Monoid<Optional<Integer>> streamableAdd = Monoid.of(Streamable.of(0), (a,b)-> Streamable.of(a.get()+b.get()));
AnyM.fromStream(Stream.of(2,8,3,1)).reduceM(streamableAdd);
//AnyM[Optional(14)];
reduceMIterable in interface ReduceM<T>reducer - An identity value (approx. a seed) and BiFunction with a single type to reduce this anyMpublic final AnyM<T> reduceMCompletableFuture(Monoid<java.util.concurrent.CompletableFuture<T>> reducer)
ReduceM
Monoid<Optional<Integer>> streamableAdd = Monoid.of(CompletableFuture.completedFuture(0), (a,b)-> CompletableFuture.supplyAsync(()->a.get()+b.get()));
AnyM.fromStream(Stream.of(2,8,3,1)).reduceM(streamableAdd);
//AnyM[Optional(14)];
reduceMCompletableFuture in interface ReduceM<T>reducer - An identity value (approx. a seed) and BiFunction with a single type to reduce this anyMpublic final AnyM<T> reduceM(Monoid<AnyM<T>> reducer)
AnyM
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)];
public java.lang.String toString()
public java.util.List<T> toList()
AnyMpublic java.util.Set<T> toSet()
AnyMpublic java.util.Iterator<T> iterator()
public <R> AnyM<R> applyMStream(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn)
ApplyM
AnyM<Integer> applied =AnyM.fromStream(Stream.of(1,2,3))
.applyM(Stream.of( (Integer a)->a+1 ,(Integer a) -> a*2));
assertThat(applied.toList(),equalTo(Arrays.asList(2, 2, 3, 4, 4, 6)));
applyMStream in interface ApplyM<T>fn - Stream of Functions to applypublic <R> AnyM<R> applyMOptional(java.util.Optional<java.util.function.Function<? super T,? extends R>> fn)
ApplyM
Any<Integer> applied =AnyM.fromOptional(Optional.of(2)).applyM(AnyM.fromOptional(Optional.of( (Integer a)->a+1)) );
assertThat(applied.toList(),equalTo(Arrays.asList(3)));
applyMOptional in interface ApplyM<T>public <R> AnyM<R> applyMCompletableFuture(java.util.concurrent.CompletableFuture<java.util.function.Function<? super T,? extends R>> fn)
ApplyM
Any<Integer> applied =AnyM.fromOptional(Optional.of(2))..applyM(AnyM.fromCompletableFuture(CompletableFuture.completedFuture( (Integer a)->a+1)) );
assertThat(applied.toList(),equalTo(Arrays.asList(3)));
applyMCompletableFuture in interface ApplyM<T>public <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
AnyM.fromArray(1,2,3)
.forEachAnyM2(a->AnyM.fromIntStream(IntStream.range(10,13)),
a->b->a+b);
//AnyM[11,14,12,15,13,16]
public <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
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]
forEach2 in interface AnyM<T>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 elementspublic <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
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]
forEach3 in interface AnyM<T>monad1 - Nested Stream to iterate overmonad2 - Nested Stream to iterate overyieldingFunction - Function with pointers to the current element from both Monads that generates the new elementspublic <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
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]