public class SequenceM<T> extends java.lang.Object implements Unwrapable, java.util.stream.Stream<T>, java.lang.Iterable<T>
| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(java.util.function.Predicate<? super T> c)
True if predicate matches all elements when Monad converted to a Stream
|
AnyM<T> |
anyM() |
boolean |
anyMatch(java.util.function.Predicate<? super T> c)
True if a single element matches when Monad converted to a Stream
|
SequenceM<T> |
append(T... values)
Append values to the end of this SequenceM
|
SequenceM<T> |
appendStream(java.util.stream.Stream<T> stream)
Append Stream to this SequenceM
|
<U> SequenceM<U> |
cast(java.lang.Class<U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException. |
void |
close() |
<R,A> R |
collect(java.util.stream.Collector<? super T,A,R> collector)
Mutable reduction / collection over this Monad converted to a Stream
|
java.util.List |
collect(java.util.stream.Stream<java.util.stream.Collector> collectors)
Apply multiple collectors Simulataneously to this Monad
|
<R> R |
collect(java.util.function.Supplier<R> supplier,
java.util.function.BiConsumer<R,? super T> accumulator,
java.util.function.BiConsumer<R,R> combiner) |
<R> java.util.List<R> |
collectIterable(java.lang.Iterable<java.util.stream.Collector> collectors)
Apply multiple Collectors, simultaneously to a Stream
|
long |
count() |
SequenceM<T> |
cycle()
Convert to a Stream with the values infinitely cycled
|
<R> SequenceM<R> |
cycle(java.lang.Class<R> monadC,
int times)
Convert to a Stream, repeating the resulting structure specified times
and lifting all values to the specified Monad type
|
SequenceM<T> |
cycle(int times)
Convert to a Stream with the values repeated specified times
|
SequenceM<T> |
cycle(Monoid<T> m,
int times)
Convert to a Stream with the result of a reduction operation repeated
specified times
|
SequenceM<T> |
cycleUntil(java.util.function.Predicate<? super T> predicate)
Repeat in a Stream until specified predicate holds
|
SequenceM<T> |
cycleWhile(java.util.function.Predicate<? super T> predicate)
Repeat in a Stream while specified predicate holds
|
SequenceM<T> |
deleteBetween(int start,
int end)
Delete elements between given indexes in a Stream
|
SequenceM<T> |
distinct() |
Pair<SequenceM<T>,SequenceM<T>> |
duplicate()
Duplicate a Stream, buffers intermediate values, leaders may change positions so a limit
can be safely applied to the leading stream.
|
static <T> SequenceM<T> |
empty() |
SequenceM<T> |
filter(java.util.function.Predicate<? super T> fn) |
java.util.Optional<T> |
findAny() |
java.util.Optional<T> |
findFirst() |
<R> SequenceM<R> |
flatMap(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> fn)
flatMap operation
|
<R> SequenceM<R> |
flatMapAnyM(java.util.function.Function<? super T,AnyM<? extends R>> fn) |
<R> SequenceM<R> |
flatMapCollection(java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
Convenience method & performance optimisation
flatMapping to a Stream will result in the Stream being converted to a List, if the host Monad
type is not a Stream.
|
<R> SequenceM<R> |
flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn) |
<R> SequenceM<R> |
flatMapLazySeq(java.util.function.Function<? super T,com.nurkiewicz.lazyseq.LazySeq<? extends R>> fn) |
<R> SequenceM<R> |
flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn) |
<R> SequenceM<R> |
flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn) |
java.util.stream.DoubleStream |
flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper) |
java.util.stream.IntStream |
flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper) |
java.util.stream.LongStream |
flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper) |
<T1> SequenceM<T1> |
flatten()
join / flatten one level of a nested hierarchy
|
T |
foldLeft(Monoid<T> reducer) |
T |
foldLeft(T identity,
java.util.function.BinaryOperator<T> accumulator) |
<T> T |
foldLeftMapToType(Monoid<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
|
T |
foldRight(Monoid<T> reducer) |
T |
foldRight(T identity,
java.util.function.BinaryOperator<T> accumulator) |
<T> T |
foldRightMapToType(Monoid<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
|
void |
forEach(java.util.function.Consumer<? super T> action) |
void |
forEachOrdered(java.util.function.Consumer<? super T> action) |
static <T> SequenceM<T> |
fromIterable(java.lang.Iterable<T> iterable)
Construct a Sequence from an Iterable
|
static <T> SequenceM<T> |
fromStream(java.util.stream.Stream<T> stream)
Construct a Sequence from a Stream
|
<K> java.util.Map<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
|
SequenceM<java.util.List<T>> |
grouped(int groupSize)
Group elements in a Stream
|
HeadAndTail<T> |
headAndTail()
extract head and tail together
|
java.util.Optional<HeadAndTail<T>> |
headAndTailOptional() |
SequenceM<T> |
insertAt(int pos,
T... values)
Insert data into a stream at given position
|
SequenceM<T> |
insertStreamAt(int pos,
SequenceM<T> stream)
Insert a Stream into the middle of this stream at the specified position
|
SequenceM<T> |
intersperse(T value)
Returns a stream with a given value interspersed between any two values
of this stream.
|
boolean |
isParallel() |
java.util.Iterator<T> |
iterator() |
java.lang.String |
join()
assertEquals("123".length(),of(1, 2, 3).join().length());
|
java.lang.String |
join(java.lang.String sep)
assertEquals("1, 2, 3".length(), of(1, 2, 3).join(", ").length());
|
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());
|
SequenceM<java.lang.String> |
liftAndBindBufferedReader(java.util.function.Function<? super T,java.io.BufferedReader> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied BufferedReaders
|
SequenceM<java.lang.Character> |
liftAndBindCharSequence(java.util.function.Function<? super T,java.lang.CharSequence> fn)
Perform a flatMap operation where the result will be a flattened stream of Characters
from the CharSequence returned by the supplied function.
|
SequenceM<java.lang.String> |
liftAndBindFile(java.util.function.Function<? super T,java.io.File> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied files.
|
SequenceM<java.lang.String> |
liftAndBindURL(java.util.function.Function<? super T,java.net.URL> fn)
Perform a flatMap operation where the result will be a flattened stream of Strings
from the text loaded from the supplied URLs
|
SequenceM<T> |
limit(long num)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().limit(2).toList(),equalTo(Arrays.asList(4,3))); |
SequenceM<T> |
limitUntil(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).limitUntil(i->i==6).toList(),equalTo(Arrays.asList(4,3))); |
SequenceM<T> |
limitWhile(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().sorted().limitWhile(i->i<6).toList(),equalTo(Arrays.asList(3,4))); |
<R> SequenceM<R> |
map(java.util.function.Function<? super T,? extends R> fn) |
<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
|
<R> R |
mapReduce(Monoid<R> 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
|
java.util.stream.DoubleStream |
mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper) |
java.util.stream.IntStream |
mapToInt(java.util.function.ToIntFunction<? super T> mapper) |
java.util.stream.LongStream |
mapToLong(java.util.function.ToLongFunction<? super T> mapper) |
java.util.Optional<T> |
max(java.util.Comparator<? super T> comparator) |
<C extends java.lang.Comparable<? super C>> |
maxBy(java.util.function.Function<T,C> f) |
java.util.Optional<T> |
min(java.util.Comparator<? super T> comparator) |
<C extends java.lang.Comparable<? super C>> |
minBy(java.util.function.Function<T,C> f)
Extract the minimum as determined by supplied function
|
boolean |
noneMatch(java.util.function.Predicate<? super T> c) |
static <T> SequenceM<T> |
of(T... elements)
Construct a Sequence from the provided elements
|
<U> SequenceM<U> |
ofType(java.lang.Class<U> type)
Keep only those elements in a stream that are of a given type.
|
java.util.stream.Stream<T> |
onClose(java.lang.Runnable closeHandler) |
SequenceM<T> |
parallel() |
Pair<SequenceM<T>,SequenceM<T>> |
partition(java.util.function.Predicate<T> splitter)
Partition a Stream into two one a per element basis, based on predicate's boolean value
|
SequenceM<T> |
peek(java.util.function.Consumer<? super T> c) |
SequenceM<T> |
prepend(T... values)
Prepend given values to the start of the Stream
|
SequenceM<T> |
prependStream(java.util.stream.Stream<T> stream)
Prepend Stream to this SequenceM
|
Quadruple<SequenceM<T>,SequenceM<T>,SequenceM<T>,SequenceM<T>> |
quadruplicate()
Makes four copies of a Stream
Buffers intermediate values, leaders may change positions so a limit
can be safely applied to the leading stream.
|
java.util.Optional<T> |
reduce(java.util.function.BinaryOperator<T> accumulator) |
java.util.List<T> |
reduce(java.lang.Iterable<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.
|
T |
reduce(Monoid<T> reducer) |
java.util.List<T> |
reduce(java.util.stream.Stream<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.
|
T |
reduce(T identity,
java.util.function.BinaryOperator<T> accumulator) |
<U> U |
reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator,
java.util.function.BinaryOperator<U> combiner) |
SequenceM<T> |
reverse() |
static <T> SequenceM<T> |
reversedListOf(java.util.List<T> elements)
Construct a Reveresed Sequence from the provided elements
|
static <T> SequenceM<T> |
reversedOf(T... elements)
Construct a Reveresed Sequence from the provided elements
|
SequenceM<T> |
scanLeft(Monoid<T> monoid)
Scan left using supplied Monoid
|
SequenceM<T> |
scanLeft(T identity,
java.util.function.BiFunction<T,T,T> combiner)
Scan left
|
SequenceM<T> |
scanRight(Monoid<T> monoid)
Scan right
|
<U> SequenceM<T> |
scanRight(T identity,
java.util.function.BiFunction<T,T,T> combiner)
Scan right
|
SequenceM<T> |
sequential() |
SequenceM<T> |
shuffle() |
SequenceM<T> |
skip(long num)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().skip(2).toList(),equalTo(Arrays.asList(6,7))); |
SequenceM<T> |
skipUntil(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().skipUntil(i->i==6).toList(),equalTo(Arrays.asList(6,7))); |
SequenceM<T> |
skipWhile(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().sorted().skipWhile(i->i<6).toList(),equalTo(Arrays.asList(6,7)));
|
SequenceM<java.util.List<T>> |
sliding(int windowSize)
Create a sliding view over this Sequence
|
SequenceM<java.util.List<T>> |
sliding(int windowSize,
int increment)
Create a sliding view over this Sequence
|
SequenceM<T> |
sorted()
assertThat(SequenceM.of(4,3,6,7)).sorted().toList(),equalTo(Arrays.asList(3,4,6,7))); |
SequenceM<T> |
sorted(java.util.Comparator<? super T> c)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().sorted((a,b) -> b-a).toList(),equalTo(Arrays.asList(7,6,4,3)));
|
Pair<SequenceM<T>,SequenceM<T>> |
splitAt(int where)
Split at supplied location
|
Pair<java.util.Optional<T>,SequenceM<T>> |
splitAtHead()
Split a Stream at it's head (similar to headAndTail)
|
Pair<SequenceM<T>,SequenceM<T>> |
splitBy(java.util.function.Predicate<T> splitter)
Split stream at point where predicate no longer holds
|
java.util.Spliterator<T> |
spliterator() |
boolean |
startsWith(java.lang.Iterable<T> iterable)
assertTrue(monad(Stream.of(1,2,3,4)).startsWith(Arrays.asList(1,2,3)));
|
boolean |
startsWith(java.util.Iterator<T> iterator)
assertTrue(monad(Stream.of(1,2,3,4)).startsWith(Arrays.asList(1,2,3).iterator())) |
java.util.stream.Stream<T> |
stream()
Unwrap this Monad into a Stream.
|
java.lang.Object[] |
toArray() |
<A> A[] |
toArray(java.util.function.IntFunction<A[]> generator) |
<C extends java.util.Collection<T>> |
toCollection(java.util.function.Supplier<C> collectionFactory) |
java.util.Collection<T> |
toConcurrentLazyCollection()
Lazily converts this SequenceM into a Collection.
|
Streamable<T> |
toConcurrentLazyStreamable() |
java.util.Collection<T> |
toLazyCollection()
Lazily converts this SequenceM into a Collection.
|
Streamable<T> |
toLazyStreamable() |
java.util.List<T> |
toList() |
java.util.Set<T> |
toSet() |
<T> java.util.stream.Stream<T> |
toStream() |
Streamable<T> |
toStreamable() |
Triple<SequenceM<T>,SequenceM<T>,SequenceM<T>> |
triplicate()
Triplicates a Stream
Buffers intermediate values, leaders may change positions so a limit
can be safely applied to the leading stream.
|
SequenceM<T> |
unordered() |
<R> R |
unwrap() |
java.util.concurrent.CompletableFuture<java.util.List<T>> |
unwrapCompletableFuture()
CompletableFuture<List<String>> cf = anyM("hello","world")
.asSequence()
.unwrapCompletableFuture();
assertThat(cf.join(),equalTo(Arrays.asList("hello","world")));
|
java.util.Optional<java.util.List<T>> |
unwrapOptional()
Type safe unwrap
|
java.util.stream.Stream<T> |
unwrapStream()
Type safe unwrap
|
static <T,U> Pair<SequenceM<T>,SequenceM<U>> |
unzip(SequenceM<Pair<T,U>> sequence)
Unzip a zipped Stream
|
static <T1,T2,T3> Triple<SequenceM<T1>,SequenceM<T2>,SequenceM<T3>> |
unzip3(SequenceM<Triple<T1,T2,T3>> sequence)
Unzip a zipped Stream into 3
|
static <T1,T2,T3,T4> |
unzip4(SequenceM<Quadruple<T1,T2,T3,T4>> sequence)
Unzip a zipped Stream into 4
|
boolean |
xMatch(int num,
java.util.function.Predicate<? super T> c)
Check that there are specified number of matches of predicate in the Stream
|
<S,R> SequenceM<R> |
zip(AnyM<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Zip this SequenceM against any monad type.
|
<S,R> SequenceM<R> |
zip(SequenceM<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Generic zip function.
|
<S> SequenceM<Pair<T,S>> |
zip(java.util.stream.Stream<? extends S> second)
Zip 2 streams into one
|
<S,U> SequenceM<Triple<T,S,U>> |
zip3(java.util.stream.Stream<? extends S> second,
java.util.stream.Stream<? extends U> third)
zip 3 Streams into one
|
<T2,T3,T4> SequenceM<Quadruple<T,T2,T3,T4>> |
zip4(java.util.stream.Stream<T2> second,
java.util.stream.Stream<T3> third,
java.util.stream.Stream<T4> fourth)
zip 4 Streams into 1
|
<S,R> SequenceM<R> |
zipStream(java.util.stream.BaseStream<? extends S,? extends java.util.stream.BaseStream<? extends S,?>> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Zip this Monad with a Stream
|
SequenceM<Pair<T,java.lang.Long>> |
zipWithIndex()
Add an index to the current Stream
|
public final <R> R unwrap()
unwrap in interface Unwrapablepublic final <T1> SequenceM<T1> flatten()
assertThat(SequenceM.<Integer>of(Arrays.asList(1,2)).flatten().toList().size(),equalTo(asList(1, 2).size()));
// or more advanced example
AnyM<AnyM<Integer>> applied =anyM(Optional.of(2))
.simpleFilter(anyM(Streamable.of( (Integer a)->a>5 ,(Integer a) -> a<3)));
assertThat(applied.toSequence().flatten().toList(),equalTo(Arrays.asList(2)));
public final java.util.stream.Stream<T> unwrapStream()
Stream<String> stream = anyM("hello","world").asSequence().unwrapStream();
assertThat(stream.collect(Collectors.toList()),equalTo(Arrays.asList("hello","world")));
public final java.util.Optional<java.util.List<T>> unwrapOptional()
Optional<List<String>> stream = anyM("hello","world")
.asSequence()
.unwrapOptional();
assertThat(stream.get(),equalTo(Arrays.asList("hello","world")));
public final java.util.concurrent.CompletableFuture<java.util.List<T>> unwrapCompletableFuture()
CompletableFuture<List<String>> cf = anyM("hello","world")
.asSequence()
.unwrapCompletableFuture();
assertThat(cf.join(),equalTo(Arrays.asList("hello","world")));
public final SequenceM<T> cycle(int times)
assertThat(anyM(Stream.of(1,2,2)).asSequence()
.cycle(3).collect(Collectors.toList()),
equalTo(Arrays.asList(1,2,2,1,2,2,1,2,2)));
times - Times values should be repeated within a Streampublic final SequenceM<T> cycle()
assertEquals(asList(1, 1, 1, 1, 1,1),of(1).cycle().limit(6).toList());
public final Pair<SequenceM<T>,SequenceM<T>> duplicate()
Pair<SequenceM<Integer>, SequenceM<Integer>> copies =of(1,2,3,4,5,6).duplicate();
assertTrue(copies.v1.anyMatch(i->i==2));
assertTrue(copies.v2.anyMatch(i->i==2));
public final Triple<SequenceM<T>,SequenceM<T>,SequenceM<T>> triplicate()
Triple<SequenceM<Triple<T1,T2,T3>>,SequenceM<Triple<T1,T2,T3>>,SequenceM<Triple<T1,T2,T3>>> triple = sequence.triplicate();
public final Quadruple<SequenceM<T>,SequenceM<T>,SequenceM<T>,SequenceM<T>> quadruplicate()
Quadruple<SequenceM<Quadruple<T1,T2,T3,T4>>,SequenceM<Quadruple<T1,T2,T3,T4>>,SequenceM<Quadruple<T1,T2,T3,T4>>,SequenceM<Quadruple<T1,T2,T3,T4>>> quad = sequence.quadruplicate();
public final Pair<java.util.Optional<T>,SequenceM<T>> splitAtHead()
SequenceM.of(1,2,3).splitAtHead()
//Optional[1], SequenceM[2,3]
public final Pair<SequenceM<T>,SequenceM<T>> splitAt(int where)
SequenceM.of(1,2,3).splitAt(1)
//SequenceM[1], SequenceM[2,3]
public final Pair<SequenceM<T>,SequenceM<T>> splitBy(java.util.function.Predicate<T> splitter)
SequenceM.of(1, 2, 3, 4, 5, 6).splitBy(i->i<4)
//SequenceM[1,2,3] SequenceM[4,5,6]
public final Pair<SequenceM<T>,SequenceM<T>> partition(java.util.function.Predicate<T> splitter)
SequenceM.of(1, 2, 3, 4, 5, 6).partition(i -> i % 2 != 0)
//SequenceM[1,3,5], SequenceM[2,4,6]
public static final <T,U> Pair<SequenceM<T>,SequenceM<U>> unzip(SequenceM<Pair<T,U>> sequence)
unzip(SequenceM.of(new Pair(1, "a"), new Pair(2, "b"), new Pair(3, "c")))
// SequenceM[1,2,3], SequenceM[a,b,c]
public static final <T1,T2,T3> Triple<SequenceM<T1>,SequenceM<T2>,SequenceM<T3>> unzip3(SequenceM<Triple<T1,T2,T3>> sequence)
unzip3(SequenceM.of(new Triple(1, "a", 2l), new Triple(2, "b", 3l), new Triple(3,"c", 4l)))
// SequenceM[1,2,3], SequenceM[a,b,c], SequenceM[2l,3l,4l]
public static final <T1,T2,T3,T4> Quadruple<SequenceM<T1>,SequenceM<T2>,SequenceM<T3>,SequenceM<T4>> unzip4(SequenceM<Quadruple<T1,T2,T3,T4>> sequence)
unzip4(SequenceM.of(new Quadruple(1, "a", 2l,'z'), new Quadruple(2, "b", 3l,'y'), new Quadruple(3,
"c", 4l,'x')));
// SequenceM[1,2,3], SequenceM[a,b,c], SequenceM[2l,3l,4l], SequenceM[z,y,x]
public final SequenceM<T> cycle(Monoid<T> m, int times)
List<Integer> list = AsGenericMonad,asMonad(Stream.of(1,2,2))
.cycle(Reducers.toCountInt(),3)
.collect(Collectors.toList());
//is asList(3,3,3);
m - Monoid to be used in reductiontimes - Number of times value should be repeatedpublic final <R> SequenceM<R> cycle(java.lang.Class<R> monadC, int times)
{
@code
List<Optional<Integer>> list = monad(Stream.of(1, 2)).cycle(Optional.class,
2).toList();
// is asList(Optional.of(1),Optional.of(2),Optional.of(1),Optional.of(2) ));
}
monadC - class typetimes - public final SequenceM<T> cycleWhile(java.util.function.Predicate<? super T> predicate)
count =0;
assertThat(anyM(Stream.of(1,2,2)).asSequence()
.cycleWhile(next -> count++<6)
.collect(Collectors.toList()),equalTo(Arrays.asList(1,2,2,1,2,2)));
predicate - repeat while truepublic final SequenceM<T> cycleUntil(java.util.function.Predicate<? super T> predicate)
count =0;
assertThat(anyM(Stream.of(1,2,2)).asSequence()
.cycleUntil(next -> count++>6)
.collect(Collectors.toList()),equalTo(Arrays.asList(1,2,2,1,2,2,1)));
predicate - repeat while truepublic final <S> SequenceM<Pair<T,S>> zip(java.util.stream.Stream<? extends S> second)
List<Pair<Integer, String>> list = of(1, 2).zip(of("a", "b", "c", "d")).toList();
// [[1,"a"],[2,"b"]]
public final <S,U> SequenceM<Triple<T,S,U>> zip3(java.util.stream.Stream<? extends S> second, java.util.stream.Stream<? extends U> third)
List<Triple<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']]
public final <T2,T3,T4> SequenceM<Quadruple<T,T2,T3,T4>> zip4(java.util.stream.Stream<T2> second, java.util.stream.Stream<T3> third, java.util.stream.Stream<T4> fourth)
List<Quadruple<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"]]
public final SequenceM<Pair<T,java.lang.Long>> zipWithIndex()
assertEquals(asList(new Pair("a", 0L), new Pair("b", 1L)), of("a", "b").zipWithIndex().toList());
public final <S,R> SequenceM<R> zip(SequenceM<? extends S> second, java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
{
@code
Stream<List<Integer>> zipped = asMonad(Stream.of(1, 2, 3)).zip(
asMonad(Optional.of(2)), (a, b) -> Arrays.asList(a, b));
// [[1,2]]
}
second - Monad to zip withzipper - Zipping functionpublic final <S,R> SequenceM<R> zip(AnyM<? extends S> second, java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Stream<List<Integer>> zipped = anyM(Stream.of(1,2,3))
.asSequence()
.zip(anyM(Optional.of(2)),
(a,b) -> Arrays.asList(a,b)).toStream();
List<Integer> zip = zipped.collect(Collectors.toList()).get(0);
assertThat(zip.get(0),equalTo(1));
assertThat(zip.get(1),equalTo(2));
public final <S,R> SequenceM<R> zipStream(java.util.stream.BaseStream<? extends S,? extends java.util.stream.BaseStream<? extends S,?>> second, java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
{
@code
Stream<List<Integer>> zipped = asMonad(Stream.of(1, 2, 3)).zip(
Stream.of(2, 3, 4), (a, b) -> Arrays.asList(a, b));
// [[1,2][2,3][3,4]]
}
second - Stream to zip withzipper - Zip funcitonpublic final SequenceM<java.util.List<T>> sliding(int windowSize)
List<List<Integer>> list = anyM(Stream.of(1,2,3,4,5,6))
.asSequence()
.sliding(2)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2));
assertThat(list.get(1),hasItems(2,3));
windowSize - Size of sliding windowpublic final SequenceM<java.util.List<T>> sliding(int windowSize, int increment)
List<List<Integer>> list = anyM(Stream.of(1,2,3,4,5,6))
.asSequence()
.sliding(3,2)
.collect(Collectors.toList());
assertThat(list.get(0),hasItems(1,2,3));
assertThat(list.get(1),hasItems(3,4,5));
windowSize - number of elements in each batchincrement - for each windowpublic final SequenceM<java.util.List<T>> grouped(int groupSize)
{
@code
List<List<Integer>> list = monad(Stream.of(1, 2, 3, 4, 5, 6)).grouped(3)
.collect(Collectors.toList());
assertThat(list.get(0), hasItems(1, 2, 3));
assertThat(list.get(1), hasItems(4, 5, 6));
}
groupSize - Size of each Grouppublic final <K> java.util.Map<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());
public final SequenceM<T> distinct()
distinct in interface java.util.stream.Stream<T>public final SequenceM<T> scanLeft(Monoid<T> monoid)
assertEquals(asList("", "a", "ab", "abc"),monad(Stream.of("a", "b", "c")).scanLeft(Reducers.toString("")).toList());
monoid - public final SequenceM<T> scanLeft(T identity, java.util.function.BiFunction<T,T,T> combiner)
assertThat(of("a", "b", "c").scanLeft("", String::concat).toList().size(),
is(4));
public final SequenceM<T> scanRight(Monoid<T> monoid)
assertThat(of("a", "b", "c").scanRight(Monoid.of("", String::concat)).toList().size(),
is(asList("", "c", "bc", "abc").size()));
public final <U> SequenceM<T> scanRight(T identity, java.util.function.BiFunction<T,T,T> combiner)
assertThat(of("a", "ab", "abc").map(str->str.length()).scanRight(0, (t, u) -> u + t).toList().size(),
is(asList(0, 3, 5, 6).size()));
public final SequenceM<T> sorted()
assertThat(SequenceM.of(4,3,6,7)).sorted().toList(),equalTo(Arrays.asList(3,4,6,7)));
sorted in interface java.util.stream.Stream<T>public final SequenceM<T> sorted(java.util.Comparator<? super T> c)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().sorted((a,b) -> b-a).toList(),equalTo(Arrays.asList(7,6,4,3)));
sorted in interface java.util.stream.Stream<T>c - Compartor to sort withpublic final SequenceM<T> skip(long num)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().skip(2).toList(),equalTo(Arrays.asList(6,7)));
skip in interface java.util.stream.Stream<T>num - Number of elemenets to skippublic final SequenceM<T> skipWhile(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().sorted().skipWhile(i->i<6).toList(),equalTo(Arrays.asList(6,7)));
p - Predicate to skip while truepublic final SequenceM<T> skipUntil(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().skipUntil(i->i==6).toList(),equalTo(Arrays.asList(6,7)));
p - Predicate to skip until truepublic final SequenceM<T> limit(long num)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().limit(2).toList(),equalTo(Arrays.asList(4,3)));
limit in interface java.util.stream.Stream<T>num - Limit element size to numpublic final SequenceM<T> limitWhile(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().sorted().limitWhile(i->i<6).toList(),equalTo(Arrays.asList(3,4)));
p - Limit while predicate is truepublic final SequenceM<T> limitUntil(java.util.function.Predicate<? super T> p)
assertThat(anyM(Stream.of(4,3,6,7)).limitUntil(i->i==6).toList(),equalTo(Arrays.asList(4,3)));
p - Limit until predicate is truepublic final boolean allMatch(java.util.function.Predicate<? super T> c)
assertThat(of(1,2,3,4,5).allMatch(it-> it>0 && it <6),equalTo(true));
allMatch in interface java.util.stream.Stream<T>c - Predicate to check if all matchpublic final boolean anyMatch(java.util.function.Predicate<? super T> c)
assertThat(of(1,2,3,4,5).anyMatch(it-> it.equals(3)),equalTo(true));
anyMatch in interface java.util.stream.Stream<T>c - Predicate to check if any matchpublic boolean xMatch(int num,
java.util.function.Predicate<? super T> c)
assertTrue(SequenceM.of(1,2,3,5,6,7).xMatch(3, i-> i>4 ));
public final boolean noneMatch(java.util.function.Predicate<? super T> c)
noneMatch in interface java.util.stream.Stream<T>public final java.lang.String join()
assertEquals("123".length(),of(1, 2, 3).join().length());
public final java.lang.String join(java.lang.String sep)
assertEquals("1, 2, 3".length(), of(1, 2, 3).join(", ").length());
public final 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());
public final <C extends java.lang.Comparable<? super C>> java.util.Optional<T> minBy(java.util.function.Function<T,C> f)
public final java.util.Optional<T> min(java.util.Comparator<? super T> comparator)
min in interface java.util.stream.Stream<T>public final <C extends java.lang.Comparable<? super C>> java.util.Optional<T> maxBy(java.util.function.Function<T,C> f)
public final java.util.Optional<T> max(java.util.Comparator<? super T> comparator)
max in interface java.util.stream.Stream<T>public final HeadAndTail<T> headAndTail()
SequenceM<String> helloWorld = SequenceM.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"));
public final java.util.Optional<HeadAndTail<T>> headAndTailOptional()
public final java.util.Optional<T> findFirst()
findFirst in interface java.util.stream.Stream<T>public final java.util.Optional<T> findAny()
findAny in interface java.util.stream.Stream<T>public final <R> R mapReduce(Monoid<R> reducer)
reducer - Monoid to reduce valuespublic final <R> R mapReduce(java.util.function.Function<? super T,? extends R> mapper, Monoid<R> reducer)
mapper - Function to map Monad typereducer - Monoid to reduce valuespublic final <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
collect in interface java.util.stream.Stream<T>collector - Collection operation definitionpublic final <R> R collect(java.util.function.Supplier<R> supplier,
java.util.function.BiConsumer<R,? super T> accumulator,
java.util.function.BiConsumer<R,R> combiner)
collect in interface java.util.stream.Stream<T>public final java.util.List collect(java.util.stream.Stream<java.util.stream.Collector> collectors)
List result =SequenceM.of(1,2,3).collect(Stream.of(Collectors.toList(),
Collectors.summingInt(Integer::intValue),
Collectors.averagingInt(Integer::intValue)));
assertThat(result.get(0),equalTo(Arrays.asList(1,2,3)));
assertThat(result.get(1),equalTo(6));
assertThat(result.get(2),equalTo(2.0));
collectors - Stream of Collectors to applypublic <R> java.util.List<R> collectIterable(java.lang.Iterable<java.util.stream.Collector> collectors)
List result = SequenceM.of(1,2,3).collect(
Arrays.asList(Collectors.toList(),
Collectors.summingInt(Integer::intValue),
Collectors.averagingInt(Integer::intValue)));
assertThat(result.get(0),equalTo(Arrays.asList(1,2,3)));
assertThat(result.get(1),equalTo(6));
assertThat(result.get(2),equalTo(2.0));
stream - Stream to collectcollectors - Collectors to applypublic final T reduce(Monoid<T> reducer)
reducer - Use supplied Monoid to reduce valuespublic final java.util.Optional<T> reduce(java.util.function.BinaryOperator<T> accumulator)
reduce in interface java.util.stream.Stream<T>public final T reduce(T identity, java.util.function.BinaryOperator<T> accumulator)
reduce in interface java.util.stream.Stream<T>public final <U> U reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator,
java.util.function.BinaryOperator<U> combiner)
reduce in interface java.util.stream.Stream<T>public final java.util.List<T> reduce(java.util.stream.Stream<Monoid<T>> reducers)
reducers - public final java.util.List<T> reduce(java.lang.Iterable<Monoid<T>> reducers)
reducers - public final T foldLeft(Monoid<T> reducer)
reducer - Use supplied Monoid to reduce values starting via foldLeftpublic final <T> T foldLeftMapToType(Monoid<T> reducer)
reducer - Monoid to reduce valuespublic final T foldRight(Monoid<T> reducer)
reducer - Use supplied Monoid to reduce values starting via foldRightpublic final <T> T foldRightMapToType(Monoid<T> reducer)
reducer - Monoid to reduce valuespublic final Streamable<T> toStreamable()
public final java.util.Set<T> toSet()
public final java.util.List<T> toList()
public final <C extends java.util.Collection<T>> C toCollection(java.util.function.Supplier<C> collectionFactory)
public final <T> java.util.stream.Stream<T> toStream()
public final java.util.stream.Stream<T> stream()
public final boolean startsWith(java.lang.Iterable<T> iterable)
assertTrue(monad(Stream.of(1,2,3,4)).startsWith(Arrays.asList(1,2,3)));
iterable - public final boolean startsWith(java.util.Iterator<T> iterator)
assertTrue(monad(Stream.of(1,2,3,4)).startsWith(Arrays.asList(1,2,3).iterator())) iterator - public final <R> SequenceM<R> map(java.util.function.Function<? super T,? extends R> fn)
map in interface java.util.stream.Stream<T>public final SequenceM<T> peek(java.util.function.Consumer<? super T> c)
peek in interface java.util.stream.Stream<T>public final <R> SequenceM<R> flatMap(java.util.function.Function<? super T,? extends java.util.stream.Stream<? extends R>> fn)
flatMap in interface java.util.stream.Stream<T>fn - public final <R> SequenceM<R> flatMapAnyM(java.util.function.Function<? super T,AnyM<? extends R>> fn)
public final <R> SequenceM<R> flatMapCollection(java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
AnyM<Integer> opt = anyM(Optional.of(20));
Optional<List<Integer>> optionalList = opt.flatMap( i -> anyM(Stream.of(1,2,i))).unwrap();
//Optional [1,2,20]
In such cases using Arrays.asList would be more performant
AnyM<Integer> opt = anyM(Optional.of(20));
Optional<List<Integer>> optionalList = opt.flatMapCollection( i -> asList(1,2,i))).unwrap();
//Optional [1,2,20]
fn - public final <R> SequenceM<R> flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
public final <R> SequenceM<R> flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
public final <R> SequenceM<R> flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
public final <R> SequenceM<R> flatMapLazySeq(java.util.function.Function<? super T,com.nurkiewicz.lazyseq.LazySeq<? extends R>> fn)
public final SequenceM<java.lang.Character> liftAndBindCharSequence(java.util.function.Function<? super T,java.lang.CharSequence> fn)
List<Character> result = anyM("input.file")
.asSequence()
.liftAndBindCharSequence(i->"hello world")
.toList();
assertThat(result,equalTo(Arrays.asList('h','e','l','l','o',' ','w','o','r','l','d')));
fn - public final SequenceM<java.lang.String> liftAndBindFile(java.util.function.Function<? super T,java.io.File> fn)
List<String> result = anyM("input.file")
.asSequence()
.map(getClass().getClassLoader()::getResource)
.peek(System.out::println)
.map(URL::getFile)
.liftAndBindFile(File::new)
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
fn - public final SequenceM<java.lang.String> liftAndBindURL(java.util.function.Function<? super T,java.net.URL> fn)
List<String> result = anyM("input.file")
.asSequence()
.liftAndBindURL(getClass().getClassLoader()::getResource)
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
fn - public final SequenceM<java.lang.String> liftAndBindBufferedReader(java.util.function.Function<? super T,java.io.BufferedReader> fn)
Listresult = anyM("input.file") .asSequence() .map(getClass().getClassLoader()::getResourceAsStream) .map(InputStreamReader::new) .liftAndBindBufferedReader(BufferedReader::new) .toList(); assertThat(result,equalTo(Arrays.asList("hello","world")));
fn - public final SequenceM<T> filter(java.util.function.Predicate<? super T> fn)
filter in interface java.util.stream.Stream<T>public void forEach(java.util.function.Consumer<? super T> action)
public java.util.Iterator<T> iterator()
public java.util.Spliterator<T> spliterator()
public boolean isParallel()
public java.util.stream.IntStream mapToInt(java.util.function.ToIntFunction<? super T> mapper)
mapToInt in interface java.util.stream.Stream<T>public java.util.stream.LongStream mapToLong(java.util.function.ToLongFunction<? super T> mapper)
mapToLong in interface java.util.stream.Stream<T>public java.util.stream.DoubleStream mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
mapToDouble in interface java.util.stream.Stream<T>public java.util.stream.IntStream flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper)
flatMapToInt in interface java.util.stream.Stream<T>public java.util.stream.LongStream flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper)
flatMapToLong in interface java.util.stream.Stream<T>public java.util.stream.DoubleStream flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper)
flatMapToDouble in interface java.util.stream.Stream<T>public void forEachOrdered(java.util.function.Consumer<? super T> action)
forEachOrdered in interface java.util.stream.Stream<T>public java.lang.Object[] toArray()
toArray in interface java.util.stream.Stream<T>public <A> A[] toArray(java.util.function.IntFunction<A[]> generator)
toArray in interface java.util.stream.Stream<T>public long count()
count in interface java.util.stream.Stream<T>public SequenceM<T> intersperse(T value)
public <U> SequenceM<U> ofType(java.lang.Class<U> type)
public <U> SequenceM<U> cast(java.lang.Class<U> type)
ClassCastException.
// ClassCastException SequenceM.of(1, "a", 2, "b", 3).cast(Integer.class)public java.util.Collection<T> toLazyCollection()
Collection<Integer> col = SequenceM.of(1,2,3,4,5)
.peek(System.out::println)
.toLazyCollection();
System.out.println("first!");
col.forEach(System.out::println);
//Will print out "first!" before anything else
public java.util.Collection<T> toConcurrentLazyCollection()
Collection<Integer> col = SequenceM.of(1,2,3,4,5)
.peek(System.out::println)
.toConcurrentLazyCollection();
System.out.println("first!");
col.forEach(System.out::println);
//Will print out "first!" before anything else
public Streamable<T> toLazyStreamable()
public Streamable<T> toConcurrentLazyStreamable()
public static <T> SequenceM<T> of(T... elements)
of in interface java.util.stream.Stream<T>elements - To Construct sequence frompublic static <T> SequenceM<T> reversedOf(T... elements)
elements - To Construct sequence frompublic static <T> SequenceM<T> reversedListOf(java.util.List<T> elements)
elements - To Construct sequence frompublic static <T> SequenceM<T> fromStream(java.util.stream.Stream<T> stream)
stream - Stream to construct Sequence frompublic static <T> SequenceM<T> fromIterable(java.lang.Iterable<T> iterable)
iterable - to construct Sequence frompublic java.util.stream.Stream<T> onClose(java.lang.Runnable closeHandler)
public void close()
public static <T> SequenceM<T> empty()
empty in interface java.util.stream.Stream<T>public SequenceM<T> appendStream(java.util.stream.Stream<T> stream)
List<String> result = of(1,2,3).appendStream(of(100,200,300))
.map(it ->it+"!!")
.collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","2!!","3!!","100!!","200!!","300!!")));
stream - to appendpublic SequenceM<T> prependStream(java.util.stream.Stream<T> stream)
List<String> result = of(1,2,3).prependStream(of(100,200,300))
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("100!!","200!!","300!!","1!!","2!!","3!!")));
stream - to Prependpublic SequenceM<T> append(T... values)
List<String> result = of(1,2,3).append(100,200,300)
.map(it ->it+"!!")
.collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","2!!","3!!","100!!","200!!","300!!")));
values - to appendpublic SequenceM<T> prepend(T... values)
List<String> result = of(1,2,3).prepend(100,200,300)
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("100!!","200!!","300!!","1!!","2!!","3!!")));
values - to prependpublic SequenceM<T> insertAt(int pos, T... values)
List<String> result = of(1,2,3).insertAt(1,100,200,300)
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","100!!","200!!","300!!","2!!","3!!")));
pos - to insert data atvalues - to insertpublic SequenceM<T> deleteBetween(int start, int end)
List<String> result = of(1,2,3,4,5,6).deleteBetween(2,4)
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","2!!","5!!","6!!")));
start - indexend - indexpublic SequenceM<T> insertStreamAt(int pos, SequenceM<T> stream)
List<String> result = of(1,2,3).insertStreamAt(1,of(100,200,300))
.map(it ->it+"!!").collect(Collectors.toList());
assertThat(result,equalTo(Arrays.asList("1!!","100!!","200!!","300!!","2!!","3!!")));
pos - to insert Stream atstream - to insert