2017 © Pedro Peláez
 

library tailrecursion

Tail Recursion Class

image

starship/tailrecursion

Tail Recursion Class

  • Sunday, February 23, 2014
  • by evan108108
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

TailRecursion

Tail-Recursion class for php 5.4 or greater, (*1)

Inspired by beberlei, (*2)

Install

The recommended way to install react/tailrecursion is through composer., (*3)

{
    "require": {
        "starship/tailrecursion": "dev-master"
    }
}

Examples

use \Starship\TailRecursion\TailRecursion as tr;

echo tr::init(function($n, $acc = 1) {
    if ($n == 1) {
        return $acc;
    }
    return $this->tail($n - 1, $acc * $n);
})->run(4);  // 1 * 2 * 3 * 4 = 24
use \Starship\TailRecursion\TailRecursion as tr;

$flatList = tr::init(function($list, $acc=[]) {
    if(count($list) < 1) {
        return $acc;
    }
    if(is_array($result = array_shift($list))) {    
         return $this->tail(array_merge($result, $list), $acc); 
    }
    $acc[] = $result;       
    return $this->tail($list, $acc);
});

//Will output [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print_r($flatList->run([1,[2,3],[4,[5]],[6], [7,[8,9],10]], [0])); 

The Versions

23/02 2014

dev-master

9999999-dev https://github.com/evan108108/TailRecursion

Tail Recursion Class

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Benjamin Eberlei
by Evan Frohlich

tail recursion