<?php
lass MapIterator implements Iterator {
private $f;
private $inner;
public function __construct($f, $inner) {
$this->f = $f;
$this->inner = $inner;
}
function rewind() {
return $this->inner->rewind();
}
function current() {
return call_user_func($this->f, $this->inner->current());
}
function key() {
return $this->inner->key();
}
function next() {
return $this->inner->next();
}
function valid() {
return $this->inner->valid();
}
}
function iter_map($f, $iter) {
return new MapIterator($f, $iter);
}
// example:
function times_two($x) {
return $x * 2;
}
$source = new ArrayIterator([0, 1, 2, 3, 4, 5, 6]);
//var_dump(iterator_to_array(iter_map('times_two', $source)));
print_r(iterator_to_array(gen_map('times_two', $source)));
- Output for 5.4.0 - 5.4.22
- Parse error: syntax error, unexpected 'MapIterator' (T_STRING) in /in/97YFE on line 3
Process exited with code 255. - Output for 5.3.0 - 5.3.27
- Parse error: syntax error, unexpected T_STRING in /in/97YFE on line 3
Process exited with code 255.
preferences:
187.05 ms | 1395 KiB | 58 Q