T
- MONAD
- public interface Monad<T> extends WrappingFunctor<T>, WrappingFilterable<T>
Modifier and Type | Method and Description |
---|---|
<T> AnyMSeq<T> |
anyMSeq() |
<T> AnyMValue<T> |
anyMValue() |
default <R> Monad<R> |
applyM(Monad<java.util.function.Function<? super T,? extends R>> fn)
Apply function/s inside supplied Monad to data in current Monad
e.g.
|
default <R> Monad<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
|
default Monad<T> |
cycle(int times)
Convert to a Stream with the values repeated specified times
|
default Monad<T> |
empty() |
default Monad<T> |
filter(java.util.function.Predicate<? super T> fn)
Keep only elements for which the supplied predicates hold
e.g.
|
default <R> Monad<R> |
flatMapToStream(java.util.function.Function<java.lang.Object,? extends java.util.stream.Stream<? extends R>> fn) |
default <T1> Monad<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
static <T> Monad<T> |
fromStream(java.util.stream.Stream<T> monad) |
default T |
get() |
default java.lang.Object |
getFilterable() |
default java.lang.Object |
getFunctor()
Override this method if you are using this class to wrap a Functor that does not
implement this interface natively.
|
default <R> Monad<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
|
default <R> Monad<R> |
map(java.util.function.Function<? super T,? extends R> fn)
Transform this functor using the supplied transformation function
|
static <T> Monad<T> |
of(java.lang.Object o) |
default Monad<T> |
peek(java.util.function.Consumer<? super T> c)
Peek at the current value of this Functor, without transforming it
|
default <R> Monad<R> |
reduceM(Monoid<R> reducer)
Perform a reduction where NT is a (native) Monad type
e.g.
|
default <R> Monad<R> |
replicateM(int times)
Replicate given Monad
|
<T> ReactiveSeq<T> |
sequence() |
default java.util.stream.Stream<T> |
stream()
Unwrap this Monad into a Stream.
|
default <NT> Monad<NT> |
streamedMonad()
Transform the contents of a Monad into a Monad wrapping a Stream e.g.
|
default <T> java.lang.Object |
unit(T value)
Generate a new instance of the underlying monad with given value
|
java.lang.Object |
unwrap() |
default WrappingFilterable<T> |
withFilterable(T filter) |
default <T> Monad<T> |
withFunctor(T functor)
Will attempt to create a new instance of this functor type via constructor reflection
if this is a wrapped Functor (i.e.
|
<T> Monad<T> |
withMonad(java.lang.Object invoke) |
cast, patternMatch, trampoline
filterNot, notNull, ofType
<T> Monad<T> withMonad(java.lang.Object invoke)
default <T> Monad<T> withFunctor(T functor)
WrappingFunctor
withFunctor
in interface WrappingFunctor<T>
default java.lang.Object getFunctor()
WrappingFunctor
getFunctor
in interface WrappingFunctor<T>
default <NT> Monad<NT> streamedMonad()
Optional<List<Integer>> into Stream<Integer>
List<List<Integer>> list = monad(Optional.of(Arrays.asList(1,2,3,4,5,6)))
.<Stream<Integer>,Integer>streamedMonad()
.grouped(3)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2,3));
assertThat(list.get(1),hasItems(4,5,6));
default java.util.stream.Stream<T> stream()
default Monad<T> cycle(int times)
times
- Times values should be repeated within a Streamdefault WrappingFilterable<T> withFilterable(T filter)
withFilterable
in interface WrappingFilterable<T>
default java.lang.Object getFilterable()
getFilterable
in interface WrappingFilterable<T>
default Monad<T> filter(java.util.function.Predicate<? super T> fn)
Filterable
of(1,2,3).filter(i->i>2);
//[3]
filter
in interface Filterable<T>
filter
in interface WrappingFilterable<T>
fn
- to filter elements by, retaining matchesdefault <R> Monad<R> map(java.util.function.Function<? super T,? extends R> fn)
Functor
of(1,2,3).map(i->i*2)
//[2,4,6]
default Monad<T> peek(java.util.function.Consumer<? super T> c)
Functor
of(1,2,3).map(System.out::println)
1
2
3
default <R> Monad<R> bind(java.util.function.Function<? super T,?> fn)
fn
- flatMap functiondefault <R> Monad<R> liftAndBind(java.util.function.Function<? super T,?> fn)
fn
- flatMap functiondefault <T1> Monad<T1> flatten()
default <R> Monad<R> flatMapToStream(java.util.function.Function<java.lang.Object,? extends java.util.stream.Stream<? extends R>> fn)
default <T> java.lang.Object unit(T value)
value
- to construct new instance withdefault T get()
java.lang.Object unwrap()
unwrap
in interface WrappingFunctor<T>
<T> AnyMValue<T> anyMValue()
<T> AnyMSeq<T> anyMSeq()
<T> ReactiveSeq<T> sequence()
static <T> Monad<T> of(java.lang.Object o)
static <T> Monad<T> fromStream(java.util.stream.Stream<T> monad)
default <R> Monad<R> applyM(Monad<java.util.function.Function<? super T,? extends R>> fn)
AnyM<Integer> applied = AsAnyM.anyM(Stream.of(1,2,3))
.applyM(AsAnyM.anyM(Streamable.of( (Integer a)->a+1 ,(Integer a) -> a*2)));
assertThat(applied.toSequence().toList(),equalTo(Arrays.asList(2, 2, 3, 4, 4, 6)));
with Optionals
AnyM<Integer> applied = AsAnyM.anyM(Optional.of(2))
.applyM( AsAnyM.anyM(Optional.of( (Integer a)->a+1)) );
assertThat(applied.toSequence().toList(),equalTo(Arrays.asList(3)));
fn
- default <R> Monad<R> replicateM(int times)
AnyM<Integer> applied =AsAnyM.anyM(Optional.of(2))
.replicateM(5);
assertThat(applied.unwrap(),equalTo(Optional.of(Arrays.asList(2,2,2,2,2))));
times
- number of times to replicatedefault <R> Monad<R> reduceM(Monoid<R> reducer)
Monoid<Optional<Integer>> optionalAdd = Monoid.of(Optional.of(0), (a,b)-> Optional.of(a.get()+b.get()));
assertThat(AsAnyM.anyM(Stream.of(2,8,3,1)).reduceM(optionalAdd).unwrap(),equalTo(Optional.of(14)));
reducer
-