public interface FlatMapM<T>
Modifier and Type | Method and Description |
---|---|
AnyM<java.lang.String> |
flatMapBufferedReader(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
|
AnyM<java.lang.Character> |
flatMapCharSequence(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.
|
<R> AnyM<R> |
flatMapCollection(java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Stream.
|
<R> AnyM<R> |
flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn) |
AnyM<java.lang.String> |
flatMapFile(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.
|
<R> AnyM<R> |
flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Optional
|
<R> AnyM<R> |
flatMapSequenceM(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Stream.
|
<R> AnyM<R> |
flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
Convenience method to allow method reference support, when flatMap return type is a Stream.
|
<R> AnyM<R> |
flatMapStreamable(java.util.function.Function<? super T,Streamable<R>> fn)
Convenience method to allow method reference support, when flatMap return type is a Stream.
|
AnyM<java.lang.String> |
flatMapURL(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
|
<R> AnyM<R> flatMapStream(java.util.function.Function<? super T,java.util.stream.BaseStream<? extends R,?>> fn)
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i->Stream.of(i+1,i+2));
//AnyM[Stream[2,3,3,4,4,5]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMap(i->Stream.of(i+1,i+2));
//AnyM[Optional[2]]
fn
- flatMap function<R> AnyM<R> flatMapStreamable(java.util.function.Function<? super T,Streamable<R>> fn)
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i->Streamable.of(i+1,i+2));
//AnyM[Stream[2,3,3,4,4,5]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMap(i->Streamable.of(i+1,i+2));
//AnyM[Optional[2]]
fn
- flatMap function<R> AnyM<R> flatMapCollection(java.util.function.Function<? super T,java.util.Collection<? extends R>> fn)
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i->Arrays.asList(i+1,i+2));
//AnyM[Stream[2,3,3,4,4,5]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMap(i->Arrays.asList(i+1,i+2));
//AnyM[Optional[2]]
fn
- flatMap function<R> AnyM<R> flatMapOptional(java.util.function.Function<? super T,java.util.Optional<? extends R>> fn)
List<Integer> list = Arrays.asList(1,2,3,4,null,6);
AnyM<Integer> anyM = AnyM.fromIterable(list);
//AnyM[1,2,3,4,null,6]
AnyM<Integer> removeNulls = anyM.flatMapOptional(Optional::ofNullable);
//AnyM[1,2,3,4,6]
fn
- flatMap function<R> AnyM<R> flatMapCompletableFuture(java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<? extends R>> fn)
<R> AnyM<R> flatMapSequenceM(java.util.function.Function<? super T,SequenceM<? extends R>> fn)
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i->SequenceM.of(i+1,i+2));
//AnyM[Stream[2,3,3,4,4,5]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMap(i->SequenceM.of(i+1,i+2));
//AnyM[Optional[2]]
fn
- flatMap functionAnyM<java.lang.Character> flatMapCharSequence(java.util.function.Function<? super T,java.lang.CharSequence> fn)
List<Character> result = AnyM.fromArray("input.file")
.liftAndBindCharSequence(i->"hello world")
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList('h','e','l','l','o',' ','w','o','r','l','d')));
AnyM follows the javaslang modified 'monad' laws https://gist.github.com/danieldietrich/71be006b355d6fbc0584
In particular left-identity becomes
Left identity: unit(a).flatMap(f) ≡ select(f.apply(a))
Or in plain English, if your flatMap function returns multiple values (such as flatMap by Stream) but the current Monad only can only hold one value,
only the first value is accepted.
Example 1 : multi-values are supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMap(i-> ""+(i+1)+(i+2));
//AnyM[Stream[2,3,3,4,4,5]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMap(i->""+(i+1)+(i+2));
//AnyM[Optional[2]]
fn
- AnyM<java.lang.String> flatMapFile(java.util.function.Function<? super T,java.io.File> fn)
List<String> result = anyM("input.file")
.map(getClass().getClassLoader()::getResource)
.peek(System.out::println)
.map(URL::getFile)
.liftAndBindFile(File::new)
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
AnyM follows the javaslang modified 'monad' laws https://gist.github.com/danieldietrich/71be006b355d6fbc0584
In particular left-identity becomes
Left identity: unit(a).flatMap(f) ≡ select(f.apply(a))
Or in plain English, if your flatMap function returns multiple values (such as flatMap by Stream) but the current Monad only can only hold one value,
only the first value is accepted.
Example 1 : multi-values are supported (AnyM wraps a Stream, List, Set etc)
AnyM<String> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMapFile(this::loadFile);
//AnyM[Stream["line1","line2"]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMapFile(this::loadFile);
//AnyM[Optional["line1"]]
fn
- AnyM<java.lang.String> flatMapURL(java.util.function.Function<? super T,java.net.URL> fn)
List<String> result = anyM("input.file")
.liftAndBindURL(getClass().getClassLoader()::getResource)
.asSequence()
.toList();
assertThat(result,equalTo(Arrays.asList("hello","world")));
AnyM follows the javaslang modified 'monad' laws https://gist.github.com/danieldietrich/71be006b355d6fbc0584
In particular left-identity becomes
Left identity: unit(a).flatMap(f) ≡ select(f.apply(a))
Or in plain English, if your flatMap function returns multiple values (such as flatMap by Stream) but the current Monad only can only hold one value,
only the first value is accepted.
Example 1 : multi-values are supported (AnyM wraps a Stream, List, Set etc)
AnyM<String> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMapURL(this::loadURL);
//AnyM[Stream["line1","line2"]]
Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMapURL(this::loadURL);
//AnyM[Optional["line1"]]
fn
- AnyM<java.lang.String> flatMapBufferedReader(java.util.function.Function<? super T,java.io.BufferedReader> fn)
List<String> result = anyM("input.file") .map(getClass().getClassLoader()::getResourceAsStream) .map(InputStreamReader::new) .liftAndBindBufferedReader(BufferedReader::new) .asSequence() .toList(); assertThat(result,equalTo(Arrays.asList("hello","world")));
AnyM follows the javaslang modified 'monad' laws https://gist.github.com/danieldietrich/71be006b355d6fbc0584 In particular left-identity becomes Left identity: unit(a).flatMap(f) ≡ select(f.apply(a)) Or in plain English, if your flatMap function returns multiple values (such as flatMap by Stream) but the current Monad only can only hold one value, only the first value is accepted. Example 1 : multi-values are supported (AnyM wraps a Stream, List, Set etc)Example 2 : multi-values are not supported (AnyM wraps a Stream, List, Set etc)AnyM<String> anyM = AnyM.fromStream(Stream.of(1,2,3)).flatMapBufferedRead(this::reader); //AnyM[Stream["line1","line2"]]
AnyM<Integer> anyM = AnyM.fromOptional(Optional.of(1)).flatMapBufferedRead(this::reader); //AnyM[Optional["line1"]]
fn
-