T
- public interface AnyM<T> extends Unwrapable
companion class for static helper methods
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
|
<T> AnyM<T> |
empty() |
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
|
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) |
<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
|
<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) |
<X> X |
monad() |
AnyM<T> |
peek(java.util.function.Consumer<? super T> c) |
AnyM<T> |
reduceM(Monoid<AnyM<T>> reducer) |
AnyM<T> |
reduceMCompletableFuture(Monoid<java.util.concurrent.CompletableFuture<T>> reducer) |
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) |
AnyM<T> |
reduceMStreamable(Monoid<Streamable<T>> reducer) |
AnyM<java.util.List<T>> |
replicateM(int times)
Replicate given Monad
|
AnyM<AnyM<T>> |
simpleFilter(AnyM<java.util.function.Predicate<? super T>> fn)
Filter current monad by each element in supplied Monad
e.g.
|
AnyM<java.util.concurrent.CompletableFuture<T>> |
simpleFilter(java.util.concurrent.CompletableFuture<java.util.function.Predicate<? super T>> fn) |
AnyM<java.util.Optional<T>> |
simpleFilter(java.util.Optional<java.util.function.Predicate<? super T>> fn) |
AnyM<java.util.stream.Stream<T>> |
simpleFilter(java.util.stream.Stream<java.util.function.Predicate<? super T>> fn) |
AnyM<java.util.stream.Stream<T>> |
simpleFilter(Streamable<java.util.function.Predicate<? super T>> fn) |
default java.util.concurrent.CompletableFuture<java.util.List<T>> |
toCompletableFuture() |
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.lang.String |
toString() |
<T> AnyM<T> |
unit(T value) |
<R> R |
unwrap() |
<R> R unwrap()
unwrap
in interface Unwrapable
<X> X monad()
<R> AnyM<R> bind(java.util.function.Function<? super T,?> fn)
fn
- flatMap function<R> AnyM<R> liftAndBind(java.util.function.Function<? super T,?> fn)
fn
- flatMap functionAnyM<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')));
fn
- 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")));
fn
- 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")));
fn
- 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")));
fn
- <T1> AnyM<T1> flatten()
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 with<R> AnyM<R> flatMap(java.util.function.Function<? super T,AnyM<? extends R>> fn)
fn
- <R> AnyM<R> flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
fn
- <R> AnyM<R> flatMapStreamable(java.util.function.Function<? super T,Streamable<R>> fn)
fn
- <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
- <R> AnyM<R> flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
fn
- <R> AnyM<R> flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
<R> AnyM<R> flatMapSequenceM(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
<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(Stream.of(1,2,3)).applyM(anyM(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
Any<Integer> applied =anyM(Optional.of(2)).applyM(anyM(Optional.of( (Integer a)->a+1)) );
assertThat(applied.toList(),equalTo(Arrays.asList(3)));
fn
- AnyM<AnyM<T>> simpleFilter(AnyM<java.util.function.Predicate<? super T>> fn)
AnyM<AnyM<Integer>> applied = anyM(Stream.of(1,2,3))
.filterM(anyM(Streamable.of( (Integer a)->a>5 ,(Integer a) -> a<3)))
.simplex();
//results in AnyM((AnyM(1),AnyM(2),AnyM(())
//or in terms of the underlying monad as Stream.of(Stream.of(1),Stream.of(2),Stream.of(())
fn
- AnyM<java.util.stream.Stream<T>> simpleFilter(java.util.stream.Stream<java.util.function.Predicate<? super T>> fn)
AnyM<java.util.stream.Stream<T>> simpleFilter(Streamable<java.util.function.Predicate<? super T>> fn)
AnyM<java.util.Optional<T>> simpleFilter(java.util.Optional<java.util.function.Predicate<? super T>> fn)
AnyM<java.util.concurrent.CompletableFuture<T>> simpleFilter(java.util.concurrent.CompletableFuture<java.util.function.Predicate<? super T>> fn)
<T> AnyM<T> unit(T value)
<T> AnyM<T> empty()
AnyM<java.util.List<T>> replicateM(int times)
AnyM<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 replicateAnyM<T> reduceMOptional(Monoid<java.util.Optional<T>> 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
- AnyM<T> reduceMStreamable(Monoid<Streamable<T>> reducer)
AnyM<T> reduceMCompletableFuture(Monoid<java.util.concurrent.CompletableFuture<T>> reducer)
java.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()