2017 © Pedro Peláez
 

library trampoline

Trampoline implementation for PHP.

image

functional-php/trampoline

Trampoline implementation for PHP.

  • Saturday, January 27, 2018
  • by krtek
  • Repository
  • 2 Watchers
  • 16 Stars
  • 631 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 67 % Grown

The README.md

Trampoline

Build Status Scrutinizer Code Quality Code Coverage Average time to resolve an issue Percentage of issues still open Chat on Gitter, (*1)

Trampolines are a technique used to avoid blowing the call stack when doing recursive calls. This is needed because PHP does not perform tail-call optimization., (*2)

For more information about what is tail-call optimization (or TCO), you can read : http://stackoverflow.com/questions/310974/what-is-tail-call-optimization#answer-310980, (*3)

For a more in depth definition of trampolines and recursion as a whole, I can recommend you read http://blog.moertel.com/posts/2013-06-12-recursion-to-iteration-4-trampolines.html which is using Python but should be easy enough to understand., (*4)

Installation

composer require functional-php/trampoline

Basic Usage

If we have the following recursive function:, (*5)

<?php

function factorial($n, $acc = 1) {
    return $n <= 1 ? $acc : factorial($n - 1, $n * $acc);
};

echo factorial(5);
// 120

We need to simply replace the recursive call by a call to the bounce function:, (*6)

``` php <?php, (*7)

use FunctionalPHP\Trampoline as T;, (*8)

function factorial($n, $acc = 1) { return $n <= 1 ? $acc : T\bounce('factorial', $n - 1, $n * $acc); };, (*9)

echo T\trampoline('factorial', 5); // 120, (*10)


The `bounce` and `trampoline` functions accepts anything that is deemed a valid [`callable`](http://php.net/manual/en/language.types.callable.php) by PHP. The `trampoline` function will also accept an instance of `Trampoline` created by `bounce` but will ignore any arguments in this case. ## Helpers You can also call statically any function from the global namespace on the `Trampoline` class if you prefer this style: ``` php <?php use FunctionalPHP\Trampoline\Trampoline; echo Trampoline::factorial(5); // 120 echo Trampoline::strtoupper('Hello!'); // HELLO!

This will however not work for functions inside a namespace., (*11)

If you want to have a ready to call function when using a trampoline, you can use the trampoline_wrapper helper. It will create a wrapper function that will call trampoline for you and return the result., (*12)

``` php <?php, (*13)

use FunctionalPHP\Trampoline as T;, (*14)

function factorial($n, $acc = 1) { return $n <= 1 ? $acc : T\bounce('factorial', $n - 1, $n * $acc); };, (*15)

$fact = T\trampoline_wrapper('factorial');, (*16)

echo $fact(5); // 120, (*17)


## Alternative method The library also contain an alternative implementation to run tail recursive function without risking a stack overflow based on an argument queue instead of a trampoline. You will need to use the `$this` variable as the recursive function. Here is the factorial example using this second method. ``` php <?php use FunctionalPHP\Trampoline as T; $fact = T\pool(function($n, $acc = 1) { return $n <= 1 ? $acc : $this($n - 1, $n * $acc); }); echo $fact(5); // 120

At this time, only anonymous functions (ie instance of the Closure class) are supported. But as soon as PHP 7.1 is released you will be able to use any callable., (*18)

From a performance standpoint, there is no measurable difference between the two approaches., (*19)

Testing

You can run the test suite for the library using:, (*20)

composer test

A test report will be available in the reports directory., (*21)

Contributing

There shouldn't be much to contribute except the potential bug but any kind of contribution are welcomed! Do not hesitate to open an issue or submit a pull request., (*22)

The Versions

27/01 2018

dev-master

9999999-dev

Trampoline implementation for PHP.

  Sources   Download

BSD BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

functional recursion trampoline

25/10 2016

1.1.0

1.1.0.0

Trampoline implementation for PHP.

  Sources   Download

BSD

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

functional recursion trampoline

24/10 2016

1.0.0

1.0.0.0

Trampoline implementation for PHP.

  Sources   Download

BSD

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

functional recursion trampoline