public class SequenceM<T> extends java.lang.Object implements Unwrapable
| 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
|
<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) |
long |
count() |
<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> |
distinct() |
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,SequenceM<? 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) |
T |
foldLeft(Monoid<T> reducer) |
<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> 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) |
SequenceM<java.util.List<T>> |
grouped(int groupSize)
Group elements in a Monad into a Stream
|
HeadAndTail<T> |
headAndTail() |
java.util.Optional<HeadAndTail<T>> |
headAndTailOptional() |
boolean |
isParallel() |
java.util.Iterator<T> |
iterator() |
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(int 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)
NB to access nested collections in non-Stream monads as a stream use
streamedMonad() first
|
SequenceM<T> |
limitWhile(java.util.function.Predicate<? super T> p)
NB to access nested collections in non-Stream monads as a stream use
streamedMonad() first
|
<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) |
java.lang.String |
mkString(java.lang.String sep) |
java.lang.String |
mkString(java.lang.String start,
java.lang.String sep,
java.lang.String end) |
java.lang.String |
mkString(java.lang.String start,
java.lang.String sep,
java.lang.String end,
boolean lazy) |
boolean |
noneMatch(java.util.function.Predicate<? super T> c) |
SequenceM<T> |
parallel() |
SequenceM<T> |
peek(java.util.function.Consumer<? super T> c) |
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)
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.
|
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> |
scanLeft(Monoid<T> monoid)
Scan left using supplied Monoid
|
SequenceM<T> |
sequential() |
SequenceM<T> |
skip(int 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 monad
|
SequenceM<T> |
sorted() |
SequenceM<T> |
sorted(java.util.Comparator<? super T> c)
e.g.
|
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) |
java.util.List<T> |
toList() |
java.util.Set<T> |
toSet() |
<T> java.util.stream.Stream<T> |
toStream() |
Streamable<T> |
toStreamable() |
SequenceM<T> |
unordered() |
<R> R |
unwrap() |
java.util.concurrent.CompletableFuture<java.util.List<T>> |
unwrapCompletableFuture() |
java.util.Optional<java.util.List<T>> |
unwrapOptional() |
java.util.stream.Stream<T> |
unwrapStream()
Type safe unwrap
|
boolean |
xMatch(int num,
java.util.function.Predicate<? super T> c) |
<S,R> SequenceM<R> |
zip(AnyM<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper) |
<S,R> SequenceM<R> |
zip(SequenceM<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Generic zip function.
|
<S,R> SequenceM<R> |
zip(java.util.stream.Stream<? extends S> second,
java.util.function.BiFunction<? super T,? super S,? extends R> zipper)
Zip this Monad with a Stream
|
public final <R> R unwrap()
unwrap in interface Unwrapablepublic final java.util.stream.Stream<T> unwrapStream()
public final java.util.Optional<java.util.List<T>> unwrapOptional()
public final java.util.concurrent.CompletableFuture<java.util.List<T>> unwrapCompletableFuture()
public final SequenceM<T> cycle(int times)
times - Times values should be repeated within a Streampublic 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)
predicate - repeat while truepublic final SequenceM<T> cycleUntil(java.util.function.Predicate<? super T> predicate)
predicate - repeat while truepublic 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)
public final <S,R> SequenceM<R> zip(java.util.stream.Stream<? 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)
windowSize - Size of sliding 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 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> sorted()
monad(Optional.of(Arrays.asList(1,2,3))).sorted() // Monad[Stream[List[1,2,3]]]
monad(Optional.of(Arrays.asList(1,2,3))).streamedMonad().sorted() // Monad[Stream[1,2,3]]
assertThat(monad(Stream.of(4,3,6,7)).sorted().toList(),equalTo(Arrays.asList(3,4,6,7)));
public final SequenceM<T> sorted(java.util.Comparator<? super T> c)
anyM(Optional.of(Arrays.asList(1,2,3))).asSequence().sorted( (a,b)->b-a) // Monad[Stream[List[1,2,3]]]
anyM(Optional.of(Arrays.asList(1,2,3))).toSequence().sorted( (a,b)->b-a) // Monad[Stream[3,2,1]]
c - Compartor to sort withpublic final SequenceM<T> skip(int num)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().skip(2).toList(),equalTo(Arrays.asList(6,7)));
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(int num)
assertThat(anyM(Stream.of(4,3,6,7)).asSequence().limit(2).toList(),equalTo(Arrays.asList(4,3)));
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 SequenceM<T> parallel()
public final boolean allMatch(java.util.function.Predicate<? super T> c)
c - Predicate to check if all matchpublic final boolean anyMatch(java.util.function.Predicate<? super T> c)
c - Predicate to check if any matchpublic boolean xMatch(int num,
java.util.function.Predicate<? super T> c)
public final boolean noneMatch(java.util.function.Predicate<? super T> c)
public final java.lang.String mkString(java.lang.String sep)
public final java.lang.String mkString(java.lang.String start,
java.lang.String sep,
java.lang.String end)
public final java.lang.String mkString(java.lang.String start,
java.lang.String sep,
java.lang.String end,
boolean lazy)
public final <C extends java.lang.Comparable<? super C>> java.util.Optional<T> minBy(java.util.function.Function<T,C> f)
public final <C extends java.lang.Comparable<? super C>> java.util.Optional<T> maxBy(java.util.function.Function<T,C> f)
public final HeadAndTail<T> headAndTail()
public final java.util.Optional<HeadAndTail<T>> headAndTailOptional()
public final java.util.Optional<T> findFirst()
public final java.util.Optional<T> findAny()
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)
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)
public final java.util.List collect(java.util.stream.Stream<java.util.stream.Collector> collectors)
List result = monad(Stream.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));
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. I.e. streamedMonad().collect(collectors);collectors - Stream of Collectors to applypublic final T reduce(Monoid<T> reducer)
reducer - Use supplied Monoid to reduce valuespublic final <U> U reduce(U identity,
java.util.function.BiFunction<U,? super T,U> accumulator,
java.util.function.BinaryOperator<U> combiner)
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 <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> flatMap(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
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 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)
public java.util.stream.LongStream mapToLong(java.util.function.ToLongFunction<? super T> mapper)
public java.util.stream.DoubleStream mapToDouble(java.util.function.ToDoubleFunction<? super T> mapper)
public java.util.stream.IntStream flatMapToInt(java.util.function.Function<? super T,? extends java.util.stream.IntStream> mapper)
public java.util.stream.LongStream flatMapToLong(java.util.function.Function<? super T,? extends java.util.stream.LongStream> mapper)
public java.util.stream.DoubleStream flatMapToDouble(java.util.function.Function<? super T,? extends java.util.stream.DoubleStream> mapper)
public void forEachOrdered(java.util.function.Consumer<? super T> action)
public java.lang.Object[] toArray()
public <A> A[] toArray(java.util.function.IntFunction<A[]> generator)
public long count()