Answer the question
In order to leave comments, you need to log in
How to work with coroutines for reading and writing at the same time?
Actually the subject, let's say we have a coroutine:
foreach ($items as $item) {
yield $item;
}
// foreach (myfoo() as $item) ...
foreach ($items as &$item) {
$item = yield;
}
// while ($iterator = myfoo()) { $iterator->send(23); }
foreach ($items as &$item) {
$item = (yield $item);
}
// while ($iterator = myfoo()) { $iterator->current() === 42 ?: $iterator->send(23); }
foreach ($items as &$item) {
$item = yield (yield $item);
}
function myfoo(Generator $writer = null): Generator
{
foreach ($items as &$item) {
yield $item;
if ($writer !== null) {
$item = $writer->current(); // $writer->next() ?
}
}
}
// myfoo(yield ээээээ.... А дальше что писать? На этом мои полномочия всё (с)
foreach ($items as &$item) {
$data = (yield $item);
if ($data !== null) {
$item = $data;
yield $item;
}
}
$i = 0;
foreach ($reader = myfoo() as $value) {
echo ++$i . ') ' . $value . "\n";
$reader->send($i);
}
1) 2
2) 3
3) 4
и т.д
.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question