小编sna*_*ter的帖子

这个(类似蹦床的)构造是否有名字?

我想在创建类似于功能建议方法组合的系统时避免吹掉堆栈.这涉及树遍历(在我的实现中),条件递归等.可用于将递归转换为循环的极少数方法之一是蹦床.我试过这个然后发现我需要实现例如.短路布尔表达式评估.简而言之,我已经实现了蹦床与延续的组合,现在我试图找出这种结构是否存在以及它的名称是什么 - 因为我一直无法找到任何这样的现有结构.

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

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];
}
Run Code Online (Sandbox Code Playgroud)

Bounce类:

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();
    } …
Run Code Online (Sandbox Code Playgroud)

php recursion functional-programming trampolines

5
推荐指数
0
解决办法
145
查看次数