承接 bugadani/recursor 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bugadani/recursor

Composer 安装命令:

composer require bugadani/recursor

包简介

A simple library to enable quasi-recursive execution

README 文档

README

Recursor enabled execution of recursive algorithms in an iterative manner by utiliting PHP's generator feature. This is particularly useful, because there may be algorithms that are easy to implement recursively but hard iteratively. Also, PHP imposes an artificial nesting limit on recursion.

To transform a recursive function into a quasi-recursive one using Recursor, basically the only work that is needed is to replace return keywords with yield and recusrive function calls should also be prefixed with yield.

An example that will generate the nth Fibonacci-number:

$fibonacci = function ($x) use (&$fibonacci) {
    if ($x === 0) {
        yield 0;
    } else if ($x === 1) {
        yield 1;
    } else {
        $x1 = (yield $fibonacci($x - 1));   //retrieves return value of recursive call
        $x2 = (yield $fibonacci($x - 2));
        yield $x1 + $x2;                    //yielding a non-generator acts as a return
    }
};

$wrapped = new Recursor($fibonacci);

$wrapped(5); // returns 5
$wrapped(6); // returns 8

Notes

  • Recursor is built for PHP 5.5. This means that PHP7's generator-return is not supported. As a workaround, functions will "return" the first non-generator value it yields and because of this, they will not be able to generate sequences.
  • If you wish to yield a generator that should not be executed, you can wrap it in \IteratorIterator or a custom wrapper.

Downsides

Recursor relies heavily on generators. Each recursive call instantiates and executes a generator, which has a certain CPU and memory overhead. Also, the actual executor function is quite complicated which imposes even more overhead. Because of this, relying on Recurson in performance-sensitive applications is not recommended and an iterative solution should be implemented.

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固