| @@ 5-54 (lines=50) @@ | ||
| 2 | ||
| 3 | namespace Collections\Iterator; |
|
| 4 | ||
| 5 | class LazyMapIterator implements \Iterator |
|
| 6 | { |
|
| 7 | /** |
|
| 8 | * @var \Iterator |
|
| 9 | */ |
|
| 10 | private $it; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @var callable |
|
| 14 | */ |
|
| 15 | private $fn; |
|
| 16 | ||
| 17 | public function __construct($it, $fn) |
|
| 18 | { |
|
| 19 | $this->it = $it; |
|
| 20 | $this->fn = $fn; |
|
| 21 | } |
|
| 22 | ||
| 23 | public function __clone() |
|
| 24 | { |
|
| 25 | $this->it = clone $this->it; |
|
| 26 | } |
|
| 27 | ||
| 28 | public function rewind() |
|
| 29 | { |
|
| 30 | $this->it->rewind(); |
|
| 31 | } |
|
| 32 | ||
| 33 | public function valid() |
|
| 34 | { |
|
| 35 | return $this->it->valid(); |
|
| 36 | } |
|
| 37 | ||
| 38 | public function next() |
|
| 39 | { |
|
| 40 | $this->it->next(); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function key() |
|
| 44 | { |
|
| 45 | return $this->it->key(); |
|
| 46 | } |
|
| 47 | ||
| 48 | public function current() |
|
| 49 | { |
|
| 50 | $fn = $this->fn; |
|
| 51 | ||
| 52 | return $fn($this->it->current()); |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| @@ 7-56 (lines=50) @@ | ||
| 4 | ||
| 5 | use Collections\KeyedIterator; |
|
| 6 | ||
| 7 | class LazyMapKeyedIterator implements KeyedIterator |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var KeyedIterator |
|
| 11 | */ |
|
| 12 | private $it; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var callable |
|
| 16 | */ |
|
| 17 | private $fn; |
|
| 18 | ||
| 19 | public function __construct($it, $fn) |
|
| 20 | { |
|
| 21 | $this->it = $it; |
|
| 22 | $this->fn = $fn; |
|
| 23 | } |
|
| 24 | ||
| 25 | public function __clone() |
|
| 26 | { |
|
| 27 | $this->it = clone $this->it; |
|
| 28 | } |
|
| 29 | ||
| 30 | public function rewind() |
|
| 31 | { |
|
| 32 | $this->it->rewind(); |
|
| 33 | } |
|
| 34 | ||
| 35 | public function valid() |
|
| 36 | { |
|
| 37 | return $this->it->valid(); |
|
| 38 | } |
|
| 39 | ||
| 40 | public function next() |
|
| 41 | { |
|
| 42 | $this->it->next(); |
|
| 43 | } |
|
| 44 | ||
| 45 | public function key() |
|
| 46 | { |
|
| 47 | return $this->it->key(); |
|
| 48 | } |
|
| 49 | ||
| 50 | public function current() |
|
| 51 | { |
|
| 52 | $fn = $this->fn; |
|
| 53 | ||
| 54 | return $fn($this->it->current()); |
|
| 55 | } |
|
| 56 | } |
|
| 57 | ||
| @@ 7-56 (lines=50) @@ | ||
| 4 | ||
| 5 | use Collections\KeyedIterator; |
|
| 6 | ||
| 7 | class LazyMapWithKeyIterator implements KeyedIterator |
|
| 8 | { |
|
| 9 | /** |
|
| 10 | * @var KeyedIterator |
|
| 11 | */ |
|
| 12 | private $it; |
|
| 13 | ||
| 14 | /** |
|
| 15 | * @var callable |
|
| 16 | */ |
|
| 17 | private $fn; |
|
| 18 | ||
| 19 | public function __construct($it, $fn) |
|
| 20 | { |
|
| 21 | $this->it = $it; |
|
| 22 | $this->fn = $fn; |
|
| 23 | } |
|
| 24 | ||
| 25 | public function __clone() |
|
| 26 | { |
|
| 27 | $this->it = clone $this->it; |
|
| 28 | } |
|
| 29 | ||
| 30 | public function rewind() |
|
| 31 | { |
|
| 32 | $this->it->rewind(); |
|
| 33 | } |
|
| 34 | ||
| 35 | public function valid() |
|
| 36 | { |
|
| 37 | return $this->it->valid(); |
|
| 38 | } |
|
| 39 | ||
| 40 | public function next() |
|
| 41 | { |
|
| 42 | $this->it->next(); |
|
| 43 | } |
|
| 44 | ||
| 45 | public function key() |
|
| 46 | { |
|
| 47 | return $this->it->key(); |
|
| 48 | } |
|
| 49 | ||
| 50 | public function current() |
|
| 51 | { |
|
| 52 | $fn = $this->fn; |
|
| 53 | ||
| 54 | return $fn($this->it->key(), $this->it->current()); |
|
| 55 | } |
|
| 56 | } |
|