Created
January 16, 2019 08:47
-
-
Save deltam/2eb47482de7a37aff797d2dc15ccfe80 to your computer and use it in GitHub Desktop.
trampoline.php
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 | |
// JavaScript・再帰・トランポリン - Qiita | |
// https://qiita.com/41semicolon/items/985bdd2f551d9392463c | |
function countDown($num) { | |
if ($num == 1) return 1; | |
if ($num%10000 == 0) echo $num."\n"; | |
return countDown($num-1); | |
} | |
//countDown(650000); | |
function countDown2($num) { | |
if ($num == 1) return 1; | |
if ($num%10000 == 0) echo $num."\n"; | |
return function() use($num) { | |
return countDown2($num-1); | |
}; | |
} | |
function trampoline($fn) { | |
return function(...$args) use($fn) { | |
$result = $fn(...$args); | |
while ($result instanceof \Closure) { | |
$result = $result(); | |
} | |
return $result; | |
}; | |
} | |
$tramped = trampoline('countDown2'); | |
echo $tramped(650000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment