Instantly share code, notes, and snippets.

Embed
What would you like to do?
Single generator
<?php
$generatorWithTwoWayCommunication = function()
{
$randomValue = mt_rand();
yield $randomValue;
$receivedFromOuterSpace = yield;
yield $receivedFromOuterSpace * $receivedFromOuterSpace;
return $receivedFromOuterSpace * $randomValue;
};
$generator = $generatorWithTwoWayCommunication();
$valueFromFirstIteration = $generator->current();
$generator->next();
$valueFromSecondIteration = $generator->send(666);
$generator->next();
$valueReturnedFromGenerator = $generator->getReturn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment