public final class Trampoline extends Object
Whenever a method has a tail call that tail call can be externalised by not
returning the result of the call, but instead an object instructing the
Trampoline to perform the call to that method and to return the value
provided by that function. That function can itself return the instruction to
call another method. This allows for iterative execution instead of recursive
one preventing StackOverflowErrors.
This class provides all helper methods required for Trampoline
implementations with functions with up to two arguments (helpers for more
arguments may be added in the future). Use Trampoline.Result<T> (where T is the return type of the
complete trampolined call structure) as the return type of any involved
method. Normal methods invoked from inside a Trampoline must either be called
as a subcall and the result packed into a Trampoline.Result instance
using result(T) or they can be wrapped into a
Trampoline.Result using bounceWrapped(java.util.function.Supplier<T>).
| Modifier and Type | Class and Description |
|---|---|
static class |
Trampoline.Result<T> |
| Constructor and Description |
|---|
Trampoline() |
| Modifier and Type | Method and Description |
|---|---|
static <T,A,B> Trampoline.Result<T> |
bounce(java.util.function.BiFunction<A,B,Trampoline.Result<T>> f,
A param0,
B param1) |
static <T,A> Trampoline.Result<T> |
bounce(java.util.function.Function<A,Trampoline.Result<T>> f,
A param0) |
static <T> Trampoline.Result<T> |
bounce(java.util.function.Supplier<Trampoline.Result<T>> f) |
static <T,A,B> Trampoline.Result<T> |
bounceWrapped(java.util.function.BiFunction<A,B,T> f,
A param0,
B param1) |
static <T,A> Trampoline.Result<T> |
bounceWrapped(java.util.function.Function<A,T> f,
A param0) |
static <T> Trampoline.Result<T> |
bounceWrapped(java.util.function.Supplier<T> f) |
static <T> Trampoline.Result<T> |
result(T value) |
static <T> T |
run(Trampoline.Result<T> result) |
public static <T> Trampoline.Result<T> result(T value)
public static <T> Trampoline.Result<T> bounce(java.util.function.Supplier<Trampoline.Result<T>> f)
public static <T,A> Trampoline.Result<T> bounce(java.util.function.Function<A,Trampoline.Result<T>> f, A param0)
public static <T,A,B> Trampoline.Result<T> bounce(java.util.function.BiFunction<A,B,Trampoline.Result<T>> f, A param0, B param1)
public static <T> Trampoline.Result<T> bounceWrapped(java.util.function.Supplier<T> f)
public static <T,A> Trampoline.Result<T> bounceWrapped(java.util.function.Function<A,T> f, A param0)
public static <T,A,B> Trampoline.Result<T> bounceWrapped(java.util.function.BiFunction<A,B,T> f, A param0, B param1)
public static <T> T run(Trampoline.Result<T> result)
Copyright © 2016. All Rights Reserved.