T
- The type contained on the nested Try withinpublic interface TryT<T,X extends java.lang.Throwable> extends org.reactivestreams.Publisher<T>, Functor<T>, Filterable<T>
Modifier and Type | Method and Description |
---|---|
default <B> TryT<B,X> |
bind(java.util.function.Function<? super T,TryT<B,X>> f)
Flat Map the wrapped Try
|
default <U> TryT<U,X> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException . |
<R> TryT<R,X> |
empty() |
static <T,X extends java.lang.Throwable> |
emptyList() |
static <A,X extends java.lang.Throwable> |
emptyOptional() |
MaybeT<T> |
filter(java.util.function.Predicate<? super T> test)
Filter the wrapped Try
|
default MaybeT<T> |
filterNot(java.util.function.Predicate<? super T> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
<B> TryT<B,X> |
flatMap(java.util.function.Function<? super T,? extends Try<B,X>> f) |
static <A,X extends java.lang.Throwable> |
fromAnyM(AnyM<A> anyM)
|
static <A,X extends java.lang.Throwable> |
fromAnyMSeq(AnyMSeq<A> anyM) |
static <A,X extends java.lang.Throwable> |
fromAnyMValue(AnyMValue<A> anyM) |
static <A,X extends java.lang.Throwable> |
fromFuture(java.util.concurrent.CompletableFuture<Try<A,X>> future) |
static <A,X extends java.lang.Throwable> |
fromIterable(java.lang.Iterable<Try<A,X>> iterableOfTrys) |
static <A,X extends java.lang.Throwable> |
fromIterablTryue(java.lang.Iterable<Try<A,X>> iterableOfTrys) |
static <A,X extends java.lang.Throwable> |
fromOptional(java.util.Optional<Try<A,X>> optional) |
static <A,X extends java.lang.Throwable> |
fromPublisher(org.reactivestreams.Publisher<Try<A,X>> publisherOfTrys) |
static <A,X extends java.lang.Throwable> |
fromStream(java.util.stream.Stream<Try<A,X>> streamOfTrys) |
static <A,X extends java.lang.Throwable,V extends MonadicValue<Try<A,X>>> |
fromValue(V monadicValue) |
static <U,R,X extends java.lang.Throwable> |
lift(java.util.function.Function<? super U,? extends R> fn)
Lift a function into one that accepts and returns an TryT
This allows multiple monad types to add functionality to existing functions and methods
e.g.
|
static <U1,U2,R,X extends java.lang.Throwable> |
lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
Lift a BiFunction into one that accepts and returns TryTs
This allows multiple monad types to add functionality to existing functions and methods
e.g.
|
<B> TryT<B,X> |
map(java.util.function.Function<? super T,? extends B> f)
Map the wrapped Try
|
default MaybeT<T> |
notNull()
Filter elements retaining only values which are not null
|
static <A,X extends java.lang.Throwable> |
of(AnyM<Try<A,X>> monads)
Construct an TryT from an AnyM that wraps a monad containing Trys
|
default <U> MaybeT<U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a stream that are of a given type.
|
default <R> TryT<R,X> |
patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1,
java.util.function.Supplier<? extends R> otherwise)
Transform the elements of this Stream with a Pattern Matching case and default value
|
TryT<T,X> |
peek(java.util.function.Consumer<? super T> peek)
Peek at the current value of the Try
|
default <R> TryT<R,X> |
trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Performs a map operation that can call a recursive method without running out of stack space
|
<R> TryT<R,X> |
unit(R value) |
AnyM<Try<T,X>> |
unwrap() |
TryT<T,X> peek(java.util.function.Consumer<? super T> peek)
TryT.of(AnyM.fromStream(Try.success(10))
.peek(System.out::println);
//prints 10
MaybeT<T> filter(java.util.function.Predicate<? super T> test)
TryT.of(AnyM.fromStream(Try.success(10))
.filter(t->t!=10);
//TryT<AnyM<Stream<Optional.empty>>>
filter
in interface Filterable<T>
test
- Predicate to filter the wrapped Try<B> TryT<B,X> map(java.util.function.Function<? super T,? extends B> f)
TryT.of(AnyM.fromStream(Try.success(10))
.map(t->t=t+1);
//TryT<AnyM<Stream<Success[11]>>>
default <B> TryT<B,X> bind(java.util.function.Function<? super T,TryT<B,X>> f)
TryT.of(AnyM.fromStream(Try.success(10))
.flatMap(t->Try.failure(new Exception());
//TryT<AnyM<Stream<Failure[Excption]>>>
f
- FlatMap functionstatic <U,R,X extends java.lang.Throwable> java.util.function.Function<TryT<U,X>,TryT<R,X>> lift(java.util.function.Function<? super U,? extends R> fn)
Function<Integer,Integer> add2 = i -> i+2;
Function<TryT<Integer,RuntimeException>, TryT<Integer,RuntimeException>> optTAdd2 = TryT.lift(add2);
Stream<Integer> withNulls = Stream.of(1,2,null);
AnyM<Integer> stream = AnyM.ofMonad(withNulls);
AnyM<Try<Integer,RuntimeException>> streamOpt = stream.map(this::toTry);
List<Integer> results = optTAdd2.apply(TryT.of(streamOpt))
.unwrap()
.<Stream<Try<Integer,RuntimeException>>>unwrap()
.filter(Try::isSuccess)
.map(Try::get)
.collect(Collectors.toList());
//Arrays.asList(3,4);
fn
- Function to enhance with functionality from Try and another monad typestatic <U1,U2,R,X extends java.lang.Throwable> java.util.function.BiFunction<TryT<U1,X>,TryT<U2,X>,TryT<R,X>> lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
BiFunction<Integer,Integer,Integer> add = (a,b) -> a+b;
BiFunction<TryT<Integer,RuntimeException>,TryT<Integer,RuntimeException>, TryT<Integer,RuntimeException>> optTAdd2 = TryT.lift2(add);
Stream<Integer> withNulls = Stream.of(1,2,null);
AnyM<Integer> stream = AnyM.ofMonad(withNulls);
AnyM<Try<Integer,RuntimeException>> streamOpt = stream.map(this::toTry);
CompletableFuture<Try<Integer,RuntimeException>> two = CompletableFuture.completedFuture(Try.of(2));
AnyM<Try<Integer,RuntimeException>> future= AnyM.ofMonad(two);
List<Integer> results = optTAdd2.apply(TryT.of(streamOpt),TryT.of(future))
.unwrap()
.<Stream<Try<Integer,RuntimeException>>>unwrap()
.filter(Try::isSuccess)
.map(Try::get)
.collect(Collectors.toList());
//Arrays.asList(3,4);
fn
- BiFunction to enhance with functionality from Try and another monad typestatic <A,X extends java.lang.Throwable> TryT<A,X> fromAnyM(AnyM<A> anyM)
anyM
- AnyM that doesn't contain a monad wrapping an Trystatic <A,X extends java.lang.Throwable> TryT<A,X> of(AnyM<Try<A,X>> monads)
monads
- AnyM that contains a monad wrapping an Trystatic <A,X extends java.lang.Throwable> TryTValue<A,X> fromAnyMValue(AnyMValue<A> anyM)
static <A,X extends java.lang.Throwable> TryTSeq<A,X> fromIterable(java.lang.Iterable<Try<A,X>> iterableOfTrys)
static <A,X extends java.lang.Throwable> TryTSeq<A,X> fromStream(java.util.stream.Stream<Try<A,X>> streamOfTrys)
static <A,X extends java.lang.Throwable> TryTSeq<A,X> fromPublisher(org.reactivestreams.Publisher<Try<A,X>> publisherOfTrys)
static <A,X extends java.lang.Throwable,V extends MonadicValue<Try<A,X>>> TryTValue<A,X> fromValue(V monadicValue)
static <A,X extends java.lang.Throwable> TryTValue<A,X> fromOptional(java.util.Optional<Try<A,X>> optional)
static <A,X extends java.lang.Throwable> TryTValue<A,X> fromFuture(java.util.concurrent.CompletableFuture<Try<A,X>> future)
static <A,X extends java.lang.Throwable> TryTValue<A,X> fromIterablTryue(java.lang.Iterable<Try<A,X>> iterableOfTrys)
static <T,X extends java.lang.Throwable> TryTSeq<T,X> emptyList()
static <A,X extends java.lang.Throwable> TryTValue<A,X> emptyOptional()
default <U> TryT<U,X> cast(java.lang.Class<? extends U> type)
Functor
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)default <R> TryT<R,X> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Functor
ReactiveSeq.of(10,20,30,40)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
Trampoline<Long> fibonacci(int i){
return fibonacci(i,1,0);
}
Trampoline<Long> fibonacci(int n, long a, long b) {
return n == 0 ? Trampoline.done(b) : Trampoline.more( ()->fibonacci(n-1, a+b, a));
}
55
6765
832040
102334155
ReactiveSeq.of(10_000,200_000,3_000_000,40_000_000)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
completes successfully
trampoline
in interface Functor<T>
mapper
- TCO Transformation functiondefault <R> TryT<R,X> patternMatch(java.util.function.Function<Matchable.CheckValue1<T,R>,Matchable.CheckValue1<T,R>> case1, java.util.function.Supplier<? extends R> otherwise)
Functor
List<String> result = CollectionX.of(1,2,3,4)
.patternMatch(
c->c.valuesWhere(i->"even", (Integer i)->i%2==0 )
)
// CollectionX["odd","even","odd","even"]
patternMatch
in interface Functor<T>
case1
- Function to generate a case (or chain of cases as a single case)otherwise
- Value if supplied case doesn't matchdefault <U> MaybeT<U> ofType(java.lang.Class<? extends U> type)
Filterable
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
ofType
in interface Filterable<T>
default MaybeT<T> filterNot(java.util.function.Predicate<? super T> fn)
Filterable
of(1,2,3).filter(i->i>2);
//[1,2]
filterNot
in interface Filterable<T>
fn
- to filter elements by, retaining matchesdefault MaybeT<T> notNull()
Filterable
of(1,2,null,4).nonNull();
//[1,2,4]
notNull
in interface Filterable<T>