2017 © Pedro Peláez
 

library generator-nest

Generator delegation support for PHP5.

image

hedronium/generator-nest

Generator delegation support for PHP5.

  • Thursday, December 22, 2016
  • by hedronium
  • Repository
  • 3 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

GeneratorNest

Generator delegation Meme, (*1)

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., (*2)

This little package has a generator that allows for generator delegation in PHP5., (*3)

Installation

composer require hedronium/generator-nest

Usage

Consider that we have this generator., (*4)

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:, (*5)

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., (*6)

Enter Generator nest., (*7)

use Hedronium\GeneratorNest\GeneratorNest;

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

now the output will simply be, (*8)

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!, (*9)

LICENSE

MIT., (*10)

The Versions

22/12 2016

dev-master

9999999-dev

Generator delegation support for PHP5.

  Sources   Download

MIT

22/12 2016

v1.0

1.0.0.0

Generator delegation support for PHP5.

  Sources   Download

MIT