public 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) |
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 HeadAndTail<T> |
headAndTail()
extract head and tail together, where head is expected to be present
|
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) |
default void |
print(java.io.PrintWriter writer) |
default void |
printErr() |
default void |
printOut() |
default java.util.Optional<T> |
reduce(java.util.function.BinaryOperator<T> accumulator) |
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)
ReactiveSeq.of("hello","2","world","4").reduce(Reducers.toString(","));
//hello,2,world,4
|
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) |
default <U> U |
reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator) |
default <U> U |
reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator,
java.util.function.BinaryOperator<U> combiner) |
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()
{
@code
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) |
default <R> R |
visit(java.util.function.BiFunction<? super Maybe<T>,? super ReactiveSeq<T>,? extends R> match) |
default <R> R |
visit(java.util.function.BiFunction<? super T,? super ReactiveSeq<T>,? extends R> match,
java.util.function.Supplier<? extends R> ifEmpty)
Destructures this Traversable into it's head and tail.
|
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()
default <R> R visit(java.util.function.BiFunction<? super T,? super ReactiveSeq<T>,? extends R> match, java.util.function.Supplier<? extends R> ifEmpty)
ListX.of(1,2,3,4,5,6,7,8,9)
.dropRight(5)
.plus(10)
.visit((x,xs) ->
xs.join(x.>2?"hello":"world")),()->"NIL"
);
//2world3world4
match
- default <R> R visit(java.util.function.BiFunction<? super Maybe<T>,? super ReactiveSeq<T>,? extends R> match)
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 <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator)
default <U> U reduce(U identity, java.util.function.BiFunction<U,? super T,U> accumulator, java.util.function.BinaryOperator<U> combiner)
default ListX<T> reduce(java.util.stream.Stream<? extends Monoid<T>> reducers)
{ @code 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
- accumulator
- default <U> U foldRight(U identity, java.util.function.BiFunction<? super T,? super U,? extends U> accumulator)
default <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)
default void print(java.io.PrintWriter writer)
default void printOut()
default void printErr()
default <K> MapX<K,java.util.List<T>> groupBy(java.util.function.Function<? super T,? extends K> classifier)
{ @code 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 HeadAndTail<T> headAndTail()
{ @code SequenceM<String> helloWorld = ReactiveSeq.of("hello", "world", "last"); HeadAndTail<String> headAndTail = helloWorld.headAndTail(); String head = headAndTail.head(); assertThat(head, equalTo("hello")); SequenceM<String> tail = headAndTail.tail(); assertThat(tail.headAndTail().head(), equalTo("world")); }
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)))
iterator
- default 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()
{ @code 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()
{ @code 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()
{ @code 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
SequenceeM.generate(()->"next job:"+formatDate(new Date()))
.map(this::processJob)
.schedule("0 20 * * *",Executors.newScheduledThreadPool(1));
Connect to the Scheduled Stream
{ @code 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
SequenceeM.generate(()->"next job:"+formatDate(new Date()))
.map(this::processJob)
.scheduleFixedDelay(60_000,Executors.newScheduledThreadPool(1));
Connect to the Scheduled Stream
{ @code HotStream<Data> dataStream = SequenceeM.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
{ @code HotStream<Data> dataStream = SequenceeM.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)
default 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 ));