T
- The type contained on the Xor withinpublic interface XorT<ST,T> extends org.reactivestreams.Publisher<T>, Functor<T>, Filterable<T>
Modifier and Type | Method and Description |
---|---|
default <B> XorT<ST,B> |
bind(java.util.function.Function<? super T,XorT<ST,? extends B>> f)
Flat Map the wrapped Xor
|
default <U> XorT<ST,U> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException . |
<R> XorT<ST,R> |
empty() |
static <ST,T> XorTSeq<ST,T> |
emptyList() |
static <ST,PT> XorTValue<ST,PT> |
emptyOptional() |
XorT<ST,T> |
filter(java.util.function.Predicate<? super T> test)
Filter the wrapped Xor
|
default XorT<ST,T> |
filterNot(java.util.function.Predicate<? super T> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
<ST2,B> XorT<ST2,B> |
flatMap(java.util.function.Function<? super T,? extends MonadicValue2<? extends ST2,? extends B>> f) |
static <ST,A> XorT<ST,A> |
fromAnyM(AnyM<A> anyM)
|
static <ST,A> XorTSeq<ST,A> |
fromAnyMSeq(AnyMSeq<A> anyM) |
static <ST,A> XorTValue<ST,A> |
fromAnyMValue(AnyMValue<A> anyM) |
static <ST,A> XorTValue<ST,A> |
fromFuture(java.util.concurrent.CompletableFuture<Xor<ST,A>> future) |
static <ST,A> XorTSeq<ST,A> |
fromIterable(java.lang.Iterable<Xor<ST,A>> iterableOfXors) |
static <ST,A> XorTValue<ST,A> |
fromIterablXorue(java.lang.Iterable<Xor<ST,A>> iterableOfXors) |
static <ST,A> XorTValue<ST,A> |
fromOptional(java.util.Optional<Xor<ST,A>> optional) |
static <ST,A> XorTSeq<ST,A> |
fromPublisher(org.reactivestreams.Publisher<Xor<ST,A>> publisherOfXors) |
static <ST,A> XorTSeq<ST,A> |
fromStream(java.util.stream.Stream<Xor<ST,A>> streamOfXors) |
static <A,ST,V extends MonadicValue<Xor<ST,A>>> |
fromValue(V monadicValue) |
static <ST,U,R> java.util.function.Function<XorT<ST,U>,XorT<ST,R>> |
lift(java.util.function.Function<? super U,? extends R> fn)
Lift a function into one that accepts and returns an XorT This allows
multiple monad types to add functionality to existing functions and
methods
e.g.
|
static <ST,U1,U2,R> |
lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
Lift a BiFunction into one that accepts and returns XorTs This allows
multiple monad types to add functionality to existing functions and
methods
e.g.
|
<B> XorT<ST,B> |
map(java.util.function.Function<? super T,? extends B> f)
Map the wrapped Xor
|
default XorT<ST,T> |
notNull()
Filter elements retaining only values which are not null
|
static <ST,A> XorT<ST,A> |
of(AnyM<Xor<ST,A>> monads)
Construct an XorT from an AnyM that wraps a monad containing Xors
|
default <U> XorT<ST,U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a stream that are of a given type.
|
default <R> XorT<ST,R> |
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
|
XorT<ST,T> |
peek(java.util.function.Consumer<? super T> peek)
Peek at the current value of the Xor
|
XorT<T,ST> |
swap() |
default <R> XorT<ST,R> |
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> XorT<ST,R> |
unit(R value) |
AnyM<Xor<ST,T>> |
unwrap() |
<ST2,B> XorT<ST2,B> flatMap(java.util.function.Function<? super T,? extends MonadicValue2<? extends ST2,? extends B>> f)
XorT<ST,T> peek(java.util.function.Consumer<? super T> peek)
XorT.of(AnyM.fromStream(Xor.of(10))
.peek(System.out::println);
//prints 10
XorT<ST,T> filter(java.util.function.Predicate<? super T> test)
XorT.of(AnyM.fromStream(Xor.of(10))
.filter(t->t!=10);
//XorT<AnyM<Stream<Xor.empty>>>
filter
in interface Filterable<T>
test
- Predicate to filter the wrapped Xor<B> XorT<ST,B> map(java.util.function.Function<? super T,? extends B> f)
XorT.of(AnyM.fromStream(Xor.of(10))
.map(t->t=t+1);
//XorT<AnyM<Stream<Xor[11]>>>
default <B> XorT<ST,B> bind(java.util.function.Function<? super T,XorT<ST,? extends B>> f)
XorT.of(AnyM.fromStream(Xor.of(10))
.flatMap(t->Xor.empty();
//XorT<AnyM<Stream<Xor.empty>>>
f
- FlatMap functionstatic <ST,U,R> java.util.function.Function<XorT<ST,U>,XorT<ST,R>> lift(java.util.function.Function<? super U,? extends R> fn)
Function<Integer, Integer> add2 = i -> i + 2;
Function<XorT<Integer>, XorT<Integer>> optTAdd2 = XorT.lift(add2);
Stream<Integer> withNulls = Stream.of(1, 2, null);
AnyM<Integer> stream = AnyM.ofMonad(withNulls);
AnyM<Xor<Integer>> streamOpt = stream.map(Xor::ofNullable);
List<Integer> results = optTAdd2.apply(XorT.of(streamOpt)).unwrap().<Stream<Xor<Integer>>> unwrap()
.filter(Xor::isPresent).map(Xor::get).collect(Collectors.toList());
// Arrays.asList(3,4);
fn
- Function to enhance with functionality from Xor and another
monad typestatic <ST,U1,U2,R> java.util.function.BiFunction<XorT<ST,U1>,XorT<ST,U2>,XorT<ST,R>> lift2(java.util.function.BiFunction<? super U1,? super U2,? extends R> fn)
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
BiFunction<XorT<Integer>, XorT<Integer>, XorT<Integer>> optTAdd2 = XorT.lift2(add);
Stream<Integer> withNulls = Stream.of(1, 2, null);
AnyM<Integer> stream = AnyM.ofMonad(withNulls);
AnyM<Xor<Integer>> streamOpt = stream.map(Xor::ofNullable);
CompletableFuture<Xor<Integer>> two = CompletableFuture.supplyAsync(() -> Xor.of(2));
AnyM<Xor<Integer>> future = AnyM.ofMonad(two);
List<Integer> results = optTAdd2.apply(XorT.of(streamOpt), XorT.of(future)).unwrap()
.<Stream<Xor<Integer>>> unwrap().filter(Xor::isPresent).map(Xor::get)
.collect(Collectors.toList());
// Arrays.asList(3,4);
fn
- BiFunction to enhance with functionality from Xor and
another monad typestatic <ST,A> XorT<ST,A> fromAnyM(AnyM<A> anyM)
anyM
- AnyM that doesn't contain a monad wrapping an Xorstatic <ST,A> XorT<ST,A> of(AnyM<Xor<ST,A>> monads)
monads
- AnyM that contains a monad wrapping an Xorstatic <ST,A> XorTSeq<ST,A> fromPublisher(org.reactivestreams.Publisher<Xor<ST,A>> publisherOfXors)
static <A,ST,V extends MonadicValue<Xor<ST,A>>> XorTValue<ST,A> fromValue(V monadicValue)
static <ST,A> XorTValue<ST,A> fromFuture(java.util.concurrent.CompletableFuture<Xor<ST,A>> future)
static <ST,A> XorTValue<ST,A> fromIterablXorue(java.lang.Iterable<Xor<ST,A>> iterableOfXors)
static <ST,PT> XorTValue<ST,PT> emptyOptional()
static <ST,T> XorTSeq<ST,T> emptyList()
default <U> XorT<ST,U> cast(java.lang.Class<? extends U> type)
Functor
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)default <R> XorT<ST,R> 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> XorT<ST,R> 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> XorT<ST,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 XorT<ST,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 XorT<ST,T> notNull()
Filterable
of(1,2,null,4).nonNull();
//[1,2,4]
notNull
in interface Filterable<T>