Skip to content

Instantly share code, notes, and snippets.

@deltam
Created January 16, 2019 08:47
Show Gist options
  • Save deltam/2eb47482de7a37aff797d2dc15ccfe80 to your computer and use it in GitHub Desktop.
Save deltam/2eb47482de7a37aff797d2dc15ccfe80 to your computer and use it in GitHub Desktop.
trampoline.php
<?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