I was perusing PHP7's features, and tried some examples of their enhanced "yield" in Perl6. The first was pretty easy, the second I haven't figured out. Curious what yinz can do.
Here's the first: ==php== <?php function gen() { yield 1; yield 2; yield from gen2(); } function gen2() { yield 3; yield 4; } foreach (gen() as $val) { echo $val, PHP_EOL; } ?> The above example will output: 1 2 3 4 ===p6=== (Condensed) sub gen {gather {take 1; take 2;.take for gen2();}} sub gen2 {gather {take 3; take 4}} .say for gen; ### This is the PHP example I'm having trouble doing the equivalent of in Perl6: ==PHP7=== Generator Return Expressions This feature builds upon the generator functionality introduced into PHP 5.5. It enables for a return statement to be used within a generator to enable for a final expression to be returned (return by reference is not allowed). This value can be fetched using the new Generator::getReturn() method, which may only be used once the generator has finishing yielding values. <?php $gen = (function() { yield 1; yield 2; return 3; })(); foreach ($gen as $val) { echo $val, PHP_EOL; } echo $gen->getReturn(), PHP_EOL; The above example will output: 1 2 3 Being able to explicitly return a final value from a generator is a handy ability to have. This is because it enables for a final value to be returned by a generator (from perhaps some form of coroutine computation) that can be specifically handled by the client code executing the generator. This is far simpler than forcing the client code to firstly check whether the final value has been yielded, and then if so, to handle that value specifically. ### -y