|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.iluwatar.trampoline; |
| 25 | + |
| 26 | + |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | + |
| 29 | +/** |
| 30 | + * <div> |
| 31 | + * By representing a computation in one of 2 states |
| 32 | + (completed with result, or a reference to the reminder of the computation, |
| 33 | + something like the way a java.util.Supplier does) |
| 34 | + it is possible to implement algorithms recursively in Java without blowing the stack |
| 35 | + and to interleave the execution of functions without hard coding them together or even using threads. |
| 36 | + </div> |
| 37 | + <div> |
| 38 | + Trampoline has 2 state : [done], [ more] |
| 39 | + </div> |
| 40 | + When get is called on the returned Trampoline, internally it will iterate calling ‘jump’ |
| 41 | + on the returned Trampoline as long as the concrete instance returned is More, |
| 42 | + stopping once the returned instance is Done. Essential we convert looping via recursion into iteration, |
| 43 | + the key enabling mechanism is the fact that Trampoline.more is a lazy operation. |
| 44 | + Trampoline in cyclops-react extends java.util.Supplier. Calling Trampoline.more we are basically creating |
| 45 | + a Supplier that defers the actual recursive call, and having defered the call we can move it outside of the recursive loop. |
| 46 | + This means we can define algorithms recursively in Java but execute them iteratively. |
| 47 | + */ |
| 48 | + |
| 49 | +@Slf4j |
| 50 | +public class TrampolineApp { |
| 51 | + public static void main(String[] args) { |
| 52 | + log.info("start pattern"); |
| 53 | + Integer result = loop(10, 1).result(); |
| 54 | + log.info("result {}" ,result); |
| 55 | + |
| 56 | + } |
| 57 | + /** |
| 58 | + * Manager for pattern. |
| 59 | + * */ |
| 60 | + public static Trampoline<Integer> loop(int times,int prod){ |
| 61 | + if(times==0) |
| 62 | + return Trampoline.done(prod); |
| 63 | + else |
| 64 | + return Trampoline.more(()->loop(times-1,prod*times)); |
| 65 | + } |
| 66 | + |
| 67 | +} |
0 commit comments