@@ -17,19 +17,19 @@ and to interleave the execution of functions without hard coding them together.
17
17
## Explanation
18
18
19
19
Recursion is a frequently adopted technique for solving algorithmic problems in a divide and conquer
20
- style. For example calculating fibonacci accumulating sum and factorials. In these kinds of problems
21
- recursion is more straightforward than their loop counterpart. Furthermore recursion may need less
22
- code and looks more concise. There is a saying that every recursion problem can be solved using
23
- a loop with the cost of writing code that is more difficult to understand.
20
+ style. For example, calculating Fibonacci accumulating sum and factorials. In these kinds of
21
+ problems, recursion is more straightforward than its loop counterpart. Furthermore, recursion may
22
+ need less code and looks more concise. There is a saying that every recursion problem can be solved
23
+ using a loop with the cost of writing code that is more difficult to understand.
24
24
25
- However recursion type solutions have one big caveat. For each recursive call it typically needs
25
+ However, recursion- type solutions have one big caveat. For each recursive call, it typically needs
26
26
an intermediate value stored and there is a limited amount of stack memory available. Running out of
27
27
stack memory creates a stack overflow error and halts the program execution.
28
28
29
- Trampoline pattern is a trick that allows us define recursive algorithms in Java without blowing the
29
+ Trampoline pattern is a trick that allows defining recursive algorithms in Java without blowing the
30
30
stack.
31
31
32
- Real world example
32
+ Real- world example
33
33
34
34
> A recursive Fibonacci calculation without the stack overflow problem using the Trampoline pattern.
35
35
@@ -105,24 +105,26 @@ public interface Trampoline<T> {
105
105
Using the ` Trampoline ` to get Fibonacci values.
106
106
107
107
``` java
108
- public static Trampoline<Integer > loop(int times, int prod) {
108
+ public static void main(String [] args) {
109
+ LOGGER . info(" Start calculating war casualties" );
110
+ var result = loop(10 , 1 ). result();
111
+ LOGGER . info(" The number of orcs perished in the war: {}" , result);
112
+ }
113
+
114
+ public static Trampoline<Integer > loop(int times, int prod) {
109
115
if (times == 0 ) {
110
- return Trampoline . done(prod);
116
+ return Trampoline . done(prod);
111
117
} else {
112
- return Trampoline . more(() - > loop(times - 1 , prod * times));
118
+ return Trampoline . more(() - > loop(times - 1 , prod * times));
113
119
}
114
- }
115
-
116
- log. info(" start pattern" );
117
- var result = loop(10 , 1 ). result();
118
- log. info(" result {}" , result);
120
+ }
119
121
```
120
122
121
123
Program output:
122
124
123
125
```
124
- start pattern
125
- result 3628800
126
+ 19:22:24.462 [main] INFO com.iluwatar.trampoline.TrampolineApp - Start calculating war casualties
127
+ 19:22:24.472 [main] INFO com.iluwatar.trampoline.TrampolineApp - The number of orcs perished in the war: 3628800
126
128
```
127
129
128
130
## Class diagram
@@ -133,8 +135,8 @@ result 3628800
133
135
134
136
Use the Trampoline pattern when
135
137
136
- * For implementing tail recursive function . This pattern allows to switch on a stackless operation.
137
- * For interleaving the execution of two or more functions on the same thread.
138
+ * For implementing tail- recursive functions . This pattern allows to switch on a stackless operation.
139
+ * For interleaving execution of two or more functions on the same thread.
138
140
139
141
## Known uses
140
142
0 commit comments