Blueberry icon indicating copy to clipboard operation
Blueberry copied to clipboard

Generator comprehensions

Open hikari-no-yume opened this issue 10 years ago • 5 comments

Like list comprehensions, but produces a generator. Python has these.

I know @gosukiwi stated that we don't want to require PHP 5.5+, but it wouldn't stop people with 5.4 using Blueberry, they just couldn't use generator comprehensions.

Ideal syntax:

  • [2 * i for i in [1..10] where i % 2 == 0] - list comprehension
  • (2 * i for i in [1..10] where i % 2 == 0) - generator comprehension

hikari-no-yume avatar Jan 10 '15 17:01 hikari-no-yume

I love lazy collections. Generator comprehensions could easily be implemented on PHP 5.5+ using yield.

result = function($collection) {
    foreach($collection as $item) {
         yield $item;
    }
}

Something I really like of PHP 5.4 is Traits, which would be an elegant way to implement Ruby-like modules. I'll leave this pull request open so anyone willing to implement Generator Comprehensions can submit a pull request :smile:

gosukiwi avatar Jan 10 '15 17:01 gosukiwi

Generators are a 5.5 thing, not 5.4.

hikari-no-yume avatar Jan 10 '15 17:01 hikari-no-yume

My bad. Fixed it.

gosukiwi avatar Jan 10 '15 17:01 gosukiwi

Well, Traits are a 5.4 thing. ;)

hikari-no-yume avatar Jan 10 '15 18:01 hikari-no-yume

Them PHP versions :stuck_out_tongue:

gosukiwi avatar Jan 10 '15 18:01 gosukiwi