Skip to content

Commit 82eb41b

Browse files
committed
Update README.md
1 parent 24e8fa1 commit 82eb41b

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

trampoline/README.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,23 @@ tags:
1010

1111
## Intent
1212

13-
Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack and to interleave
14-
the execution of functions without hard coding them together.
13+
Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack
14+
and to interleave the execution of functions without hard coding them together.
1515

1616
## Explanation
1717

1818
Recursion is a frequently adopted technique for solving algorithmic problems in a divide and conquer
19-
style. For example calculating fibonacci accumulating sum and factorials. In these kinds of problems recursion is
20-
more straightforward than their loop counterpart. Furthermore recursion may need less code and looks more concise.
21-
There is a saying that every recursion problem can be solved using a loop with the cost of writing code that is more
22-
difficult to understand.
19+
style. For example calculating fibonacci accumulating sum and factorials. In these kinds of problems
20+
recursion is more straightforward than their loop counterpart. Furthermore recursion may need less
21+
code and looks more concise. There is a saying that every recursion problem can be solved using
22+
a loop with the cost of writing code that is more difficult to understand.
2323

24-
However recursion type solutions have one big caveat. For each recursive call it typically needs an intermediate value
25-
stored and there is a limited amount of stack memory available. Running out of stack memory creates a stack overflow
26-
error and halts the program execution.
24+
However recursion type solutions have one big caveat. For each recursive call it typically needs
25+
an intermediate value stored and there is a limited amount of stack memory available. Running out of
26+
stack memory creates a stack overflow error and halts the program execution.
2727

28-
Trampoline pattern is a trick that allows us define recursive algorithms in Java without blowing the stack.
28+
Trampoline pattern is a trick that allows us define recursive algorithms in Java without blowing the
29+
stack.
2930

3031
Real world example
3132

@@ -37,14 +38,18 @@ In plain words
3738
3839
Wikipedia says
3940

40-
> 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.
41+
> In Java, trampoline refers to using reflection to avoid using inner classes, for example in event
42+
> listeners. The time overhead of a reflection call is traded for the space overhead of an inner
43+
> class. Trampolines in Java usually involve the creation of a GenericListener to pass events to
44+
> an outer class.
4145
4246
**Programmatic Example**
4347

4448
Here's the `Trampoline` implementation in Java.
4549

46-
When `get` is called on the returned Trampoline, internally it will iterate calling `jump` on the returned `Trampoline`
47-
as long as the concrete instance returned is `Trampoline`, stopping once the returned instance is `done`.
50+
When `get` is called on the returned Trampoline, internally it will iterate calling `jump` on the
51+
returned `Trampoline` as long as the concrete instance returned is `Trampoline`, stopping once the
52+
returned instance is `done`.
4853

4954
```java
5055
public interface Trampoline<T> {
@@ -68,7 +73,7 @@ public interface Trampoline<T> {
6873
}
6974

7075
static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) {
71-
return new Trampoline<T>() {
76+
return new Trampoline<>() {
7277
@Override
7378
public boolean complete() {
7479
return false;
@@ -110,15 +115,21 @@ Using the `Trampoline` to get Fibonacci values.
110115
log.info("start pattern");
111116
var result = loop(10, 1).result();
112117
log.info("result {}", result);
113-
114-
// start pattern
115-
// result 3628800
118+
```
119+
120+
Program output:
121+
122+
```
123+
start pattern
124+
result 3628800
116125
```
117126

118127
## Class diagram
128+
119129
![alt text](./etc/trampoline.urm.png "Trampoline pattern class diagram")
120130

121131
## Applicability
132+
122133
Use the Trampoline pattern when
123134

124135
* For implementing tail recursive function. This pattern allows to switch on a stackless operation.

trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static <T> Trampoline<T> done(final T result) {
8282
* @return Trampoline with more work
8383
*/
8484
static <T> Trampoline<T> more(final Trampoline<Trampoline<T>> trampoline) {
85-
return new Trampoline<T>() {
85+
return new Trampoline<>() {
8686
@Override
8787
public boolean complete() {
8888
return false;

0 commit comments

Comments
 (0)