ST - Secondary typePT - Primary typepublic interface Xor<ST,PT> extends java.util.function.Supplier<PT>, MonadicValue2<ST,PT>, Functor<PT>, Filterable<PT>, ApplicativeFunctor<PT>
Xor.primary("hello").map(v->v+" world")
//Xor.primary["hello world"]
Values can be accumulated via
Xor.accumulateSecondary(ListX.of(Xor.secondary("failed1"),
Xor.secondary("failed2"),
Xor.primary("success")),
Semigroups.stringConcat)
//failed1failed2
Xor<String,String> fail1 = Xor.secondary("failed1");
fail1.swap().ap((a,b)->a+b)
.ap(Xor.secondary("failed2").swap())
.ap(Xor.<String,String>primary("success").swap())
//failed1failed2
| Modifier and Type | Interface and Description |
|---|---|
static class |
Xor.Primary<ST,PT> |
static class |
Xor.Secondary<ST,PT> |
ApplicativeFunctor.Applicatives, ApplicativeFunctor.ApplyFunctions<T>Value.ValueImpl<T>Convertable.SupplierToConvertable<T>| Modifier and Type | Method and Description |
|---|---|
static <ST,PT,R> Xor<?,R> |
accumulatePrimary(CollectionX<Xor<ST,PT>> xors,
java.util.function.Function<? super PT,R> mapper,
Semigroup<R> reducer) |
static <ST,PT,R> Xor<?,R> |
accumulatePrimary(CollectionX<Xor<ST,PT>> xors,
Reducer<R> reducer) |
static <ST,PT> Xor<?,PT> |
accumulatePrimary(CollectionX<Xor<ST,PT>> xors,
Semigroup<PT> reducer) |
static <ST,PT,R> Xor<?,R> |
accumulateSecondary(CollectionX<Xor<ST,PT>> xors,
java.util.function.Function<? super ST,R> mapper,
Semigroup<R> reducer) |
static <ST,PT,R> Xor<?,R> |
accumulateSecondary(CollectionX<Xor<ST,PT>> xors,
Reducer<R> reducer) |
static <ST,PT> Xor<?,ST> |
accumulateSecondary(CollectionX<Xor<ST,PT>> xors,
Semigroup<ST> reducer)
Xor.accumulateSecondary(ListX.of(Xor.secondary("failed1"),
Xor.secondary("failed2"),
Xor.primary("success")),
Semigroups.stringConcat)
//Xors.Primary[failed1failed2]
|
default AnyMValue<PT> |
anyM() |
<T2,R> Xor<ST,R> |
ap(Value<? extends T2> app,
java.util.function.BiFunction<? super PT,? super T2,? extends R> fn) |
default <T2,R> Xor<ST,R> |
ap(Xor<? extends ST,? extends T2> app,
java.util.function.BinaryOperator<ST> semigroup,
java.util.function.BiFunction<? super PT,? super T2,? extends R> fn)
Accumulate secondary values with the provided BinaryOperator / Semigroup
Semigroups
Primary accumulation only occurs if all phases are primary |
default <T2,R> Xor<PStackX<ST>,R> |
apToList(Xor<ST,? extends T2> app,
java.util.function.BiFunction<? super PT,? super T2,? extends R> fn)
Accumulate secondarys into a PStackX (extended Persistent List) and Primary with the supplied combiner function
Primary accumulation only occurs if all phases are primary
|
default <U> Xor<ST,U> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException. |
default <R> Xor<ST,R> |
coflatMap(java.util.function.Function<? super MonadicValue<PT>,R> mapper) |
default Xor<ST,PT> |
combine(Monoid<PT> monoid,
MonadicValue2<? extends ST,? extends PT> v2)
Eagerly combine two MonadicValues using the supplied monoid
|
Xor<ST,PT> |
filter(java.util.function.Predicate<? super PT> test)
Keep only elements for which the supplied predicates hold
e.g.
|
default Xor<ST,PT> |
filterNot(java.util.function.Predicate<? super PT> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
<LT1,RT1> Xor<LT1,RT1> |
flatMap(java.util.function.Function<? super PT,? extends MonadicValue2<? extends LT1,? extends RT1>> mapper) |
static <ST,T> Xor<ST,T> |
fromIterable(java.lang.Iterable<T> iterable) |
static <T> Xor<java.lang.Throwable,T> |
fromPublisher(org.reactivestreams.Publisher<T> pub) |
PT |
get() |
default boolean |
isPresent() |
boolean |
isPrimary() |
boolean |
isSecondary() |
default Xor<PStackX<ST>,PT> |
list() |
<R> Xor<ST,R> |
map(java.util.function.Function<? super PT,? extends R> fn)
Transform this functor using the supplied transformation function
|
<R> Eval<R> |
matches(java.util.function.Function<Matchable.CheckValue1<ST,R>,Matchable.CheckValue1<ST,R>> fn1,
java.util.function.Function<Matchable.CheckValue1<PT,R>,Matchable.CheckValue1<PT,R>> fn2,
java.util.function.Supplier<? extends R> otherwise) |
default Xor<ST,MonadicValue<PT>> |
nest() |
default Xor<ST,PT> |
notNull()
Filter elements retaining only values which are not null
|
default <U> Xor<ST,U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a stream that are of a given type.
|
default <R> Xor<ST,R> |
patternMatch(java.util.function.Function<Matchable.CheckValue1<PT,R>,Matchable.CheckValue1<PT,R>> case1,
java.util.function.Supplier<? extends R> otherwise)
Transform the elements of this Stream with a Pattern Matching case and default value
|
Xor<ST,PT> |
peek(java.util.function.Consumer<? super PT> action)
Peek at the current value of this Functor, without transforming it
|
void |
peek(java.util.function.Consumer<? super ST> stAction,
java.util.function.Consumer<? super PT> ptAction) |
static <ST,PT> Xor<ST,PT> |
primary(PT value) |
static <ST,PT> Xor<ST,PT> |
secondary(ST value)
Create an instance of the secondary type.
|
<LT1,RT1> Xor<LT1,RT1> |
secondaryFlatMap(java.util.function.Function<? super ST,? extends Xor<LT1,RT1>> mapper) |
ST |
secondaryGet() |
<R> Xor<R,PT> |
secondaryMap(java.util.function.Function<? super ST,? extends R> fn) |
Xor<ST,PT> |
secondaryPeek(java.util.function.Consumer<? super ST> action) |
java.util.Optional<ST> |
secondaryToOptional() |
Xor<ST,PT> |
secondaryToPrimayFlatMap(java.util.function.Function<? super ST,? extends Xor<ST,PT>> fn) |
Xor<ST,PT> |
secondaryToPrimayMap(java.util.function.Function<? super ST,? extends PT> fn) |
ReactiveSeq<ST> |
secondaryToStream() |
Value<ST> |
secondaryValue() |
static <ST,PT> Xor<ListX<ST>,ListX<PT>> |
sequencePrimary(CollectionX<Xor<ST,PT>> xors) |
static <ST,PT> Xor<ListX<PT>,ListX<ST>> |
sequenceSecondary(CollectionX<Xor<ST,PT>> xors) |
Xor<PT,ST> |
swap() |
Ior<ST,PT> |
toIor() |
default java.util.Optional<PT> |
toOptional() |
default Xor<ST,PT> |
toXor() |
default <ST2> Xor<ST2,PT> |
toXor(ST2 secondary)
Convert to an Xor where the secondary value will be used if no primary value is present
|
default <R> Xor<ST,R> |
trampoline(java.util.function.Function<? super PT,? extends Trampoline<? extends R>> mapper)
Performs a map operation that can call a recursive method without running out of stack space
|
default <T> Xor<ST,T> |
unit(T unit) |
<R> R |
visit(java.util.function.Function<? super ST,? extends R> secondary,
java.util.function.Function<? super PT,? extends R> primary) |
default <R1,R2> Xor<R1,R2> |
visitXor(java.util.function.Function<? super ST,? extends R1> secondary,
java.util.function.Function<? super PT,? extends R2> primary) |
default <T2,R> Xor<ST,R> |
zip(java.util.function.BiFunction<? super PT,? super T2,? extends R> fn,
org.reactivestreams.Publisher<? extends T2> app) |
default <T2,R> Xor<ST,R> |
zip(java.lang.Iterable<? extends T2> app,
java.util.function.BiFunction<? super PT,? super T2,? extends R> fn) |
applyFunctionscollect, fold, fold, generate, iterate, mapReduce, mkString, newSubscriber, of, stream, subscribe, test, toDequeX, toEvalAlways, toEvalLater, toEvalNow, toFeatureToggle, toFutureStream, toFutureStream, toLazyImmutable, toList, toListX, toMaybe, toMutable, toPBagX, toPOrderedSetX, toPQueueX, toPSetX, toPStackX, toPVectorX, toQueueX, toSetX, toSimpleReact, toSimpleReact, toSortedSetX, toTry, toTry, toTry, unapplyendsWith, endsWithIterable, findAny, findFirst, firstValue, foldable, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, validate, xMatchcollect, fromSupplier, iterator, orElse, orElseGet, orElseThrow, toAtomicReference, toCompletableFuture, toCompletableFutureAsync, toCompletableFutureAsync, toFutureW, toFutureWAsync, toFutureWAsync, toOptionalAtomicReference, toStream, visitstatic <T> Xor<java.lang.Throwable,T> fromPublisher(org.reactivestreams.Publisher<T> pub)
static <ST,T> Xor<ST,T> fromIterable(java.lang.Iterable<T> iterable)
static <ST,PT> Xor<ST,PT> secondary(ST value)
Xor.<Integer,Integer>secondary(10).map(i->i+1);
//Xor.secondary[10]
Xor.<Integer,Integer>secondary(10).swap().map(i->i+1);
//Xor.primary[11]
value - to wrapstatic <ST,PT> Xor<ST,PT> primary(PT value)
default AnyMValue<PT> anyM()
anyM in interface MonadicValue<PT>default <R> Xor<ST,R> coflatMap(java.util.function.Function<? super MonadicValue<PT>,R> mapper)
coflatMap in interface MonadicValue<PT>default Xor<ST,MonadicValue<PT>> nest()
nest in interface MonadicValue<PT>default Xor<ST,PT> combine(Monoid<PT> monoid, MonadicValue2<? extends ST,? extends PT> v2)
MonadicValue2
Monoid<Integer> add = Mondoid.of(1,Semigroups.intSum);
Maybe.of(10).plus(add,Maybe.none());
//Maybe[10]
Maybe.none().plus(add,Maybe.of(10));
//Maybe[10]
Maybe.none().plus(add,Maybe.none());
//Maybe.none()
Maybe.of(10).plus(add,Maybe.of(10));
//Maybe[20]
Monoid<Integer> firstNonNull = Monoid.of(null , Semigroups.firstNonNull());
Maybe.of(10).plus(firstNonNull,Maybe.of(10));
//Maybe[10]
combine in interface MonadicValue2<ST,PT>default <T> Xor<ST,T> unit(T unit)
unit in interface MonadicValue<PT>unit in interface MonadicValue2<ST,PT>unit in interface Unit<PT>default java.util.Optional<PT> toOptional()
toOptional in interface Convertable<PT>Xor<ST,PT> filter(java.util.function.Predicate<? super PT> test)
Filterable
of(1,2,3).filter(i->i>2);
//[3]
filter in interface Filterable<PT>test - to filter elements by, retaining matchesXor<ST,PT> secondaryToPrimayMap(java.util.function.Function<? super ST,? extends PT> fn)
<R> Xor<ST,R> map(java.util.function.Function<? super PT,? extends R> fn)
Functor
of(1,2,3).map(i->i*2)
//[2,4,6]
map in interface ConvertableFunctor<PT>map in interface Functor<PT>map in interface MonadicValue<PT>map in interface MonadicValue2<ST,PT>fn - Transformation functionXor<ST,PT> peek(java.util.function.Consumer<? super PT> action)
Functor
of(1,2,3).map(System.out::println)
1
2
3
default boolean isPresent()
isPresent in interface Convertable<PT>default <ST2> Xor<ST2,PT> toXor(ST2 secondary)
Valuestatic <ST,PT> Xor<ListX<PT>,ListX<ST>> sequenceSecondary(CollectionX<Xor<ST,PT>> xors)
static <ST,PT,R> Xor<?,R> accumulateSecondary(CollectionX<Xor<ST,PT>> xors, Reducer<R> reducer)
static <ST,PT,R> Xor<?,R> accumulateSecondary(CollectionX<Xor<ST,PT>> xors, java.util.function.Function<? super ST,R> mapper, Semigroup<R> reducer)
static <ST,PT> Xor<ListX<ST>,ListX<PT>> sequencePrimary(CollectionX<Xor<ST,PT>> xors)
static <ST,PT,R> Xor<?,R> accumulatePrimary(CollectionX<Xor<ST,PT>> xors, Reducer<R> reducer)
static <ST,PT,R> Xor<?,R> accumulatePrimary(CollectionX<Xor<ST,PT>> xors, java.util.function.Function<? super PT,R> mapper, Semigroup<R> reducer)
static <ST,PT> Xor<?,PT> accumulatePrimary(CollectionX<Xor<ST,PT>> xors, Semigroup<PT> reducer)
static <ST,PT> Xor<?,ST> accumulateSecondary(CollectionX<Xor<ST,PT>> xors, Semigroup<ST> reducer)
Xor.accumulateSecondary(ListX.of(Xor.secondary("failed1"),
Xor.secondary("failed2"),
Xor.primary("success")),
Semigroups.stringConcat)
//Xors.Primary[failed1failed2]
xors - reducer - <R> R visit(java.util.function.Function<? super ST,? extends R> secondary, java.util.function.Function<? super PT,? extends R> primary)
default <R1,R2> Xor<R1,R2> visitXor(java.util.function.Function<? super ST,? extends R1> secondary, java.util.function.Function<? super PT,? extends R2> primary)
default <R> Xor<ST,R> patternMatch(java.util.function.Function<Matchable.CheckValue1<PT,R>,Matchable.CheckValue1<PT,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<PT>case1 - Function to generate a case (or chain of cases as a single case)otherwise - Value if supplied case doesn't match<R> Eval<R> matches(java.util.function.Function<Matchable.CheckValue1<ST,R>,Matchable.CheckValue1<ST,R>> fn1, java.util.function.Function<Matchable.CheckValue1<PT,R>,Matchable.CheckValue1<PT,R>> fn2, java.util.function.Supplier<? extends R> otherwise)
PT get()
get in interface Convertable<PT>get in interface java.util.function.Supplier<PT>ST secondaryGet()
java.util.Optional<ST> secondaryToOptional()
ReactiveSeq<ST> secondaryToStream()
<LT1,RT1> Xor<LT1,RT1> flatMap(java.util.function.Function<? super PT,? extends MonadicValue2<? extends LT1,? extends RT1>> mapper)
flatMap in interface MonadicValue2<ST,PT><LT1,RT1> Xor<LT1,RT1> secondaryFlatMap(java.util.function.Function<? super ST,? extends Xor<LT1,RT1>> mapper)
Xor<ST,PT> secondaryToPrimayFlatMap(java.util.function.Function<? super ST,? extends Xor<ST,PT>> fn)
void peek(java.util.function.Consumer<? super ST> stAction, java.util.function.Consumer<? super PT> ptAction)
boolean isPrimary()
boolean isSecondary()
<T2,R> Xor<ST,R> ap(Value<? extends T2> app, java.util.function.BiFunction<? super PT,? super T2,? extends R> fn)
ap in interface ApplicativeFunctor<PT>default Xor<PStackX<ST>,PT> list()
Xor#ap(Xor,BiFunction)default <T2,R> Xor<PStackX<ST>,R> apToList(Xor<ST,? extends T2> app, java.util.function.BiFunction<? super PT,? super T2,? extends R> fn)
app - Value to combine withfn - Combiner function for primary valuesdefault <T2,R> Xor<ST,R> ap(Xor<? extends ST,? extends T2> app, java.util.function.BinaryOperator<ST> semigroup, java.util.function.BiFunction<? super PT,? super T2,? extends R> fn)
Semigroups
Primary accumulation only occurs if all phases are primary
Xor<String,String> fail1 = Xor.secondary("failed1");
Xor<PStackX<String>,String> result = fail1.list().ap(Xor.secondary("failed2").list(), Semigroups.collectionConcat(),(a,b)->a+b);
//Secondary of [PStackX.of("failed1","failed2")))]
app - Value to combine withsemigroup - to combine secondary typesfn - To combine primary typesdefault <T2,R> Xor<ST,R> zip(java.lang.Iterable<? extends T2> app, java.util.function.BiFunction<? super PT,? super T2,? extends R> fn)
zip in interface ApplicativeFunctor<PT>default <T2,R> Xor<ST,R> zip(java.util.function.BiFunction<? super PT,? super T2,? extends R> fn, org.reactivestreams.Publisher<? extends T2> app)
zip in interface ApplicativeFunctor<PT>default <U> Xor<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<PT>default Xor<ST,PT> filterNot(java.util.function.Predicate<? super PT> fn)
Filterable
of(1,2,3).filter(i->i>2);
//[1,2]
filterNot in interface Filterable<PT>fn - to filter elements by, retaining matchesdefault Xor<ST,PT> notNull()
Filterable
of(1,2,null,4).nonNull();
//[1,2,4]
notNull in interface Filterable<PT>default <U> Xor<ST,U> cast(java.lang.Class<? extends U> type)
FunctorClassCastException.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)default <R> Xor<ST,R> trampoline(java.util.function.Function<? super PT,? 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<PT>