T
- Data type of element(s) stored in this Transformable@FunctionalInterface
public interface Transformable<T>
Modifier and Type | Method and Description |
---|---|
default <U> Transformable<U> |
cast(java.lang.Class<? extends U> type)
Cast all elements in a reactiveStream to a given type, possibly throwing a
ClassCastException . |
<R> Transformable<R> |
map(java.util.function.Function<? super T,? extends R> fn)
Transform this functor using the supplied transformation function
|
default Transformable<T> |
peek(java.util.function.Consumer<? super T> c)
Peek at the current value of this Transformable, without transforming it
|
default <R> Transformable<R> |
retry(java.util.function.Function<? super T,? extends R> fn)
Retry a transformation if it fails.
|
default <R> Transformable<R> |
retry(java.util.function.Function<? super T,? extends R> fn,
int retries,
long delay,
java.util.concurrent.TimeUnit timeUnit)
Retry a transformation if it fails.
|
default <R> Transformable<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> Transformable<U> cast(java.lang.Class<? extends U> type)
ClassCastException
.
// ClassCastException ReactiveSeq.of(1, "a", 2, "b", 3).cast(Integer.class)<R> Transformable<R> map(java.util.function.Function<? super T,? extends R> fn)
of(1,2,3).map(i->i*2)
//[2,4,6]
fn
- Transformation functiondefault Transformable<T> peek(java.util.function.Consumer<? super T> c)
of(1,2,3).map(System.out::println)
1
2
3
c
- Consumer that recieves each element from this Transformabledefault <R> Transformable<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
- TCO Transformation functiondefault <R> Transformable<R> retry(java.util.function.Function<? super T,? extends R> fn)
given(serviceMock.applyHKT(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock)
.firstValue();
//result = 42
fn
- Function to retry if failsdefault <R> Transformable<R> retry(java.util.function.Function<? super T,? extends R> fn, int retries, long delay, java.util.concurrent.TimeUnit timeUnit)
given(serviceMock.applyHKT(anyInt())).willThrow(
new RuntimeException(new SocketException("First")),
new RuntimeException(new IOException("Second"))).willReturn(
"42");
String result = ReactiveSeq.of( 1, 2, 3)
.retry(serviceMock, 7, 2, TimeUnit.SECONDS)
.firstValue();
//result = 42
fn
- Function to retry if failsretries
- Number of retriesdelay
- Delay in TimeUnitstimeUnit
- TimeUnit to use for delay