Skip to content

Commit c549218

Browse files
iluwatarohbus
andauthored
enhancement: check spelling and update topic (#1943)
Co-authored-by: Subhrodip Mohanta <[email protected]>
1 parent 4f8007d commit c549218

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

trampoline/README.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ and to interleave the execution of functions without hard coding them together.
1717
## Explanation
1818

1919
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.
2424

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
2626
an intermediate value stored and there is a limited amount of stack memory available. Running out of
2727
stack memory creates a stack overflow error and halts the program execution.
2828

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
3030
stack.
3131

32-
Real world example
32+
Real-world example
3333

3434
> A recursive Fibonacci calculation without the stack overflow problem using the Trampoline pattern.
3535
@@ -105,24 +105,26 @@ public interface Trampoline<T> {
105105
Using the `Trampoline` to get Fibonacci values.
106106

107107
```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) {
109115
if (times == 0) {
110-
return Trampoline.done(prod);
116+
return Trampoline.done(prod);
111117
} else {
112-
return Trampoline.more(() -> loop(times - 1, prod * times));
118+
return Trampoline.more(() -> loop(times - 1, prod * times));
113119
}
114-
}
115-
116-
log.info("start pattern");
117-
var result = loop(10, 1).result();
118-
log.info("result {}", result);
120+
}
119121
```
120122

121123
Program output:
122124

123125
```
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
126128
```
127129

128130
## Class diagram
@@ -133,8 +135,8 @@ result 3628800
133135

134136
Use the Trampoline pattern when
135137

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.
138140

139141
## Known uses
140142

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

-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,4 @@ T trampoline(final Trampoline<T> trampoline) {
107107
}
108108
};
109109
}
110-
111-
112110
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class TrampolineApp {
3939
* Main program for showing pattern. It does loop with factorial function.
4040
*/
4141
public static void main(String[] args) {
42-
LOGGER.info("start pattern");
42+
LOGGER.info("Start calculating war casualties");
4343
var result = loop(10, 1).result();
44-
LOGGER.info("result {}", result);
44+
LOGGER.info("The number of orcs perished in the war: {}", result);
4545

4646
}
4747

@@ -55,5 +55,4 @@ public static Trampoline<Integer> loop(int times, int prod) {
5555
return Trampoline.more(() -> loop(times - 1, prod * times));
5656
}
5757
}
58-
5958
}

0 commit comments

Comments
 (0)