T
- The type contained on the Maybe withinpublic class MaybeT<T>
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
MaybeT<T> |
filter(java.util.function.Predicate<? super T> test)
Filter the wrapped Maybe
|
<B> MaybeT<B> |
flatMap(java.util.function.Function<? super T,MaybeT<? extends B>> f)
Flat Map the wrapped Maybe
|
static <A> MaybeT<A> |
fromAnyM(AnyM<A> anyM)
|
static <U,R> java.util.function.Function<MaybeT<U>,MaybeT<R>> |
lift(java.util.function.Function<? super U,? extends R> fn)
Lift a function into one that accepts and returns an MaybeT This allows
multiple monad types to add functionality to existing functions and
methods
e.g.
|
static <U1,U2,R> java.util.function.BiFunction<MaybeT<U1>,MaybeT<U2>,MaybeT<R>> |
lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
Lift a BiFunction into one that accepts and returns MaybeTs This allows
multiple monad types to add functionality to existing functions and
methods
e.g.
|
<B> MaybeT<B> |
map(java.util.function.Function<? super T,? extends B> f)
Map the wrapped Maybe
|
static <A> MaybeT<A> |
of(AnyM<Maybe<A>> monads)
Construct an MaybeT from an AnyM that wraps a monad containing Maybes
|
MaybeT<T> |
peek(java.util.function.Consumer<? super T> peek)
Peek at the current value of the Maybe
|
java.lang.String |
toString() |
AnyM<Maybe<T>> |
unwrap() |
public MaybeT<T> peek(java.util.function.Consumer<? super T> peek)
MaybeT.of(AnyM.fromStream(Maybe.of(10))
.peek(System.out::println);
//prints 10
peek
- Consumer to accept current value of Maybepublic MaybeT<T> filter(java.util.function.Predicate<? super T> test)
MaybeT.of(AnyM.fromStream(Maybe.of(10))
.filter(t->t!=10);
//MaybeT<AnyM<Stream<Maybe.empty>>>
test
- Predicate to filter the wrapped Maybepublic <B> MaybeT<B> map(java.util.function.Function<? super T,? extends B> f)
MaybeT.of(AnyM.fromStream(Maybe.of(10))
.map(t->t=t+1);
//MaybeT<AnyM<Stream<Maybe[11]>>>
f
- Mapping function for the wrapped Maybepublic <B> MaybeT<B> flatMap(java.util.function.Function<? super T,MaybeT<? extends B>> f)
MaybeT.of(AnyM.fromStream(Maybe.of(10))
.flatMap(t->Maybe.empty();
//MaybeT<AnyM<Stream<Maybe.empty>>>
f
- FlatMap functionpublic static <U,R> java.util.function.Function<MaybeT<U>,MaybeT<R>> lift(java.util.function.Function<? super U,? extends R> fn)
{ @code Functionadd2 = i -> i + 2; Function , MaybeT > optTAdd2 = MaybeT.lift(add2); Stream withNulls = Stream.of(1, 2, null); AnyM stream = AnyM.ofMonad(withNulls); AnyM > streamOpt = stream.map(Maybe::ofNullable); List results = optTAdd2.apply(MaybeT.of(streamOpt)).unwrap(). >> unwrap() .filter(Maybe::isPresent).map(Maybe::get).collect(Collectors.toList()); // Arrays.asList(3,4); }
fn
- Function to enhance with functionality from Maybe and another
monad typepublic static <U1,U2,R> java.util.function.BiFunction<MaybeT<U1>,MaybeT<U2>,MaybeT<R>> lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
{ @code BiFunctionadd = (a, b) -> a + b; BiFunction , MaybeT , MaybeT > optTAdd2 = MaybeT.lift2(add); Stream withNulls = Stream.of(1, 2, null); AnyM stream = AnyM.ofMonad(withNulls); AnyM > streamOpt = stream.map(Maybe::ofNullable); CompletableFuture > two = CompletableFuture.supplyAsync(() -> Maybe.of(2)); AnyM > future = AnyM.ofMonad(two); List results = optTAdd2.apply(MaybeT.of(streamOpt), MaybeT.of(future)).unwrap() . >> unwrap().filter(Maybe::isPresent).map(Maybe::get) .collect(Collectors.toList()); // Arrays.asList(3,4); }
fn
- BiFunction to enhance with functionality from Maybe and
another monad typepublic static <A> MaybeT<A> fromAnyM(AnyM<A> anyM)
anyM
- AnyM that doesn't contain a monad wrapping an Maybepublic static <A> MaybeT<A> of(AnyM<Maybe<A>> monads)
monads
- AnyM that contains a monad wrapping an Maybepublic java.lang.String toString()
toString
in class java.lang.Object