T - public class AnyM<T> extends java.lang.Object implements Unwrapable
| Constructor and Description |
|---|
AnyM() |
| Modifier and Type | Method and Description |
|---|---|
AnyM<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.
|
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
|
AnyM<T> |
filter(java.util.function.Predicate<? super T> fn) |
<R> AnyM<R> |
flatMap(java.util.function.Function<? super T,AnyM<? extends R>> fn)
flatMap operation
|
<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) |
<R> AnyM<R> |
flatMapLazySeq(java.util.function.Function<? super T,com.nurkiewicz.lazyseq.LazySeq<? extends R>> fn) |
<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) |
<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
|
<T1> AnyM<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
void |
forEach(java.util.function.Consumer<? super T> action) |
<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
|
AnyM<java.lang.String> |
liftAndBindBufferedReader(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> |
liftAndBindCharSequence(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.
|
AnyM<java.lang.String> |
liftAndBindFile(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.
|
AnyM<java.lang.String> |
liftAndBindURL(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
|
<R> AnyM<R> |
map(java.util.function.Function<? super T,? extends R> fn) |
<MONAD> Monad<MONAD,T> |
monad() |
AnyM<T> |
peek(java.util.function.Consumer<? super T> c) |
<R> AnyM<R> |
reduceM(Monoid<R> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
<R> AnyM<R> |
replicateM(int times)
Replicate given Monad
|
<R> AnyM<R> |
simpleFilter(AnyM<java.util.function.Predicate<? super T>> fn)
Filter current monad by each element in supplied Monad
e.g.
|
<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.lang.String |
toString() |
<R> R |
unwrap() |
public final <R> R unwrap()
unwrap in interface Unwrapablepublic final <R> AnyM<R> bind(java.util.function.Function<? super T,?> fn)
fn - flatMap functionpublic final <R> AnyM<R> liftAndBind(java.util.function.Function<? super T,?> fn)
fn - flatMap functionpublic final AnyM<java.lang.Character> liftAndBindCharSequence(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')));
fn - public final AnyM<java.lang.String> liftAndBindFile(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")));
fn - public final AnyM<java.lang.String> liftAndBindURL(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")));
fn - public final AnyM<java.lang.String> liftAndBindBufferedReader(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")));
fn - public final <T1> AnyM<T1> flatten()
public final AnyM<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)));
next - Monad to aggregate content withpublic void forEach(java.util.function.Consumer<? super T> action)
public final <R> AnyM<R> flatMap(java.util.function.Function<? super T,AnyM<? extends R>> fn)
fn - public final <R> AnyM<R> flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
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]
fn - public final <R> AnyM<R> flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
fn - public final <R> AnyM<R> flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
public final <R> AnyM<R> flatMapLazySeq(java.util.function.Function<? super T,com.nurkiewicz.lazyseq.LazySeq<? extends R>> fn)
public final <R> AnyM<R> flatMapSequenceM(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
public 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));
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());
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 typepublic final <R> AnyM<R> applyM(AnyM<java.util.function.Function<? super T,? extends R>> fn)
Simplex<Integer> applied =monad(Stream.of(1,2,3)).applyM(monad(Streamable.of( (Integer a)->a+1 ,(Integer a) -> a*2))).simplex();
assertThat(applied.toList(),equalTo(Arrays.asList(2, 2, 3, 4, 4, 6)));
with Optionals
Simplex<Integer> applied =monad(Optional.of(2)).applyM(monad(Optional.of( (Integer a)->a+1)) ).simplex();
assertThat(applied.toList(),equalTo(Arrays.asList(3)));fn - public final <R> AnyM<R> simpleFilter(AnyM<java.util.function.Predicate<? super T>> fn)
Simplex<Stream<Integer>> applied = monad(Stream.of(1,2,3))
.filterM(monad(Streamable.of( (Integer a)->a>5 ,(Integer a) -> a<3)))
.simplex();
//results in Stream.of(Stream.of(1),Stream.of(2),Stream.of(())
fn - public final <R> AnyM<R> replicateM(int times)
Simplex<Optional<Integer>> applied =monad(Optional.of(2)).replicateM(5).simplex();
assertThat(applied.unwrap(),equalTo(Optional.of(Arrays.asList(2,2,2,2,2))));
times - number of times to replicatepublic final <R> AnyM<R> reduceM(Monoid<R> reducer)
Monoid<Optional<Integer>> optionalAdd = Monoid.of(Optional.of(0), (a,b)-> Optional.of(a.get()+b.get()));
assertThat(monad(Stream.of(2,8,3,1)).reduceM(optionalAdd).unwrap(),equalTo(Optional.of(14)));
reducer - public java.lang.String toString()
toString in class java.lang.Object