- array_reverse: documentation ( source)
- var_export: documentation ( source)
- range: documentation ( source)
- is_callable: documentation ( source)
<?php
// Everything you enter here will be executed by our servers. Try it!
function iterator_compose()
{
foreach (array_reverse(func_get_args()) as $it) {
$prev = null;
if (is_callable($it)) {
$prev = $it($prev);
} else if (is_object($it)) {
$prev = $it;
} else {
$prev = new $it($prev);
}
}
return $prev;
}
function iterator_chunk(\Traversable $it, $chunkSize)
{
$it->rewind();
$chunk = [];
$steps = range(0, $chunkSize);
while (true) {
foreach ($steps as $_) {
if (!$it->valid()) {
break 2;
}
$chunk[] = $it->current();
$it->next();
}
yield $chunk;
$chunk = [];
}
if ($chunk) {
yield $chunk;
}
}
$it = iterator_chunk(
new ArrayIterator(range(0, 125))
, 32);
foreach ($it as $i => $c) {
echo "====== Chunk $i ======\n";
var_export($c);
echo "\n";
}