changes fixing mistakes

This commit is contained in:
tigraboris 2018-01-20 13:36:43 +03:00
parent 94fdefee3a
commit f7825f6c99
6 changed files with 34 additions and 44 deletions

View File

@ -147,13 +147,13 @@
<module>event-sourcing</module> <module>event-sourcing</module>
<module>data-transfer-object</module> <module>data-transfer-object</module>
<module>throttling</module> <module>throttling</module>
<module>unit-of-work</module> <module>unit-of-work</module>
<module>partial-response</module> <module>partial-response</module>
<module>eip-wire-tap</module> <module>eip-wire-tap</module>
<module>eip-splitter</module> <module>eip-splitter</module>
<module>eip-aggregator</module> <module>eip-aggregator</module>
<module>retry</module> <module>retry</module>
<module>trampoline</module>
</modules> </modules>
<repositories> <repositories>

View File

@ -12,13 +12,16 @@ tags:
--- ---
## Intent ## Intent
By representing a computation in one of 2 states Trampoline pattern is used for implementing algorithms recursively in Java without blowing the stack
and to interleave the execution of functions without hard coding them together
It is possible by representing a computation in one of 2 states : done | more
(completed with result, or a reference to the reminder of the computation, (completed with result, or a reference to the reminder of the computation,
something like the way a java.util.Supplier does) 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.
## Explanation
Trampoline pattern allows to define recursive algorithms by iterative loop.
## Applicability ## Applicability
Use the Trampoline pattern when Use the Trampoline pattern when
@ -32,9 +35,11 @@ The time overhead of a reflection call is traded for the space overhead of an in
Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class. Trampolines in Java usually involve the creation of a GenericListener to pass events to an outer class.
## Credits ## Tutorials
* [Trampolining: a practical guide for awesome Java Developers](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076) * [Trampolining: a practical guide for awesome Java Developers](https://medium.com/@johnmcclean/trampolining-a-practical-guide-for-awesome-java-developers-4b657d9c3076)
* [Trampoline in java ](http://mindprod.com/jgloss/trampoline.html) * [Trampoline in java ](http://mindprod.com/jgloss/trampoline.html)
* [cyclops-react](https://github.com/aol/cyclops-react)
## Credits
* [library 'cyclops-react' uses the pattern](https://github.com/aol/cyclops-react)

View File

@ -34,19 +34,16 @@
</parent> </parent>
<artifactId>trampoline</artifactId> <artifactId>trampoline</artifactId>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.16.18</version> <version>1.16.18</version>
<scope>provided</scope>
</dependency> </dependency>

View File

@ -2,13 +2,13 @@ package com.iluwatar.trampoline;
import java.util.stream.Stream; import java.util.stream.Stream;
/**When get is called on the returned Trampoline, internally it will iterate calling jump /**<p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
on the returned Trampoline as long as the concrete instance returned is More, * <p>When get is called on the returned Trampoline, internally it will iterate calling jump
stopping once the returned instance is Done. Essential we convert looping via recursion into iteration, on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)},
the key enabling mechanism is the fact that Trampoline.more is a lazy operation. stopping once the returned instance is {@link #done(Object)}.</p>
Trampoline in cyclops-react extends java.util.Supplier. Calling Trampoline.more we are basically creating <p>Essential we convert looping via recursion into iteration,
a Supplier that defers the actual recursive call, and having defered the call we can move it outside of the recursive loop. the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.</p>
This means we can define algorithms recursively in Java but execute them iteratively.*/ */
public interface Trampoline<T> { public interface Trampoline<T> {
T get(); T get();

View File

@ -1,17 +1,17 @@
/** /**
* The MIT License * The MIT License
* Copyright (c) 2014-2016 Ilkka Seppälä * Copyright (c) 2014-2016 Ilkka Seppälä
* * <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* * <p>
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* * <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * 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; import lombok.extern.slf4j.Slf4j;
/** /**
* <div> * <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
* By representing a computation in one of 2 states * <p>it is possible to implement algorithms recursively in Java without blowing the stack
(completed with result, or a reference to the reminder of the computation, * and to interleave the execution of functions without hard coding them together or even using threads.</p>
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.
</div>
<div>
Trampoline has 2 state : [done], [ more]
</div>
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.
*/ */
@Slf4j @Slf4j
@ -51,17 +37,18 @@ public class TrampolineApp {
public static void main(String[] args) { public static void main(String[] args) {
log.info("start pattern"); log.info("start pattern");
Integer result = loop(10, 1).result(); 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<Integer> loop(int times,int prod){ public static Trampoline<Integer> loop(int times, int prod) {
if(times==0) if (times == 0)
return Trampoline.done(prod); return Trampoline.done(prod);
else else
return Trampoline.more(()->loop(times-1,prod*times)); return Trampoline.more(() -> loop(times - 1, prod * times));
} }
} }

View File

@ -8,8 +8,9 @@ import static org.junit.Assert.*;
public class TrampolineAppTest { public class TrampolineAppTest {
@Test @Test
public void test()throws IOException{ public void testTrampolineWithFactorialFunction()throws IOException{
int result = TrampolineApp.loop(10, 1).result(); int result = TrampolineApp.loop(10, 1).result();
assertEquals("Be equal",3628800,result); assertEquals("Be equal",3628800,result);
} }