| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace FunctionalPHP\Trampoline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This method should be used inside your recursive functions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * when you need to make a tail recursive call. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Instead of calling the function directly it will give back | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * the control to the trampoline which will in turn call your | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * function again in order to avoid a stack overflow. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @param callable $f a tail recursive function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @param array ...$args arguments for the function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @return callable a callable for the Trampoline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | function bounce(callable $f, ...$args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     return Trampoline::bounce($f, ...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Launch a trampoline. The given callable should be a tail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * recursive function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * All recursive calls inside the function should be made | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * using the bounce function so that the trampoline can | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * correctly avoid stack overflows. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @param callable $f a tail recursive function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @param array ...$args arguments for the function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @return mixed The final result of your function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | function trampoline(callable $f, ...$args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     return Trampoline::run($f, ...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | function trampoline_wrapper(callable $f) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     return function(...$args) use($f) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return Trampoline::run($f, ...$args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * Alternative method to get a tail recursive function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * without risk of stack overflows. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * All recursive calls inside the function should be made | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * by using the `$this` directly as a function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * @param callable $f a tail recursive function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  * @return callable equivalent function without stack overflow risk | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |  */ | 
            
                                                        
            
                                    
            
            
                | 56 |  |  | function pool(callable $f) | 
            
                                                        
            
                                    
            
            
                | 57 |  |  | { | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     return Pool::get($f); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  |