T
- Data types of elements managed by wrapped scalar Monad.public interface AnyMValue<W extends WitnessType<W>,T> extends AnyM<W,T>, Value<T>, Filters<T>, MonadicValue<T>
AnyM.AnyMFactory
Value.ValueImpl<T>
Convertable.SupplierToConvertable<T>
Fn0.FunctionalOperations<R>, Fn0.Instances, Fn0.SupplierKind<R>
Modifier and Type | Method and Description |
---|---|
default AnyMValue<W,java.util.List<T>> |
aggregate(AnyM<W,T> next)
Aggregate the contents of this Monad and the supplied Monad
|
default <U> AnyMValue<W,U> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException . |
default <R> AnyMValue<W,R> |
coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
Perform a coflatMap operation.
|
default <R,A> R |
collect(java.util.stream.Collector<? super T,A,R> collector)
Collect the contents of the monad wrapped by this AnyM into supplied collector
A mutable reduction operation equivalent to Stream#collect
|
default <T2,R> AnyMValue<W,R> |
combine(Value<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Lazily combine this ApplicativeFunctor with the supplied value via the supplied BiFunction
Example
|
default AnyMValue<W,T> |
combineEager(Monoid<T> monoid,
AnyMValue<W,? extends T> v2) |
default <T> AnyMValue<W,T> |
empty()
Construct an AnyM wrapping a new empty instance of the wrapped type
e.g.
|
default <T> AnyMValue<W,T> |
emptyUnit() |
default boolean |
eqv(AnyMValue<?,T> t)
Equivalence test, returns true if this Monad is equivalent to the supplied monad
e.g.
|
default AnyMValue<W,T> |
filter(java.util.function.Predicate<? super T> p)
Perform a filter operation on the wrapped monad instance e.g.
|
default AnyMValue<W,T> |
filterNot(java.util.function.Predicate<? super T> fn)
Remove any elements for which the predicate holds (inverse operation to filter)
e.g.
|
default <R> AnyMValue<W,R> |
flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
A flattening transformation operation (@see
Optional.flatMap(Function) |
default <R> AnyMValue<W,R> |
flatMapA(java.util.function.Function<? super T,? extends AnyM<W,? extends R>> fn) |
default <R> AnyMValue<W,R> |
flatMapI(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> fn)
A flattening transformation operation that takes the first value from the returned Iterable.
|
default <R> AnyMValue<W,R> |
flatMapP(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> fn)
A flattening transformation operation that takes the first value from the returned Publisher.
|
default <R> AnyMValue<W,R> |
flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> fn) |
default T |
get()
Apply this function to the arguments.
|
default boolean |
isPresent() |
default java.util.Iterator<T> |
iterator() |
default <R> AnyMValue<W,R> |
map(java.util.function.Function<? super T,? extends R> fn)
Transform this functor using the supplied transformation function
|
default Xor<AnyMValue<W,T>,AnyMSeq<W,T>> |
matchable()
Allows structural matching on the value / seq nature of this AnyM.
|
default java.lang.String |
mkString()
Returns the class name and the name of the subclass, if there is any value, the value is showed between square brackets.
|
default AnyMValue<W,MonadicValue<T>> |
nest()
cojoin pattern.
|
default AnyMValue<W,T> |
notNull()
Filter elements retaining only values which are not null
|
default <U> AnyMValue<W,U> |
ofType(java.lang.Class<? extends U> type)
Keep only those elements in a stream that are of a given type.
|
default AnyMValue<W,T> |
peek(java.util.function.Consumer<? super T> c)
Perform a peek operation on the wrapped monad e.g.
|
default <R> AnyMValue<W,R> |
retry(java.util.function.Function<? super T,? extends R> fn)
Retry a transformation if it fails.
|
default <R> AnyMValue<W,R> |
retry(java.util.function.Function<? super T,? extends R> fn,
int retries,
long delay,
java.util.concurrent.TimeUnit timeUnit)
Retry a transformation if it fails.
|
default ReactiveSeq<T> |
stream() |
default <R> AnyMValue<W,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
|
default <T> AnyMValue<W,T> |
unit(T value)
Construct a new instanceof AnyM using the type of the underlying wrapped monad
|
default AnyMValue<W,T> |
zip(java.util.function.BinaryOperator<Zippable<T>> combiner,
Zippable<T> app)
Combine two applicatives together using the provided BinaryOperator (Semigroup, Monoid and Reducer all
extend BinaryOperator - checkout Semigroups and Monoids for a large number of canned combinations).
|
default <T2,R> AnyMValue<W,R> |
zip(java.lang.Iterable<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zip (combine) this Zippable with the supplied Iterable using the supplied combining function
|
default <S,U> AnyMValue<W,org.jooq.lambda.tuple.Tuple3<T,S,U>> |
zip3(java.lang.Iterable<? extends S> second,
java.lang.Iterable<? extends U> third)
zip 3 Streams into one
|
default <S,U,R> AnyMValue<W,R> |
zip3(java.lang.Iterable<? extends S> second,
java.lang.Iterable<? extends U> third,
Fn3<? super T,? super S,? super U,? extends R> fn3) |
default <T2,T3,T4> AnyMValue<W,org.jooq.lambda.tuple.Tuple4<T,T2,T3,T4>> |
zip4(java.lang.Iterable<? extends T2> second,
java.lang.Iterable<? extends T3> third,
java.lang.Iterable<? extends T4> fourth)
zip 4 Streams into 1
|
default <T2,T3,T4,R> |
zip4(java.lang.Iterable<? extends T2> second,
java.lang.Iterable<? extends T3> third,
java.lang.Iterable<? extends T4> fourth,
Fn4<? super T,? super T2,? super T3,? super T4,? extends R> fn) |
default <T2,R> AnyMValue<W,R> |
zipP(org.reactivestreams.Publisher<? extends T2> app,
java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zip (combine) this Zippable with the supplied Publisher, using the supplied combining function
|
default <U> AnyMValue<W,org.jooq.lambda.tuple.Tuple2<T,U>> |
zipP(org.reactivestreams.Publisher<? extends U> other) |
default <R> AnyMValue<W,R> |
zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn) |
default <R> AnyMValue<W,R> |
zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn) |
default <R> AnyMValue<W,R> |
zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn) |
adapter, ap, ap2, coflatMapA, eqv, flatten, flattenI, fromArray, fromCollectionX, fromCompletableFuture, fromDoubleStream, fromEither, fromEither3, fromEither4, fromEither5, fromEval, fromFutureW, fromIntStream, fromIor, fromIterable, fromList, fromLongStream, fromMaybe, fromMonadicValue, fromOptional, fromOptionalDouble, fromOptionalInt, fromOptionalLong, fromPublisher, fromRange, fromRangeLong, fromSet, fromStream, fromStream, fromStream, fromStreamable, fromTry, fromXor, liftF, liftF2, liftF2, liftF3, liftF3, liftF4, liftF4, liftF5, liftF5, liftMFuture, liftMFuture, liftMList, liftMListX, listFromCompletableFuture, listFromEither, listFromEither3, listFromEither4, listFromEither5, listFromEval, listFromFutureW, listFromIor, listFromMaybe, listFromOptional, listFromStream, listFromStreamable, listFromXor, listOf, narrow, nestA, ofNullable, ofSeq, ofValue, ofValue, reactiveSeq, sequence, sequence, streamOf, toString, traverse, traverse, unitIterator
unwrap
combineEager, forEach2, forEach2, forEach3, forEach3, forEach4, forEach4
apply, generate, iterate, newSubscriber, of, subscribe, test, toEvalAlways, toEvalLater, toEvalNow, toIor, toMaybe, toTry, toTry, toTry, toXor, toXor
endsWith, endsWithIterable, findAny, findFirst, firstValue, foldLeft, foldLeft, foldLeft, foldLeft, foldRight, foldRight, foldRight, foldRightMapToType, get, groupBy, join, join, join, mapReduce, mapReduce, print, print, printErr, printOut, reduce, reduce, reduce, reduce, reduce, reduce, reduce, schedule, scheduleFixedDelay, scheduleFixedRate, single, single, singleOptional, startsWith, startsWithIterable, toConcurrentLazyCollection, toConcurrentLazyStreamable, toLazyCollection, xMatch
futureStream, getStreamable, isEmpty, jdkStream, reveresedJDKStream, reveresedStream
fromSupplier, orElse, orElseGet, orElseThrow, toCompletableFuture, toCompletableFutureAsync, toCompletableFutureAsync, toFuture, toFutureWAsync, toFutureWAsync, toOptional, toStream, visit
default <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
AnyM
AnyM<Integer> monad1 = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> monad2 = AnyM.fromOptional(Optional.of(1));
List<Integer> list1 = monad1.collect(Collectors.toList());
List<Integer> list2 = monad2.collect(Collectors.toList());
collect
in interface AnyM<W extends WitnessType<W>,T>
collect
in interface Convertable<T>
collector
- JDK collector to perform mutable reductiondefault boolean eqv(AnyMValue<?,T> t)
{code Optional.of(1) and CompletableFuture.completedFuture(1) are equivalent }
t
- Monad to compare todefault AnyMValue<W,T> zip(java.util.function.BinaryOperator<Zippable<T>> combiner, Zippable<T> app)
Zippable
zip
in interface Zippable<T>
Semigroup
,
Semigroups
,
Monoid
,
To lift any Semigroup (or monoid) up to handling Applicatives use the combineApplicatives operator in Semigroups
{@see com.aol.cyclops2.Semigroups#combineApplicatives(BiFunction) } or Monoids
{ {@see com.aol.cyclops2.Monoids#combineApplicatives(java.util.function.Function, com.aol.cyclops2.function.Monoid)
}
{@code
BinaryOperator> sumMaybes = Semigroups.combineScalarFunctors(Semigroups.intSum);
Maybe.just(1).zip(sumMaybes, Maybe.just(5))
//Maybe.just(6));
}
default <R> AnyMValue<W,R> coflatMap(java.util.function.Function<? super MonadicValue<T>,R> mapper)
MonadicValue
Maybe.none().coflatMap(m -> m.isPresent() ? m.get() : 10);
//Maybe[10]
coflatMap
in interface MonadicValue<T>
mapper
- Mapping / transformation functiondefault AnyMValue<W,MonadicValue<T>> nest()
MonadicValue
nest
in interface MonadicValue<T>
default java.lang.String mkString()
Value
default <R> AnyMValue<W,R> zipWith(java.lang.Iterable<java.util.function.Function<? super T,? extends R>> fn)
default <R> AnyMValue<W,R> zipWithS(java.util.stream.Stream<java.util.function.Function<? super T,? extends R>> fn)
default <R> AnyMValue<W,R> zipWithP(org.reactivestreams.Publisher<java.util.function.Function<? super T,? extends R>> fn)
default <R> AnyMValue<W,R> retry(java.util.function.Function<? super T,? extends R> fn)
Transformable
given(serviceMock.apply(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock)
.firstValue();
//result = 42
retry
in interface AnyM<W extends WitnessType<W>,T>
retry
in interface Transformable<T>
fn
- Function to retry if failsdefault <U> AnyMValue<W,org.jooq.lambda.tuple.Tuple2<T,U>> zipP(org.reactivestreams.Publisher<? extends U> other)
default <R> AnyMValue<W,R> retry(java.util.function.Function<? super T,? extends R> fn, int retries, long delay, java.util.concurrent.TimeUnit timeUnit)
Transformable
given(serviceMock.apply(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock, 7, 2, TimeUnit.SECONDS)
.firstValue();
//result = 42
retry
in interface AnyM<W extends WitnessType<W>,T>
retry
in interface Transformable<T>
fn
- Function to retry if failsretries
- Number of retriesdelay
- Delay in TimeUnitstimeUnit
- TimeUnit to use for delaydefault <S,U> AnyMValue<W,org.jooq.lambda.tuple.Tuple3<T,S,U>> zip3(java.lang.Iterable<? extends S> second, java.lang.Iterable<? extends U> third)
Zippable
List<Tuple3<Integer, Integer, Character>> list = of(1, 2, 3, 4, 5, 6).zip3(of(100, 200, 300, 400), of('a', 'b', 'c')).collect(Collectors.toList());
// [[1,100,'a'],[2,200,'b'],[3,300,'c']]
default <S,U,R> AnyMValue<W,R> zip3(java.lang.Iterable<? extends S> second, java.lang.Iterable<? extends U> third, Fn3<? super T,? super S,? super U,? extends R> fn3)
default <T2,T3,T4> AnyMValue<W,org.jooq.lambda.tuple.Tuple4<T,T2,T3,T4>> zip4(java.lang.Iterable<? extends T2> second, java.lang.Iterable<? extends T3> third, java.lang.Iterable<? extends T4> fourth)
Zippable
List<Tuple4<Integer, Integer, Character, String>> list = of(1, 2, 3, 4, 5, 6).zip4(of(100, 200, 300, 400), of('a', 'b', 'c'), of("hello", "world"))
.collect(Collectors.toList());
// [[1,100,'a',"hello"],[2,200,'b',"world"]]
default <T2,T3,T4,R> AnyMValue<W,R> zip4(java.lang.Iterable<? extends T2> second, java.lang.Iterable<? extends T3> third, java.lang.Iterable<? extends T4> fourth, Fn4<? super T,? super T2,? super T3,? super T4,? extends R> fn)
default <U> AnyMValue<W,U> ofType(java.lang.Class<? extends U> type)
Filters
// (1, 2, 3) ReactiveSeq.of(1, "a", 2, "b",3).ofType(Integer.class)
default AnyMValue<W,T> filterNot(java.util.function.Predicate<? super T> fn)
Filters
of(1,2,3).filter(i->i>2);
//[1,2]
default AnyMValue<W,T> notNull()
Filters
of(1,2,null,4).nonNull();
//[1,2,4]
default <U> AnyMValue<W,U> cast(java.lang.Class<? extends U> type)
Transformable
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)cast
in interface Transformable<T>
default <R> AnyMValue<W,R> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Transformable
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 Transformable<T>
mapper
- TCO Transformation functiondefault AnyMValue<W,T> filter(java.util.function.Predicate<? super T> p)
AnyM
AnyM.fromOptional(Optional.of(10)).filter(i->i<10);
//AnyM[Optional.empty()]
AnyM.fromStream(Stream.of(5,10)).filter(i->i<10);
//AnyM[Stream[5]]
default <R> AnyMValue<W,R> map(java.util.function.Function<? super T,? extends R> fn)
Transformable
of(1,2,3).map(i->i*2)
//[2,4,6]
map
in interface AnyM<W extends WitnessType<W>,T>
map
in interface MonadicValue<T>
map
in interface Transformable<T>
fn
- Transformation functiondefault AnyMValue<W,T> peek(java.util.function.Consumer<? super T> c)
AnyM
AnyM.fromCompletableFuture(CompletableFuture.supplyAsync(()->loadData())
.peek(System.out::println)
peek
in interface AnyM<W extends WitnessType<W>,T>
peek
in interface Transformable<T>
c
- Consumer to accept current datadefault boolean isPresent()
isPresent
in interface Convertable<T>
default AnyMValue<W,java.util.List<T>> aggregate(AnyM<W,T> next)
AnyM
AnyM.fromStream(Stream.of(1,2,3,4))
.aggregate(fromEither5(Optional.of(5)))
AnyM[Stream[List[1,2,3,4,5]]
List<Integer> result = AnyM.fromStream(Stream.of(1,2,3,4))
.aggregate(fromEither5(Optional.of(5)))
.toSequence()
.flatten()
.toList();
assertThat(result,equalTo(Arrays.asList(1,2,3,4,5)));
default <R> AnyMValue<W,R> flatMapA(java.util.function.Function<? super T,? extends AnyM<W,? extends R>> fn)
default <R> AnyMValue<W,R> flatMapI(java.util.function.Function<? super T,? extends java.lang.Iterable<? extends R>> fn)
MonadicValue
Maybe.just(1).map(i->i+2).flatMapI(i->Arrays.asList(()->i*3,20);
//Maybe[9]
flatMapI
in interface AnyM<W extends WitnessType<W>,T>
flatMapI
in interface MonadicValue<T>
fn
- transformation functiondefault <R> AnyMValue<W,R> flatMapP(java.util.function.Function<? super T,? extends org.reactivestreams.Publisher<? extends R>> fn)
MonadicValue
FutureW.ofResult(1).map(i->i+2).flatMapP(i->Flux.just(()->i*3,20);
//FutureW[9]
flatMapP
in interface AnyM<W extends WitnessType<W>,T>
flatMapP
in interface MonadicValue<T>
fn
- transformation functiondefault <R> AnyMValue<W,R> flatMapS(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> fn)
flatMapS
in interface AnyM<W extends WitnessType<W>,T>
flatMapS
in interface MonadicValue<T>
default <R> AnyMValue<W,R> flatMap(java.util.function.Function<? super T,? extends MonadicValue<? extends R>> fn)
MonadicValue
Optional.flatMap(Function)
Eval.now(1).map(i->i+2).flatMap(i->Eval.later(()->i*3);
//Eval[9]
flatMap
in interface MonadicValue<T>
fn
- transformation functiondefault T get()
org.jooq.lambda.function.Function0
get
in interface Convertable<T>
get
in interface org.jooq.lambda.function.Function0<T>
get
in interface java.util.function.Supplier<T>
default <T> AnyMValue<W,T> unit(T value)
AnyM
AnyM<Integer> ints = AnyM.fromList(Arrays.asList(1,2,3);
AnyM<String> string = ints.unit("hello");
default <T> AnyMValue<W,T> empty()
AnyM
Any<Integer> ints = AnyM.fromStream(Stream.of(1,2,3));
AnyM<Integer> empty=ints.empty();
default Xor<AnyMValue<W,T>,AnyMSeq<W,T>> matchable()
AnyM
AnyM<String> monad;
monad.matchable().visit(v->handleValue(v.get()),s->handleSequence(s.toList()));
default <T2,R> AnyMValue<W,R> combine(Value<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
MonadicValue
Maybe<Integer> some = Maybe.just(10);
just.combine(Eval.now(20), this::add);
//Some[30]
Maybe<Integer> none = Maybe.none();
none.combine(Eval.now(20), this::add);
//None
combine
in interface MonadicValue<T>
app
- Value to combine with this one.fn
- BiFunction to combine themdefault <T2,R> AnyMValue<W,R> zip(java.lang.Iterable<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zippable
default <T2,R> AnyMValue<W,R> zipP(org.reactivestreams.Publisher<? extends T2> app, java.util.function.BiFunction<? super T,? super T2,? extends R> fn)
Zippable
default java.util.Iterator<T> iterator()