From 2d1b75644f0c8bd748ffc3525d8df6713184572e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Mon, 27 May 2024 14:17:46 +0300 Subject: [PATCH] docs: update trampoline --- trampoline/README.md | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/trampoline/README.md b/trampoline/README.md index fd0395010..a42a43bc6 100644 --- a/trampoline/README.md +++ b/trampoline/README.md @@ -93,17 +93,22 @@ public interface Trampoline { Using the `Trampoline` to get Fibonacci values. ```java -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); -} +@Slf4j +public class TrampolineApp { -public static Trampoline loop(int times, int prod) { - if (times == 0) { - return Trampoline.done(prod); - } else { - return Trampoline.more(() -> loop(times - 1, prod * times)); + 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); + } else { + return Trampoline.more(() -> loop(times - 1, prod * times)); + } } } ``` @@ -115,10 +120,6 @@ Program output: 19:22:24.472 [main] INFO com.iluwatar.trampoline.TrampolineApp - The number of orcs perished in the war: 3628800 ``` -## Class diagram - -![Trampoline](./etc/trampoline.urm.png "Trampoline pattern class diagram") - ## Applicability Use the Trampoline pattern when @@ -126,6 +127,14 @@ Use the Trampoline pattern when * When dealing with algorithms that use recursion heavily and risk running into stack overflow errors. * When tail-call optimization is not supported by the Java language natively. +## Tutorials + +* [Laziness, trampolines, monoids and other functional amenities: This is not your father's Java(Mario Fusco)](https://www.slideshare.net/mariofusco/lazine) +* [Trampoline.java (totallylazy)](https://github.com/bodar/totallylazy/blob/master/src/com/googlecode/totallylazy/Trampoline.java) +* [Trampoline: Java Glossary (mindprod.com)](http://mindprod.com/jgloss/trampoline.html) +* [Trampolining: A practical guide for awesome Java Developers (John McClean)](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076) +* [What is a trampoline function? (Stack Overflow)](https://stackoverflow.com/questions/189725/what-is-a-trampoline-function) + ## Known Uses * Implementing algorithms that require deep recursion, such as certain tree traversals, combinatorial algorithms, and mathematical computations. @@ -153,12 +162,7 @@ Trade-offs: ## Credits -* [Trampolining: A practical guide for awesome Java Developers](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076) -* [Trampoline: Java Glossary](http://mindprod.com/jgloss/trampoline.html) -* [Laziness, trampolines, monoids and other functional amenities: This is not your father's Java](https://www.slideshare.net/mariofusco/lazine) -* [Trampoline implementation](https://github.com/bodar/totallylazy/blob/master/src/com/googlecode/totallylazy/Trampoline.java) -* [What is a trampoline function? - Stack Overflow](https://stackoverflow.com/questions/189725/what-is-a-trampoline-function) -* [Functional Programming for Java Developers: Tools for Better Concurrency, Abstraction, and Agility](https://amzn.to/4dRu4rJ) * [Functional Programming in Java](https://amzn.to/3JUIc5Q) +* [Functional Programming for Java Developers: Tools for Better Concurrency, Abstraction, and Agility](https://amzn.to/4dRu4rJ) * [Java 8 in Action: Lambdas, Streams, and functional-style programming](https://amzn.to/3QCmGXs) * [Modern Java in Action: Lambdas, streams, functional and reactive programming](https://amzn.to/3yxdu0g)