@FunctionalInterface
public interface Functor<T>
Modifier and Type | Method and Description |
---|---|
default <U> Functor<U> |
cast(java.lang.Class<U> type)
Cast all elements in a stream to a given type, possibly throwing a
ClassCastException . |
<R> Functor<R> |
map(java.util.function.Function<? super T,? extends R> fn) |
default <R> Functor<R> |
patternMatch(java.util.function.Function<Matchable.CheckValues<T,R>,Matchable.CheckValues<T,R>> case1,
java.util.function.Supplier<? extends R> otherwise)
Transform the elements of this Stream with a Pattern Matching case and default value
|
default Functor<T> |
peek(java.util.function.Consumer<? super T> c) |
default <R> Functor<R> |
trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
Performs a map operation that can call a recursive method without running out of stack space
|
default <U> Functor<U> cast(java.lang.Class<U> type)
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)default <R> Functor<R> trampoline(java.util.function.Function<? super T,? extends Trampoline<? extends R>> mapper)
ReactiveSeq.of(10,20,30,40)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
Trampoline<Long> fibonacci(int i){
return fibonacci(i,1,0);
}
Trampoline<Long> fibonacci(int n, long a, long b) {
return n == 0 ? Trampoline.done(b) : Trampoline.more( ()->fibonacci(n-1, a+b, a));
}
55
6765
832040
102334155
ReactiveSeq.of(10_000,200_000,3_000_000,40_000_000)
.trampoline(i-> fibonacci(i))
.forEach(System.out::println);
completes successfully
mapper
- default <R> Functor<R> patternMatch(java.util.function.Function<Matchable.CheckValues<T,R>,Matchable.CheckValues<T,R>> case1, java.util.function.Supplier<? extends R> otherwise)
List<String> result = CollectionX.of(1,2,3,4)
.patternMatch(
c->c.valuesWhere(i->"even", (Integer i)->i%2==0 )
)
// CollectionX["odd","even","odd","even"]
case1
- Function to generate a case (or chain of cases as a single case)otherwise
- Value if supplied case doesn't match