Update README.md

This commit is contained in:
Ilkka Seppälä 2020-09-13 18:35:23 +03:00
parent 24e8fa1bad
commit 82eb41b641
2 changed files with 29 additions and 18 deletions

View File

@ -10,22 +10,23 @@ tags:
## Intent ## Intent
Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack and to interleave Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack
the execution of functions without hard coding them together. and to interleave the execution of functions without hard coding them together.
## Explanation ## Explanation
Recursion is a frequently adopted technique for solving algorithmic problems in a divide and conquer Recursion is a frequently adopted technique for solving algorithmic problems in a divide and conquer
style. For example calculating fibonacci accumulating sum and factorials. In these kinds of problems recursion is style. For example calculating fibonacci accumulating sum and factorials. In these kinds of problems
more straightforward than their loop counterpart. Furthermore recursion may need less code and looks more concise. recursion is more straightforward than their loop counterpart. Furthermore recursion may need less
There is a saying that every recursion problem can be solved using a loop with the cost of writing code that is more code and looks more concise. There is a saying that every recursion problem can be solved using
difficult to understand. a loop with the cost of writing code that is more difficult to understand.
However recursion type solutions have one big caveat. For each recursive call it typically needs an intermediate value However recursion type solutions have one big caveat. For each recursive call it typically needs
stored and there is a limited amount of stack memory available. Running out of stack memory creates a stack overflow an intermediate value stored and there is a limited amount of stack memory available. Running out of
error and halts the program execution. stack memory creates a stack overflow error and halts the program execution.
Trampoline pattern is a trick that allows us define recursive algorithms in Java without blowing the stack. Trampoline pattern is a trick that allows us define recursive algorithms in Java without blowing the
stack.
Real world example Real world example
@ -37,14 +38,18 @@ In plain words
Wikipedia says Wikipedia says
> In Java, trampoline refers to using reflection to avoid using inner classes, for example in event listeners. The time overhead of a reflection call is traded for the space overhead of an inner class. Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class. > In Java, trampoline refers to using reflection to avoid using inner classes, for example in event
> listeners. The time overhead of a reflection call is traded for the space overhead of an inner
> class. Trampolines in Java usually involve the creation of a GenericListener to pass events to
> an outer class.
**Programmatic Example** **Programmatic Example**
Here's the `Trampoline` implementation in Java. Here's the `Trampoline` implementation in Java.
When `get` is called on the returned Trampoline, internally it will iterate calling `jump` on the returned `Trampoline` When `get` is called on the returned Trampoline, internally it will iterate calling `jump` on the
as long as the concrete instance returned is `Trampoline`, stopping once the returned instance is `done`. returned `Trampoline` as long as the concrete instance returned is `Trampoline`, stopping once the
returned instance is `done`.
```java ```java
public interface Trampoline<T> { public interface Trampoline<T> {
@ -68,7 +73,7 @@ public interface Trampoline<T> {
} }
static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) { static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) {
return new Trampoline<T>() { return new Trampoline<>() {
@Override @Override
public boolean complete() { public boolean complete() {
return false; return false;
@ -110,15 +115,21 @@ Using the `Trampoline` to get Fibonacci values.
log.info("start pattern"); log.info("start pattern");
var result = loop(10, 1).result(); var result = loop(10, 1).result();
log.info("result {}", result); log.info("result {}", result);
```
// start pattern
// result 3628800 Program output:
```
start pattern
result 3628800
``` ```
## Class diagram ## Class diagram
![alt text](./etc/trampoline.urm.png "Trampoline pattern class diagram") ![alt text](./etc/trampoline.urm.png "Trampoline pattern class diagram")
## Applicability ## Applicability
Use the Trampoline pattern when Use the Trampoline pattern when
* For implementing tail recursive function. This pattern allows to switch on a stackless operation. * For implementing tail recursive function. This pattern allows to switch on a stackless operation.

View File

@ -82,7 +82,7 @@ public interface Trampoline<T> {
* @return Trampoline with more work * @return Trampoline with more work
*/ */
static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) { static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) {
return new Trampoline<T>() { return new Trampoline<>() {
@Override @Override
public boolean complete() { public boolean complete() {
return false; return false;