-
-
Save ak4n/1da4432b638a7c1edc0c to your computer and use it in GitHub Desktop.
Revisions
-
igorw revised this gist
Dec 6, 2013 . 1 changed file with 14 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,56 +1,39 @@ <?php // iterator impl: class MapIterator extends IteratorIterator { private $f; public function __construct($f, $inner) { parent::__construct($inner); $this->f = $f; } function current() { return call_user_func($this->f, parent::current()); } } function iter_map($f, $iter) { return new MapIterator($f, $iter); } // generator impl: function gen_map($f, $iter) { foreach ($iter as $k => $v) { yield $k => $f($v); } } // 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))); var_dump(iterator_to_array(gen_map('times_two', $source))); -
igorw revised this gist
Dec 5, 2013 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ <?php // iterator impl: class MapIterator implements Iterator { private $f; private $inner; @@ -34,12 +36,16 @@ function iter_map($f, $iter) { return new MapIterator($f, $iter); } // generator impl: function gen_map($f, $iter) { foreach ($iter as $k => $v) { yield $k => $f($v); } } // example: function times_two($x) { return $x * 2; } -
igorw created this gist
Dec 5, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ <?php class 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); } function gen_map($f, $iter) { foreach ($iter as $k => $v) { yield $k => $f($v); } } 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))); var_dump(iterator_to_array(gen_map('times_two', $source)));