定制 hedronium/generator-nest 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

hedronium/generator-nest

Composer 安装命令:

composer require hedronium/generator-nest

包简介

Generator delegation support for PHP5.

README 文档

README

Generator delegation Meme

PHP7 has native support for Generator Delegation (yield from another_generator()) but many projects still are written in PHP5 or need to support PHP5 for compatability reasons.

This little package has a generator that allows for generator delegation in PHP5.

Installation

composer require hedronium/generator-nest

Usage

Consider that we have this generator.

function combinations($letters = [])
{
	$chars = ['a', 'b', 'c'];

	foreach ($chars as $char) {
		$merged = array_merge($letters, [$char]);

		yield implode('', $merged);

		if (count($letters) < 2) {
			yield combinations($merged);
		}
	}
}

If you try to iterate over a call to this generator like:

foreach (combinations() as $combination) {
	// your code
}

you'll recieve 3 strings and few generator objects as they are. This starts to be a problem because now in order to use this generator your calling code must be recursive which almsot defeats the whole purpose of using generators in the first place.

Enter Generator nest.

use Hedronium\GeneratorNest\GeneratorNest;

foreach (GeneratorNest::nested(combinations()) as $combination) {
	echo $combination, ' ';
}

now the output will simply be

a aa aaa aab aac ab aba abb abc ac aca acb acc b ba baa bab bac bb bba bbb bbc bc bca bcb bcc c ca caa cab cac cb cba cbb cbc cc cca ccb ccc

TADDA!

LICENSE

MIT.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固