T - public class AnyMImpl<T>
extends java.lang.Object
implements com.aol.cyclops.monad.AnyM<T>
| Constructor and Description |
|---|
AnyMImpl() |
| Modifier and Type | Method and Description |
|---|---|
com.aol.cyclops.monad.AnyM<T> |
aggregate(com.aol.cyclops.monad.AnyM<T> next)
Aggregate the contents of this Monad and the supplied Monad
|
<R> com.aol.cyclops.monad.AnyM<java.util.List<R>> |
aggregateUntyped(com.aol.cyclops.monad.AnyM<?> next) |
<R> com.aol.cyclops.monad.AnyM<R> |
applyM(com.aol.cyclops.monad.AnyM<java.util.function.Function<? super T,? extends R>> fn)
Apply function/s inside supplied Monad to data in current Monad
e.g.
|
com.aol.cyclops.sequence.SequenceM<T> |
asSequence()
Wrap this Monad's contents as a Sequence without disaggreating it.
|
<R> com.aol.cyclops.monad.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> com.aol.cyclops.monad.AnyM<T> |
empty() |
com.aol.cyclops.monad.AnyM<T> |
filter(java.util.function.Predicate<? super T> fn) |
<R> com.aol.cyclops.monad.AnyM<R> |
flatMap(java.util.function.Function<? super T,com.aol.cyclops.monad.AnyM<? extends R>> fn)
flatMap operation
|
com.aol.cyclops.monad.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
|
com.aol.cyclops.monad.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> com.aol.cyclops.monad.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> com.aol.cyclops.monad.AnyM<R> |
flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn) |
com.aol.cyclops.monad.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> com.aol.cyclops.monad.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> com.aol.cyclops.monad.AnyM<R> |
flatMapSequenceM(java.util.function.Function<? super T,com.aol.cyclops.sequence.SequenceM<? extends R>> fn) |
<R> com.aol.cyclops.monad.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> com.aol.cyclops.monad.AnyM<R> |
flatMapStreamable(java.util.function.Function<? super T,com.aol.cyclops.sequence.streamable.Streamable<R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Streamable
|
com.aol.cyclops.monad.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> com.aol.cyclops.monad.AnyM<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
void |
forEach(java.util.function.Consumer<? super T> action)
Deprecated.
|
<R> com.aol.cyclops.monad.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> com.aol.cyclops.monad.AnyM<R> |
map(java.util.function.Function<? super T,? extends R> fn) |
Monad |
monad() |
com.aol.cyclops.monad.AnyM<T> |
peek(java.util.function.Consumer<? super T> c) |
com.aol.cyclops.monad.AnyM<T> |
reduceM(com.aol.cyclops.sequence.Monoid<com.aol.cyclops.monad.AnyM<T>> reducer) |
com.aol.cyclops.monad.AnyM<T> |
reduceMCompletableFuture(com.aol.cyclops.sequence.Monoid<java.util.concurrent.CompletableFuture<T>> reducer) |
com.aol.cyclops.monad.AnyM<T> |
reduceMOptional(com.aol.cyclops.sequence.Monoid<java.util.Optional<T>> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
com.aol.cyclops.monad.AnyM<T> |
reduceMStream(com.aol.cyclops.sequence.Monoid<java.util.stream.Stream<T>> reducer) |
com.aol.cyclops.monad.AnyM<T> |
reduceMStreamable(com.aol.cyclops.sequence.Monoid<com.aol.cyclops.sequence.streamable.Streamable<T>> reducer) |
com.aol.cyclops.monad.AnyM<java.util.List<T>> |
replicateM(int times)
Replicate given Monad
|
com.aol.cyclops.monad.AnyM<com.aol.cyclops.monad.AnyM<T>> |
simpleFilter(com.aol.cyclops.monad.AnyM<java.util.function.Predicate<? super T>> fn)
Filter current monad by each element in supplied Monad
e.g.
|
com.aol.cyclops.monad.AnyM<java.util.concurrent.CompletableFuture<T>> |
simpleFilter(java.util.concurrent.CompletableFuture<java.util.function.Predicate<? super T>> fn) |
com.aol.cyclops.monad.AnyM<java.util.Optional<T>> |
simpleFilter(java.util.Optional<java.util.function.Predicate<? super T>> fn) |
com.aol.cyclops.monad.AnyM<java.util.stream.Stream<T>> |
simpleFilter(java.util.stream.Stream<java.util.function.Predicate<? super T>> fn) |
com.aol.cyclops.monad.AnyM<java.util.stream.Stream<T>> |
simpleFilter(com.aol.cyclops.sequence.streamable.Streamable<java.util.function.Predicate<? super T>> fn) |
java.util.List<T> |
toList() |
<T> com.aol.cyclops.sequence.SequenceM<T> |
toSequence()
Optional<List<Integer>> into Stream<Integer> |
<NT> com.aol.cyclops.sequence.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() |
java.lang.String |
toString() |
<T> com.aol.cyclops.monad.AnyM<T> |
unit(T value) |
<R> R |
unwrap() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitfromArray, fromCollection, fromCompletableFuture, fromDoubleStream, fromFile, fromIntStream, fromIterable, fromList, fromLongStream, fromOptional, fromOptional, fromOptional, fromOptional, fromRange, fromSet, fromStream, fromStreamable, fromURL, ofConvertable, ofMonad, ofNullable, toCompletableFuture, toOptionalpublic final <R> R unwrap()
unwrap in interface com.aol.cyclops.monad.AnyM<T>unwrap in interface com.aol.cyclops.sequence.Unwrapablepublic final <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
collect in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<T> filter(java.util.function.Predicate<? super T> fn)
filter in interface com.aol.cyclops.monad.AnyM<T>public final <R> com.aol.cyclops.monad.AnyM<R> map(java.util.function.Function<? super T,? extends R> fn)
map in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<T> peek(java.util.function.Consumer<? super T> c)
peek in interface com.aol.cyclops.monad.AnyM<T>public final <R> com.aol.cyclops.monad.AnyM<R> bind(java.util.function.Function<? super T,?> fn)
bind in interface com.aol.cyclops.monad.AnyM<T>fn - flatMap functionpublic final <R> com.aol.cyclops.monad.AnyM<R> liftAndBind(java.util.function.Function<? super T,?> fn)
liftAndBind in interface com.aol.cyclops.monad.AnyM<T>fn - flatMap functionpublic final com.aol.cyclops.monad.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 com.aol.cyclops.monad.AnyM<T>fn - public final com.aol.cyclops.monad.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 com.aol.cyclops.monad.AnyM<T>fn - public final com.aol.cyclops.monad.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 com.aol.cyclops.monad.AnyM<T>fn - public final com.aol.cyclops.monad.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 com.aol.cyclops.monad.AnyM<T>fn - public final <T1> com.aol.cyclops.monad.AnyM<T1> flatten()
flatten in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<T> aggregate(com.aol.cyclops.monad.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)));
aggregate in interface com.aol.cyclops.monad.AnyM<T>next - Monad to aggregate content withpublic final <R> com.aol.cyclops.monad.AnyM<java.util.List<R>> aggregateUntyped(com.aol.cyclops.monad.AnyM<?> next)
aggregateUntyped in interface com.aol.cyclops.monad.AnyM<T>@Deprecated public void forEach(java.util.function.Consumer<? super T> action)
public final <R> com.aol.cyclops.monad.AnyM<R> flatMap(java.util.function.Function<? super T,com.aol.cyclops.monad.AnyM<? extends R>> fn)
flatMap in interface com.aol.cyclops.monad.AnyM<T>fn - public final <R> com.aol.cyclops.monad.AnyM<R> flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
flatMapStream in interface com.aol.cyclops.monad.AnyM<T>fn - public final <R> com.aol.cyclops.monad.AnyM<R> flatMapStreamable(java.util.function.Function<? super T,com.aol.cyclops.sequence.streamable.Streamable<R>> fn)
flatMapStreamable in interface com.aol.cyclops.monad.AnyM<T>fn - public final <R> com.aol.cyclops.monad.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 com.aol.cyclops.monad.AnyM<T>fn - public final <R> com.aol.cyclops.monad.AnyM<R> flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
flatMapOptional in interface com.aol.cyclops.monad.AnyM<T>fn - public final <R> com.aol.cyclops.monad.AnyM<R> flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
flatMapCompletableFuture in interface com.aol.cyclops.monad.AnyM<T>public final <R> com.aol.cyclops.monad.AnyM<R> flatMapSequenceM(java.util.function.Function<? super T,com.aol.cyclops.sequence.SequenceM<? extends R>> fn)
flatMapSequenceM in interface com.aol.cyclops.monad.AnyM<T>public final <NT> com.aol.cyclops.sequence.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 com.aol.cyclops.monad.AnyM<T>public final <T> com.aol.cyclops.sequence.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 com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.sequence.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 com.aol.cyclops.monad.AnyM<T>public final <R> com.aol.cyclops.monad.AnyM<R> applyM(com.aol.cyclops.monad.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)));applyM in interface com.aol.cyclops.monad.AnyM<T>fn - public final com.aol.cyclops.monad.AnyM<com.aol.cyclops.monad.AnyM<T>> simpleFilter(com.aol.cyclops.monad.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(())
simpleFilter in interface com.aol.cyclops.monad.AnyM<T>fn - public final com.aol.cyclops.monad.AnyM<java.util.stream.Stream<T>> simpleFilter(java.util.stream.Stream<java.util.function.Predicate<? super T>> fn)
simpleFilter in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<java.util.stream.Stream<T>> simpleFilter(com.aol.cyclops.sequence.streamable.Streamable<java.util.function.Predicate<? super T>> fn)
simpleFilter in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<java.util.Optional<T>> simpleFilter(java.util.Optional<java.util.function.Predicate<? super T>> fn)
simpleFilter in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<java.util.concurrent.CompletableFuture<T>> simpleFilter(java.util.concurrent.CompletableFuture<java.util.function.Predicate<? super T>> fn)
simpleFilter in interface com.aol.cyclops.monad.AnyM<T>public <T> com.aol.cyclops.monad.AnyM<T> unit(T value)
unit in interface com.aol.cyclops.monad.AnyM<T>public <T> com.aol.cyclops.monad.AnyM<T> empty()
empty in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.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))));
replicateM in interface com.aol.cyclops.monad.AnyM<T>times - number of times to replicatepublic final com.aol.cyclops.monad.AnyM<T> reduceMOptional(com.aol.cyclops.sequence.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)));
reduceMOptional in interface com.aol.cyclops.monad.AnyM<T>reducer - public final com.aol.cyclops.monad.AnyM<T> reduceMStream(com.aol.cyclops.sequence.Monoid<java.util.stream.Stream<T>> reducer)
reduceMStream in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<T> reduceMStreamable(com.aol.cyclops.sequence.Monoid<com.aol.cyclops.sequence.streamable.Streamable<T>> reducer)
reduceMStreamable in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<T> reduceMCompletableFuture(com.aol.cyclops.sequence.Monoid<java.util.concurrent.CompletableFuture<T>> reducer)
reduceMCompletableFuture in interface com.aol.cyclops.monad.AnyM<T>public final com.aol.cyclops.monad.AnyM<T> reduceM(com.aol.cyclops.sequence.Monoid<com.aol.cyclops.monad.AnyM<T>> reducer)
reduceM in interface com.aol.cyclops.monad.AnyM<T>public java.lang.String toString()
toString in interface com.aol.cyclops.monad.AnyM<T>toString in class java.lang.Objectpublic java.util.List<T> toList()
toList in interface com.aol.cyclops.monad.AnyM<T>