2017 © Pedro Peláez
 

library recursor

A simple library to enable quasi-recursive execution

image

bugadani/recursor

A simple library to enable quasi-recursive execution

  • Tuesday, May 10, 2016
  • by bugadani
  • Repository
  • 1 Watchers
  • 1 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Recursor

Recursor enabled execution of recursive algorithms in an iterative manner by utiliting PHP's generator feature. This is particularly useful, because there may be algorithms that are easy to implement recursively but hard iteratively. Also, PHP imposes an artificial nesting limit on recursion., (*1)

To transform a recursive function into a quasi-recursive one using Recursor, basically the only work that is needed is to replace return keywords with yield and recusrive function calls should also be prefixed with yield., (*2)

An example that will generate the nth Fibonacci-number:, (*3)

$fibonacci = function ($x) use (&$fibonacci) {
    if ($x === 0) {
        yield 0;
    } else if ($x === 1) {
        yield 1;
    } else {
        $x1 = (yield $fibonacci($x - 1));   //retrieves return value of recursive call
        $x2 = (yield $fibonacci($x - 2));
        yield $x1 + $x2;                    //yielding a non-generator acts as a return
    }
};

$wrapped = new Recursor($fibonacci);

$wrapped(5); // returns 5
$wrapped(6); // returns 8

Notes

  • Recursor is built for PHP 5.5. This means that PHP7's generator-return is not supported. As a workaround, functions will "return" the first non-generator value it yields and because of this, they will not be able to generate sequences.
  • If you wish to yield a generator that should not be executed, you can wrap it in \IteratorIterator or a custom wrapper.

Downsides

Recursor relies heavily on generators. Each recursive call instantiates and executes a generator, which has a certain CPU and memory overhead. Also, the actual executor function is quite complicated which imposes even more overhead. Because of this, relying on Recurson in performance-sensitive applications is not recommended and an iterative solution should be implemented., (*4)

The Versions

10/05 2016

v2.x-dev

2.9999999.9999999.9999999-dev

A simple library to enable quasi-recursive execution

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Dániel Buga

02/05 2016

v2.0-beta

2.0.0.0-beta

A simple library to enable quasi-recursive execution

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Dániel Buga

08/02 2016

dev-master

9999999-dev

A simple library to enable quasi-recursive execution

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Dániel Buga

08/02 2016

v1.1

1.1.0.0

A simple library to enable quasi-recursive execution

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Dániel Buga

07/02 2016

v1.0

1.0.0.0

A simple library to enable quasi-recursive execution

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Dániel Buga