Php 这个(类似蹦床的)结构有名字吗?

Php 这个(类似蹦床的)结构有名字吗?,php,recursion,functional-programming,trampolines,Php,Recursion,Functional Programming,Trampolines,我希望在创建类似于或的系统时避免破坏堆栈。这涉及到树遍历(在我的实现中)、条件递归等。将递归转换为循环的极少数方法之一是蹦床。我试过了,然后发现我需要实现短路布尔表达式求值。简言之,我已经实现了蹦床和连续体的组合,现在我试图找出这个结构是否存在,它的名字可能是什么——因为我一直无法找到任何这样的已经存在的结构 我的实现-使用手动堆栈处理进行反弹评估: function immediate($bounce, $args) { $stack = array($bounce->run($

我希望在创建类似于或的系统时避免破坏堆栈。这涉及到树遍历(在我的实现中)、条件递归等。将递归转换为循环的极少数方法之一是蹦床。我试过了,然后发现我需要实现短路布尔表达式求值。简言之,我已经实现了蹦床和连续体的组合,现在我试图找出这个结构是否存在,它的名字可能是什么——因为我一直无法找到任何这样的已经存在的结构

我的实现-使用手动堆栈处理进行反弹评估:

function immediate($bounce, $args)
{
    $stack = array($bounce->run($args));

    while ($stack[0] instanceof Bounce) {
        $current = array_pop($stack);
        if ($current instanceof Bounce) {
            $stack[] = $current;
            $stack[] = $current->current();
        } else {
            $next = array_pop($stack);
            $stack[] = $next->next($current);
        }
    }

    return $stack[0];
}
弹跳类:

class Bounce
{
    protected $current;
    protected $next;

    public function __construct($current, $next)
    {
        $this->current = $current;
        $this->next = $next;
    }

    public function current()
    {
        $fn = $this->current;
        return $fn();
    }

    public function next($arg)
    {
        $fn = $this->next;
        return $fn($arg);
    }
}
例如,短路和实现(即在JavaScript
first(args)和&second(args)
中)
$first
$second
也是可以返回
弹跳的函数

return new Bounce(
    function () use ($first, $args) {
        return $first($args);
    },
    function ($return) use ($second, $args) {
        if (!$return) {
            return $return;
        }
        return new Bounce(
            function () use ($second, $args) {
                return $second($args);
            },
            null
        );
    }
);
这允许一般递归,每个普通函数调用大约有3个函数调用的开销(尽管在变量迭代计数的一般情况下,编写非常麻烦,需要“延迟递归”)

以前有人见过这样的建筑吗