I once had a function like the following:
function f(array $xs, &$responsesOut): array { // note the &
$ys = [];
$responsesOut = [];
foreach ($xs as $x) {
$response = getResponse($x);
$ys[] = extractYFromResponse($response);
$responsesOut[] = $response;
}
return $ys;
}
This worked well, until I ran into memory problems. So I used a Generator for f
with respect to the $ys
:
function f(array $xs, &$responsesOut): iterable {
$responsesOut = [];
foreach ($xs as $x) {
$response = getResponse($x);
yield extractYFromResponse($response);
$responsesOut[] = $response;
}
}
This works, but obviously not with respect to $responsesOut
(as this part is unchanged).
What I now want to do is to bind a Generator (instead of an array) to $responsesOut
. But given that there is no new Generator()
, I cannot figure out how to do this. Maybe you can help me?