T
- Data type of element(s) in this Foldablepublic interface Foldable<T>
Modifier and Type | Method and Description |
---|---|
default boolean |
endsWith(java.util.stream.Stream<T> stream)
assertTrue(ReactiveSeq.of(1,2,3,4,5,6)
.endsWith(Stream.of(5,6)));
|
default boolean |
endsWithIterable(java.lang.Iterable<T> iterable)
assertTrue(ReactiveSeq.of(1,2,3,4,5,6)
.endsWith(Arrays.asList(5,6)));
|
default java.util.Optional<T> |
findAny() |
default java.util.Optional<T> |
findFirst() |
default T |
firstValue()
assertThat(ReactiveSeq.of(1,2,3,4)
.map(u->throw new RuntimeException())
.recover(e->"hello")
.firstValue(),equalTo("hello"));
|
default Foldable<T> |
foldable() |
default T |
foldRight(Monoid<T> reducer)
ReactiveSeq.of("a","b","c").foldRight(Reducers.toString(""));
// "cab"
|
default T |
foldRight(T identity,
java.util.function.BinaryOperator<T> accumulator)
Immutable reduction from right to left
|
default <U> U |
foldRight(U identity,
java.util.function.BiFunction<? super T,? super U,? extends U> accumulator)
Immutable reduction from right to left
|
default <T> T |
foldRightMapToType(Reducer<T> reducer)
Attempt to map this Monad to the same type as the supplied Monoid (using
mapToType on the monoid interface) Then use Monoid to reduce values
|
default java.util.Optional<T> |
get(long index)
Return the elementAt index or Optional.empty
|
default <K> MapX<K,java.util.List<T>> |
groupBy(java.util.function.Function<? super T,? extends K> classifier)
Use classifier function to group elements in this Sequence into a Map
|
default java.lang.String |
join()
assertEquals("123".length(),ReactiveSeq.of(1, 2, 3).join().length());
|
default java.lang.String |
join(java.lang.String sep)
assertEquals("1, 2, 3".length(), ReactiveSeq.of(1, 2, 3).join(", ").length());
|
default java.lang.String |
join(java.lang.String sep,
java.lang.String start,
java.lang.String end)
assertEquals("^1|2|3$".length(), of(1, 2, 3).join("|", "^", "$").length());
|
default <R> R |
mapReduce(java.util.function.Function<? super T,? extends R> mapper,
Monoid<R> reducer)
Attempt to map this Monad to the same type as the supplied Monoid, using
supplied function Then use Monoid to reduce values
|
default <R> R |
mapReduce(Reducer<R> reducer)
Attempt to map this Sequence to the same type as the supplied Monoid
(Reducer) Then use Monoid to reduce values
|
default void |
print(java.io.PrintStream str)
Write each element within this Foldable in turn to the supplied PrintStream
|
default void |
print(java.io.PrintWriter writer)
Write each element within this Foldable in turn to the supplied PrintWriter
|
default void |
printErr()
Print each value in this Foldable to the error console in turn (left-to-right)
|
default void |
printOut()
Print each value in this Foldable to the console in turn (left-to-right)
|
default java.util.Optional<T> |
reduce(java.util.function.BinaryOperator<T> accumulator)
An equivalent function to
Stream.reduce(BinaryOperator) |
default ListX<T> |
reduce(java.lang.Iterable<? extends Monoid<T>> reducers)
Reduce with multiple reducers in parallel NB if this Monad is an Optional
[Arrays.asList(1,2,3)] reduce will operate on the Optional as if the list
was one value To reduce over the values on the list, called
streamedMonad() first.
|
default T |
reduce(Monoid<T> reducer)
Reduce this Foldable to a single value, using the supplied Monoid.
|
default ListX<T> |
reduce(java.util.stream.Stream<? extends Monoid<T>> reducers)
Reduce with multiple reducers in parallel NB if this Monad is an Optional
[Arrays.asList(1,2,3)] reduce will operate on the Optional as if the list
was one value To reduce over the values on the list, called
streamedMonad() first.
|
default T |
reduce(T identity,
java.util.function.BinaryOperator<T> accumulator)
An equivalent function to
Stream.reduce(Object, BinaryOperator) |
default <U> U |
reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator)
An equivalent function to
Stream.reduce(Object, BinaryOperator) |
default <U> U |
reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator,
java.util.function.BinaryOperator<U> combiner)
An equivalent function to
Stream.reduce(Object, BiFunction, BinaryOperator) |
default HotStream<T> |
schedule(java.lang.String cron,
java.util.concurrent.ScheduledExecutorService ex)
Execute this Stream on a schedule
|
default HotStream<T> |
scheduleFixedDelay(long delay,
java.util.concurrent.ScheduledExecutorService ex)
Execute this Stream on a schedule
|
default HotStream<T> |
scheduleFixedRate(long rate,
java.util.concurrent.ScheduledExecutorService ex)
Execute this Stream on a schedule
|
default T |
single()
//1
ReactiveSeq.of(1).single();
//UnsupportedOperationException
ReactiveSeq.of().single();
//UnsupportedOperationException
ReactiveSeq.of(1,2,3).single();
|
default T |
single(java.util.function.Predicate<? super T> predicate) |
default java.util.Optional<T> |
singleOptional()
//Optional[1]
ReactiveSeq.of(1).singleOptional();
//Optional.empty
ReactiveSeq.of().singleOpional();
//Optional.empty
ReactiveSeq.of(1,2,3).singleOptional();
|
default boolean |
startsWith(java.util.stream.Stream<T> stream)
assertTrue(ReactiveSeq.of(1,2,3,4).startsWith(Stream.of(1,2,3))) |
default boolean |
startsWithIterable(java.lang.Iterable<T> iterable)
assertTrue(ReactiveSeq.of(1,2,3,4).startsWith(Arrays.asList(1,2,3)));
|
ReactiveSeq<T> |
stream() |
default CollectionX<T> |
toConcurrentLazyCollection()
Lazily converts this SequenceM into a Collection.
|
default Streamable<T> |
toConcurrentLazyStreamable()
Streamable<Integer> repeat = ReactiveSeq.of(1, 2, 3, 4, 5, 6)
.map(i ->; i + 2)
.toConcurrentLazyStreamable();
assertThat(repeat.sequenceM().toList(), equalTo(Arrays.asList(2, 4, 6, 8, 10, 12)));
assertThat(repeat.sequenceM().toList(), equalTo(Arrays.asList(2, 4, 6, 8, 10, 12)));
|
default CollectionX<T> |
toLazyCollection()
Lazily converts this SequenceM into a Collection.
|
default <S,F> Ior<ReactiveSeq<F>,ReactiveSeq<S>> |
validate(Validator<T,S,F> validator)
Apply the specified validator to all elements in this foldable, resulting in a failure Stream and success Stream.
|
default boolean |
xMatch(int num,
java.util.function.Predicate<? super T> c)
Check that there are specified number of matches of predicate in the
Stream
|
ReactiveSeq<T> stream()
ReactiveSeq
) of it's elementsdefault Foldable<T> foldable()
default <R> R mapReduce(Reducer<R> reducer)
ReactiveSeq.of("hello","2","world","4").mapReduce(Reducers.toCountInt());
//4
reducer
- Monoid to reduce valuesdefault <R> R mapReduce(java.util.function.Function<? super T,? extends R> mapper, Monoid<R> reducer)
ReactiveSeq.of("one","two","three","four")
.mapReduce(this::toInt,Reducers.toTotalInt());
//10
int toInt(String s){
if("one".equals(s))
return 1;
if("two".equals(s))
return 2;
if("three".equals(s))
return 3;
if("four".equals(s))
return 4;
return -1;
}
mapper
- Function to map Monad typereducer
- Monoid to reduce valuesdefault T reduce(Monoid<T> reducer)
ReactiveSeq.of("hello","2","world","4").reduce(Reducers.toString(","));
//hello,2,world,4
reducer
- Use supplied Monoid to reduce valuesdefault java.util.Optional<T> reduce(java.util.function.BinaryOperator<T> accumulator)
Stream.reduce(BinaryOperator)
ReactiveSeq.of(1,2,3,4,5).map(it -> it*100).reduce(
(acc,next) -> acc+next)
//Optional[1500]
accumulator
- Combiner functiondefault T reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
Stream.reduce(Object, BinaryOperator)
accumulator
- Combiner functiondefault <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator)
Stream.reduce(Object, BinaryOperator)
identity
- Identity value for the combiner function (leaves the input unchanged)accumulator
- Combiner functiondefault <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
Stream.reduce(Object, BiFunction, BinaryOperator)
default ListX<T> reduce(java.util.stream.Stream<? extends Monoid<T>> reducers)
Monoid<Integer> sum = Monoid.of(0, (a, b) -> a + b);
Monoid<Integer> mult = Monoid.of(1, (a, b) -> a * b);
List<Integer> result = ReactiveSeq.of(1, 2, 3, 4)
.reduce(Arrays.asList(sum, mult)
.foldable());
assertThat(result, equalTo(Arrays.asList(10, 24)));
reducers
- default ListX<T> reduce(java.lang.Iterable<? extends Monoid<T>> reducers)
Monoid<Integer> sum = Monoid.of(0,(a,b)->a+b);
Monoid<Integer> mult = Monoid.of(1,(a,b)->a*b);
List<Integer> result = ReactiveSeq.of(1,2,3,4))
.reduce(Arrays.asList(sum,mult) );
assertThat(result,equalTo(Arrays.asList(10,24)));
reducers
- default T foldRight(Monoid<T> reducer)
ReactiveSeq.of("a","b","c").foldRight(Reducers.toString(""));
// "cab"
reducer
- Use supplied Monoid to reduce values starting via foldRightdefault T foldRight(T identity, java.util.function.BinaryOperator<T> accumulator)
assertTrue(ReactiveSeq.of("a","b","c").foldRight("", String::concat).equals("cba"));
identity
- Identity value for the combiner function (leaves the input unchanged)accumulator
- Combining functiondefault <U> U foldRight(U identity, java.util.function.BiFunction<? super T,? super U,? extends U> accumulator)
identity
- Identity value for the combiner function (leaves the input unchanged)accumulator
- Combining functiondefault <T> T foldRightMapToType(Reducer<T> reducer)
ReactiveSeq.of(1,2,3).foldRightMapToType(Reducers.toString(""));
// "321"
reducer
- Monoid to reduce valuesdefault java.lang.String join()
assertEquals("123".length(),ReactiveSeq.of(1, 2, 3).join().length());
default java.lang.String join(java.lang.String sep)
assertEquals("1, 2, 3".length(), ReactiveSeq.of(1, 2, 3).join(", ").length());
default java.lang.String join(java.lang.String sep, java.lang.String start, java.lang.String end)
assertEquals("^1|2|3$".length(), of(1, 2, 3).join("|", "^", "$").length());
default void print(java.io.PrintStream str)
str
- PrintStream to write todefault void print(java.io.PrintWriter writer)
writer
- PrintWriter to write todefault void printOut()
default void printErr()
default <K> MapX<K,java.util.List<T>> groupBy(java.util.function.Function<? super T,? extends K> classifier)
Map<Integer, List<Integer>> map1 = of(1, 2, 3, 4).groupBy(i -> i % 2);
assertEquals(asList(2, 4), map1.get(0));
assertEquals(asList(1, 3), map1.get(1));
assertEquals(2, map1.size());
default java.util.Optional<T> findFirst()
ReactiveSeq.of(1,2,3,4,5).filter(it -> it <3).findFirst().get();
//3
(deterministic)default java.util.Optional<T> findAny()
ReactiveSeq.of(1,2,3,4,5).filter(it -> it <3).findAny().get();
//3
(non-deterministic)default boolean startsWithIterable(java.lang.Iterable<T> iterable)
assertTrue(ReactiveSeq.of(1,2,3,4).startsWith(Arrays.asList(1,2,3)));
iterable
- default boolean startsWith(java.util.stream.Stream<T> stream)
assertTrue(ReactiveSeq.of(1,2,3,4).startsWith(Stream.of(1,2,3)))
stream
- Stream to check if this Foldable has the same elements in the same order, at the startdefault boolean endsWithIterable(java.lang.Iterable<T> iterable)
assertTrue(ReactiveSeq.of(1,2,3,4,5,6)
.endsWith(Arrays.asList(5,6)));
iterable
- Values to checkdefault boolean endsWith(java.util.stream.Stream<T> stream)
assertTrue(ReactiveSeq.of(1,2,3,4,5,6)
.endsWith(Stream.of(5,6)));
stream
- Values to checkdefault CollectionX<T> toLazyCollection()
Collection<Integer> col = ReactiveSeq.of(1, 2, 3, 4, 5).peek(System.out::println).toLazyCollection();
col.forEach(System.out::println);
// Will print out "first!" before anything else
default CollectionX<T> toConcurrentLazyCollection()
Collection<Integer> col = ReactiveSeq.of(1, 2, 3, 4, 5)
.peek(System.out::println).toConcurrentLazyCollection();
col.forEach(System.out::println);
// Will print out "first!" before anything else
default Streamable<T> toConcurrentLazyStreamable()
Streamable<Integer> repeat = ReactiveSeq.of(1, 2, 3, 4, 5, 6)
.map(i ->; i + 2)
.toConcurrentLazyStreamable();
assertThat(repeat.sequenceM().toList(), equalTo(Arrays.asList(2, 4, 6, 8, 10, 12)));
assertThat(repeat.sequenceM().toList(), equalTo(Arrays.asList(2, 4, 6, 8, 10, 12)));
default T firstValue()
assertThat(ReactiveSeq.of(1,2,3,4)
.map(u->throw new RuntimeException())
.recover(e->"hello")
.firstValue(),equalTo("hello"));
default T single()
//1
ReactiveSeq.of(1).single();
//UnsupportedOperationException
ReactiveSeq.of().single();
//UnsupportedOperationException
ReactiveSeq.of(1,2,3).single();
default java.util.Optional<T> singleOptional()
//Optional[1]
ReactiveSeq.of(1).singleOptional();
//Optional.empty
ReactiveSeq.of().singleOpional();
//Optional.empty
ReactiveSeq.of(1,2,3).singleOptional();
default java.util.Optional<T> get(long index)
assertThat(ReactiveSeq.of(1,2,3,4,5).elementAt(2).get(),equalTo(3));
index
- to extract element fromdefault HotStream<T> schedule(java.lang.String cron, java.util.concurrent.ScheduledExecutorService ex)
//run at 8PM every night
ReactiveSeq.generate(()->"next job:"+formatDate(new Date()))
.map(this::processJob)
.schedule("0 20 * * *",Executors.newScheduledThreadPool(1));
Connect to the Scheduled Stream
HotStream<Data> dataStream = ReactiveSeq.generate(() -> "next job:" + formatDate(new Date())).map(this::processJob)
.schedule("0 20 * * *", Executors.newScheduledThreadPool(1));
data.connect().forEach(this::logToDB);
cron
- Expression that determines when each job will runex
- ScheduledExecutorServicedefault HotStream<T> scheduleFixedDelay(long delay, java.util.concurrent.ScheduledExecutorService ex)
//run every 60 seconds after last job completes
ReactiveSeq.generate(()->"next job:"+formatDate(new Date()))
.map(this::processJob)
.scheduleFixedDelay(60_000,Executors.newScheduledThreadPool(1));
Connect to the Scheduled Stream
HotStream<Data> dataStream = ReactiveSeq.generate(() -> "next job:" + formatDate(new Date())).map(this::processJob)
.scheduleFixedDelay(60_000, Executors.newScheduledThreadPool(1));
data.connect().forEach(this::logToDB);
delay
- Between last element completes passing through the Stream
until the next one startsex
- ScheduledExecutorServicedefault HotStream<T> scheduleFixedRate(long rate, java.util.concurrent.ScheduledExecutorService ex)
//run every 60 seconds
SequenceeM.generate(()->"next job:"+formatDate(new Date()))
.map(this::processJob)
.scheduleFixedRate(60_000,Executors.newScheduledThreadPool(1));
Connect to the Scheduled Stream
HotStream<Data> dataStream = ReactiveSeq.generate(() -> "next job:" + formatDate(new Date())).map(this::processJob)
.scheduleFixedRate(60_000, Executors.newScheduledThreadPool(1));
data.connect().forEach(this::logToDB);
rate
- Time in millis between job runsex
- ScheduledExecutorServicedefault <S,F> Ior<ReactiveSeq<F>,ReactiveSeq<S>> validate(Validator<T,S,F> validator)
Ior
validator
- Validator
to validate each element withdefault boolean xMatch(int num, java.util.function.Predicate<? super T> c)
assertTrue(ReactiveSeq.of(1,2,3,5,6,7).xMatch(3, i-> i>4 ));