From c5492184b7f2331afea2a1f746d06ffd79b2ae0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 8 Jan 2022 14:33:19 +0200 Subject: [PATCH] enhancement: check spelling and update topic (#1943) Co-authored-by: Subhrodip Mohanta --- trampoline/README.md | 40 ++++++++++--------- .../com/iluwatar/trampoline/Trampoline.java | 2 - .../iluwatar/trampoline/TrampolineApp.java | 5 +-- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/trampoline/README.md b/trampoline/README.md index 6eb870227..eceaf3f1f 100644 --- a/trampoline/README.md +++ b/trampoline/README.md @@ -17,19 +17,19 @@ and to interleave the execution of functions without hard coding them together. ## Explanation 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 more straightforward than their loop counterpart. Furthermore recursion may need less -code and looks more concise. There is a saying that every recursion problem can be solved using -a loop with the cost of writing code that is more difficult to understand. +style. For example, calculating Fibonacci accumulating sum and factorials. In these kinds of +problems, recursion is more straightforward than its loop counterpart. Furthermore, recursion may +need less code and looks more concise. There is a saying that every recursion problem can be solved +using 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 +However, recursion-type solutions have one big caveat. For each recursive call, it typically needs an intermediate value stored and there is a limited amount of stack memory available. Running out of 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 +Trampoline pattern is a trick that allows defining recursive algorithms in Java without blowing the stack. -Real world example +Real-world example > A recursive Fibonacci calculation without the stack overflow problem using the Trampoline pattern. @@ -105,24 +105,26 @@ public interface Trampoline { Using the `Trampoline` to get Fibonacci values. ```java - public static Trampoline loop(int times, int prod) { +public static void main(String[] args) { + LOGGER.info("Start calculating war casualties"); + var result = loop(10, 1).result(); + LOGGER.info("The number of orcs perished in the war: {}", result); +} + +public static Trampoline loop(int times, int prod) { if (times == 0) { - return Trampoline.done(prod); + return Trampoline.done(prod); } else { - return Trampoline.more(() -> loop(times - 1, prod * times)); + return Trampoline.more(() -> loop(times - 1, prod * times)); } - } - - log.info("start pattern"); - var result = loop(10, 1).result(); - log.info("result {}", result); +} ``` Program output: ``` -start pattern -result 3628800 +19:22:24.462 [main] INFO com.iluwatar.trampoline.TrampolineApp - Start calculating war casualties +19:22:24.472 [main] INFO com.iluwatar.trampoline.TrampolineApp - The number of orcs perished in the war: 3628800 ``` ## Class diagram @@ -133,8 +135,8 @@ result 3628800 Use the Trampoline pattern when -* For implementing tail recursive function. This pattern allows to switch on a stackless operation. -* For interleaving the execution of two or more functions on the same thread. +* For implementing tail-recursive functions. This pattern allows to switch on a stackless operation. +* For interleaving execution of two or more functions on the same thread. ## Known uses diff --git a/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java b/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java index 1d2ea91d3..d60ef3602 100644 --- a/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java +++ b/trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java @@ -107,6 +107,4 @@ public interface Trampoline { } }; } - - } diff --git a/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java b/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java index 32a3f1850..bfeed4d69 100644 --- a/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java +++ b/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java @@ -39,9 +39,9 @@ public class TrampolineApp { * Main program for showing pattern. It does loop with factorial function. */ public static void main(String[] args) { - LOGGER.info("start pattern"); + LOGGER.info("Start calculating war casualties"); var result = loop(10, 1).result(); - LOGGER.info("result {}", result); + LOGGER.info("The number of orcs perished in the war: {}", result); } @@ -55,5 +55,4 @@ public class TrampolineApp { return Trampoline.more(() -> loop(times - 1, prod * times)); } } - }