You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: trampoline/README.md
+28-17Lines changed: 28 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,22 +10,23 @@ tags:
10
10
11
11
## Intent
12
12
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.
15
15
16
16
## Explanation
17
17
18
18
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.
23
23
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.
27
27
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.
29
30
30
31
Real world example
31
32
@@ -37,14 +38,18 @@ In plain words
37
38
38
39
Wikipedia says
39
40
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.
41
45
42
46
**Programmatic Example**
43
47
44
48
Here's the `Trampoline` implementation in Java.
45
49
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`.
48
53
49
54
```java
50
55
publicinterfaceTrampoline<T> {
@@ -68,7 +73,7 @@ public interface Trampoline<T> {
0 commit comments