|
1 | 1 | /**
|
2 | 2 | * The MIT License
|
3 | 3 | * Copyright (c) 2014-2016 Ilkka Seppälä
|
4 |
| - * |
| 4 | + * <p> |
5 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 | 6 | * of this software and associated documentation files (the "Software"), to deal
|
7 | 7 | * in the Software without restriction, including without limitation the rights
|
8 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 | 9 | * copies of the Software, and to permit persons to whom the Software is
|
10 | 10 | * furnished to do so, subject to the following conditions:
|
11 |
| - * |
| 11 | + * <p> |
12 | 12 | * The above copyright notice and this permission notice shall be included in
|
13 | 13 | * all copies or substantial portions of the Software.
|
14 |
| - * |
| 14 | + * <p> |
15 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
27 | 27 | import lombok.extern.slf4j.Slf4j;
|
28 | 28 |
|
29 | 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. |
| 30 | + * <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p> |
| 31 | + * <p>it is possible to implement algorithms recursively in Java without blowing the stack |
| 32 | + * and to interleave the execution of functions without hard coding them together or even using threads.</p> |
47 | 33 | */
|
48 | 34 |
|
49 | 35 | @Slf4j
|
50 | 36 | public class TrampolineApp {
|
51 | 37 | public static void main(String[] args) {
|
52 | 38 | log.info("start pattern");
|
53 | 39 | Integer result = loop(10, 1).result();
|
54 |
| - log.info("result {}" ,result); |
| 40 | + log.info("result {}", result); |
55 | 41 |
|
56 | 42 | }
|
| 43 | + |
57 | 44 | /**
|
58 |
| - * Manager for pattern. |
| 45 | + * Manager for pattern. Define it with a factorial function. |
59 | 46 | * */
|
60 |
| - public static Trampoline<Integer> loop(int times,int prod){ |
61 |
| - if(times==0) |
| 47 | + public static Trampoline<Integer> loop(int times, int prod) { |
| 48 | + if (times == 0) |
62 | 49 | return Trampoline.done(prod);
|
63 | 50 | else
|
64 |
| - return Trampoline.more(()->loop(times-1,prod*times)); |
| 51 | + return Trampoline.more(() -> loop(times - 1, prod * times)); |
65 | 52 | }
|
66 | 53 |
|
67 | 54 | }
|
0 commit comments