starship/tailrecursion

dev-master 2014-02-23 03:02 UTC

This package is not auto-updated.

Last update: 2024-04-27 14:33:12 UTC


README

Tail-Recursion class for php 5.4 or greater

Inspired by beberlei

Install

The recommended way to install react/tailrecursion is through composer.

{
    "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]));