-
-
Save danilopinotti/57fc85b15a217266ff789faac48c2696 to your computer and use it in GitHub Desktop.
PHP Tail Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function tail($fn) | |
{ | |
$underCall = false; | |
$pool = []; | |
return function (...$args) use (&$fn, &$underCall, &$pool) { | |
$result = null; | |
$pool[] = $args; | |
if (!$underCall) { | |
$underCall = true; | |
while ($pool) { | |
$head = array_shift($pool); | |
$result = $fn(...$head); | |
} | |
$underCall = false; | |
} | |
return $result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment