When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
+ on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)},
+ stopping once the returned instance is {@link #done(Object)}.
+ Essential we convert looping via recursion into iteration,
+ the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.
+*/
public interface Trampoline {
T get();
diff --git a/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java b/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java
index d629f7a84..73222ddf5 100644
--- a/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java
+++ b/trampoline/src/main/java/com/iluwatar/trampoline/TrampolineApp.java
@@ -1,17 +1,17 @@
/**
* The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä
- *
+ *
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
- *
+ *
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
- *
+ *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -27,23 +27,9 @@ package com.iluwatar.trampoline;
import lombok.extern.slf4j.Slf4j;
/**
- *
- * By representing a computation in one of 2 states
- (completed with result, or a reference to the reminder of the computation,
- something like the way a java.util.Supplier does)
- it is possible to implement algorithms recursively in Java without blowing the stack
- and to interleave the execution of functions without hard coding them together or even using threads.
-
-
- Trampoline has 2 state : [done], [ more]
-
- When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
- on the returned Trampoline as long as the concrete instance returned is More,
- stopping once the returned instance is Done. Essential we convert looping via recursion into iteration,
- the key enabling mechanism is the fact that Trampoline.more is a lazy operation.
- Trampoline in cyclops-react extends java.util.Supplier. Calling Trampoline.more we are basically creating
- a Supplier that defers the actual recursive call, and having defered the call we can move it outside of the recursive loop.
- This means we can define algorithms recursively in Java but execute them iteratively.
+ * Trampoline pattern allows to define recursive algorithms by iterative loop
+ * it is possible to implement algorithms recursively in Java without blowing the stack
+ * and to interleave the execution of functions without hard coding them together or even using threads.
*/
@Slf4j
@@ -51,17 +37,18 @@ public class TrampolineApp {
public static void main(String[] args) {
log.info("start pattern");
Integer result = loop(10, 1).result();
- log.info("result {}" ,result);
+ log.info("result {}", result);
}
+
/**
- * Manager for pattern.
+ * Manager for pattern. Define it with a factorial function.
* */
- public static Trampoline loop(int times,int prod){
- if(times==0)
+ public static Trampoline loop(int times, int prod) {
+ if (times == 0)
return Trampoline.done(prod);
else
- return Trampoline.more(()->loop(times-1,prod*times));
+ return Trampoline.more(() -> loop(times - 1, prod * times));
}
}
diff --git a/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java b/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java
index fb7fc09ee..9765131d5 100644
--- a/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java
+++ b/trampoline/src/test/java/com/iluwatar/trampoline/TrampolineAppTest.java
@@ -8,8 +8,9 @@ import static org.junit.Assert.*;
public class TrampolineAppTest {
+
@Test
- public void test()throws IOException{
+ public void testTrampolineWithFactorialFunction()throws IOException{
int result = TrampolineApp.loop(10, 1).result();
assertEquals("Be equal",3628800,result);
}