T
- component type of this monadpublic interface Monad<T> extends Functor<T>, Iterable<T>
A Monad
is a Functor
with a flatMap
method that satisfies the Monad laws, also known
as the three control laws:
Let
A
, B
, C
be typesunit: A -> Monad<A>
a constructorf: A -> Monad<B>
, g: B -> Monad<C>
functionsa
be an object of type A
m
be an object of type Monad<A>
Monad
interface should obey the three control laws:
unit(a).flatMap(f) ≡ f a
m.flatMap(unit) ≡ m
m.flatMap(f).flatMap(g) ≡ m.flatMap(x -> f.apply(x).flatMap(g))
Modifier and Type | Method and Description |
---|---|
Monad<T> |
filter(java.util.function.Predicate<? super T> predicate)
Filters this
Monad by testing a predicate. |
<U> Monad<U> |
flatMap(java.util.function.Function<? super T,? extends Iterable<? extends U>> mapper)
FlatMaps this value to a new value with different component type.
|
static <T1,T2,R> Function2<Monad<T1>,Monad<T2>,Monad<R>> |
lift(java.util.function.BiFunction<? super T1,? super T2,? extends R> f)
Lifts a
BiFunction to a higher Function2 that operates on Monads. |
static <T,R> Function1<? super Monad<T>,Monad<R>> |
lift(java.util.function.Function<? super T,? extends R> f)
Lifts a
Function to a higher Function1 that operates on Monads. |
static <T1,T2,T3,R> |
lift(Function3<? super T1,? super T2,? super T3,? extends R> f)
Lifts a
Function3 to a higher Function3 that operates on Monads. |
static <T1,T2,T3,T4,R> |
lift(Function4<? super T1,? super T2,? super T3,? super T4,? extends R> f)
Lifts a
Function4 to a higher Function4 that operates on Monads. |
static <T1,T2,T3,T4,T5,R> |
lift(Function5<? super T1,? super T2,? super T3,? super T4,? super T5,? extends R> f)
Lifts a
Function5 to a higher Function5 that operates on Monads. |
static <T1,T2,T3,T4,T5,T6,R> |
lift(Function6<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? extends R> f)
Lifts a
Function6 to a higher Function6 that operates on Monads. |
static <T1,T2,T3,T4,T5,T6,T7,R> |
lift(Function7<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? extends R> f)
Lifts a
Function7 to a higher Function7 that operates on Monads. |
static <T1,T2,T3,T4,T5,T6,T7,T8,R> |
lift(Function8<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? extends R> f)
Lifts a
Function8 to a higher Function8 that operates on Monads. |
<U> Monad<U> |
map(java.util.function.Function<? super T,? extends U> mapper)
Maps this value to a new value with different component type.
|
Array<T> |
toArray()
Converts this value to a
Array . |
CharSeq |
toCharSeq()
Converts this value to a
CharSeq . |
default Object[] |
toJavaArray()
Converts this value to an untyped Java array.
|
T[] |
toJavaArray(Class<T> componentType)
Converts this value to a typed Java array.
|
List<T> |
toJavaList()
Converts this value to an
List . |
<K,V> Map<K,V> |
toJavaMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Converts this value to a
Map . |
Optional<T> |
toJavaOptional()
Converts this value to an
Optional . |
Set<T> |
toJavaSet()
Converts this value to a
Set . |
java.util.stream.Stream<T> |
toJavaStream()
Converts this value to a
Stream . |
Lazy<T> |
toLazy()
Converts this value to a
Lazy . |
List<T> |
toList()
Converts this value to a
List . |
<K,V> Map<K,V> |
toMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> mapper)
Converts this value to a
Map . |
Option<T> |
toOption()
Converts this value to an
Option . |
Queue<T> |
toQueue()
Converts this value to a
Queue . |
Set<T> |
toSet()
Converts this value to a
Set . |
Stack<T> |
toStack()
Converts this value to a
Stack . |
Stream<T> |
toStream()
Converts this value to a
Stream . |
Tree<T> |
toTree()
Converts this value to a
Tree . |
Try<T> |
toTry()
Converts this value to a
Try . |
Try<T> |
toTry(java.util.function.Supplier<? extends Throwable> ifEmpty)
Converts this value to a
Try . |
Vector<T> |
toVector()
Converts this value to a
Vector . |
forEach, iterator, spliterator
static <T,R> Function1<? super Monad<T>,Monad<R>> lift(java.util.function.Function<? super T,? extends R> f)
Function
to a higher Function1
that operates on Monads.T
- 1st argument type of fR
- result type of ff
- a Functionstatic <T1,T2,R> Function2<Monad<T1>,Monad<T2>,Monad<R>> lift(java.util.function.BiFunction<? super T1,? super T2,? extends R> f)
BiFunction
to a higher Function2
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fR
- result type of ff
- a BiFunctionstatic <T1,T2,T3,R> Function3<Monad<T1>,Monad<T2>,Monad<T3>,Monad<R>> lift(Function3<? super T1,? super T2,? super T3,? extends R> f)
Function3
to a higher Function3
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fT3
- 3rd argument type of fR
- result type of ff
- a Function3static <T1,T2,T3,T4,R> Function4<Monad<T1>,Monad<T2>,Monad<T3>,Monad<T4>,Monad<R>> lift(Function4<? super T1,? super T2,? super T3,? super T4,? extends R> f)
Function4
to a higher Function4
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fT3
- 3rd argument type of fT4
- 4th argument type of fR
- result type of ff
- a Function4static <T1,T2,T3,T4,T5,R> Function5<Monad<T1>,Monad<T2>,Monad<T3>,Monad<T4>,Monad<T5>,Monad<R>> lift(Function5<? super T1,? super T2,? super T3,? super T4,? super T5,? extends R> f)
Function5
to a higher Function5
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fT3
- 3rd argument type of fT4
- 4th argument type of fT5
- 5th argument type of fR
- result type of ff
- a Function5static <T1,T2,T3,T4,T5,T6,R> Function6<Monad<T1>,Monad<T2>,Monad<T3>,Monad<T4>,Monad<T5>,Monad<T6>,Monad<R>> lift(Function6<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? extends R> f)
Function6
to a higher Function6
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fT3
- 3rd argument type of fT4
- 4th argument type of fT5
- 5th argument type of fT6
- 6th argument type of fR
- result type of ff
- a Function6static <T1,T2,T3,T4,T5,T6,T7,R> Function7<Monad<T1>,Monad<T2>,Monad<T3>,Monad<T4>,Monad<T5>,Monad<T6>,Monad<T7>,Monad<R>> lift(Function7<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? extends R> f)
Function7
to a higher Function7
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fT3
- 3rd argument type of fT4
- 4th argument type of fT5
- 5th argument type of fT6
- 6th argument type of fT7
- 7th argument type of fR
- result type of ff
- a Function7static <T1,T2,T3,T4,T5,T6,T7,T8,R> Function8<Monad<T1>,Monad<T2>,Monad<T3>,Monad<T4>,Monad<T5>,Monad<T6>,Monad<T7>,Monad<T8>,Monad<R>> lift(Function8<? super T1,? super T2,? super T3,? super T4,? super T5,? super T6,? super T7,? super T8,? extends R> f)
Function8
to a higher Function8
that operates on Monads.T1
- 1st argument type of fT2
- 2nd argument type of fT3
- 3rd argument type of fT4
- 4th argument type of fT5
- 5th argument type of fT6
- 6th argument type of fT7
- 7th argument type of fT8
- 8th argument type of fR
- result type of ff
- a Function8Monad<T> filter(java.util.function.Predicate<? super T> predicate)
Monad
by testing a predicate.
The semantics may vary from class to class, e.g. for single-valued type (like Option) and multi-values types (like Traversable). The commonality is, that filtered.isEmpty() will return true, if no element satisfied the given predicate.
Also, an implementation may throw NoSuchElementException
, if no element makes it through the filter
and this state cannot be reflected. E.g. this is the case for Either.LeftProjection
and
Either.RightProjection
.
predicate
- A predicateNullPointerException
- if predicate
is null<U> Monad<U> flatMap(java.util.function.Function<? super T,? extends Iterable<? extends U>> mapper)
FlatMap is the sequence operation for functions and behaves like the imperative ;
.
If the previous results are needed, flatMap cascades:
m1().flatMap(result1 ->
m2(result1).flatMap(result2 ->
m3(result1, result2).flatMap(result3 ->
...
)
)
);
If only the last result is needed, flatMap may be used sequentially:
m1().flatMap(this::m2)
.flatMap(this::m3)
.flatMap(...);
U
- Component type of the mapped Monad
mapper
- A mapperMonad
NullPointerException
- if mapper
is null<U> Monad<U> map(java.util.function.Function<? super T,? extends U> mapper)
map
in interface Functor<T>
U
- Component type of the mapped Monad
mapper
- A mapperMonad
NullPointerException
- if mapper
is nulldefault Object[] toJavaArray()
T[] toJavaArray(Class<T> componentType)
componentType
- Component type of the arrayNullPointerException
- if componentType is null<K,V> Map<K,V> toJavaMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> f)
Map
.K
- The key typeV
- The value typef
- A function that maps an element to a key/value pair represented by Tuple2HashMap
.Optional<T> toJavaOptional()
Optional
.Optional
.java.util.stream.Stream<T> toJavaStream()
Stream
.Stream
.<K,V> Map<K,V> toMap(java.util.function.Function<? super T,? extends Tuple2<? extends K,? extends V>> mapper)
Map
.K
- The key typeV
- The value typemapper
- A function that maps an element to a key/value pair represented by Tuple2HashMap
.Try<T> toTry()
Try
.
If this value is undefined, i.e. empty, then a new Failure(NoSuchElementException)
is returned,
otherwise a new Success(value)
is returned.
Try
.Try<T> toTry(java.util.function.Supplier<? extends Throwable> ifEmpty)
Try
.
If this value is undefined, i.e. empty, then a new Failure(ifEmpty.get())
is returned,
otherwise a new Success(value)
is returned.
ifEmpty
- an exception supplierTry
.Copyright © 2015. All Rights Reserved.