Skip to content

Instantly share code, notes, and snippets.

@pedrochaves
Last active August 29, 2015 14:20
Show Gist options
  • Save pedrochaves/74f49f729c2ce0dcf217 to your computer and use it in GitHub Desktop.
Save pedrochaves/74f49f729c2ce0dcf217 to your computer and use it in GitHub Desktop.
Generators + splat operator example
<?php
function generateNumbers($limit)
{
foreach (range(1, $limit) as $n) {
echo $n, ':';
yield $n;
}
}
function doWithSplat(...$numbers)
{
foreach ($numbers as $number) {
echo $number * 2, PHP_EOL;
}
}
function doWithoutSplat($numbers)
{
foreach ($numbers as $number) {
echo $number * 2, PHP_EOL;
}
}
// This executes the generator all the way before passing to the function
doWithSplat(...generateNumbers(10));
// This actually works as a generator
doWithoutSplat(generateNumbers(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment