37
Uncovering Iterators SJORS DE VALK 25 NOVEMBER 2010

Uncovering Iterators

  • Upload
    sdevalk

  • View
    968

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Uncovering Iterators

Uncovering Iterators

SJORS DE VALK25 NOVEMBER 2010

Page 2: Uncovering Iterators

ITERATORS (1)

∂ Black magic?

Page 3: Uncovering Iterators

ITERATORS (2)

∂ Holy grail?

Page 4: Uncovering Iterators

ITERATORS (3)

“An iterator is an object that allows a programmer to traverse through all the elements of a collection.”

∂ Wikipedia

Page 5: Uncovering Iterators

ITERATORS (4)

$i = array(1, 2, 3);reset($i);while (current($i) !== false) { echo key($values), current($values); next($values);}

∂ Array iteration (old school)

Page 6: Uncovering Iterators

ITERATORS (5)

$i = new MyIterator();$i->rewind();while ($i->valid()) { echo $i->key(), $i->current(); $i->next();}

Page 7: Uncovering Iterators

ITERATORS (6)

rewind() valid() key() current() next()

∂ As defined by the Iterator interface

Page 8: Uncovering Iterators

ITERATORS (7)

$i = new MyIterator();foreach ($i as $key => $value) { echo $key, $value;}

∂ Methods are called automatically

Page 9: Uncovering Iterators

BASICS (1)

$values = array( ‘Cameron Diaz’, ‘Alizée Jacotey’, ‘Britney Spears’, ‘Penélope Cruz’);

Page 10: Uncovering Iterators

BASICS (2)

class NaiveWomenIterator implements Iterator { function __construct(array $values) {...} function rewind() {...} function valid() {...} function key() {...} function current() {...} function next() {...}}

Page 11: Uncovering Iterators

BASICS (3)

class WomenIterator extends ArrayIterator { // Nothing here}

∂ Lean and mean

Page 12: Uncovering Iterators

BASICS (4)

$i = new WomenIterator($values);$i = new WomenFilterIterator($i);foreach ($i as $name) { echo $name;}

Page 13: Uncovering Iterators

BASICS (5)

class WomenFilterIterator extends FilterIterator { function accept() { return strpos($this->current(), ‘z’) !== false; }}

Page 14: Uncovering Iterators

FIBONACCI (1)

Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...

Page 15: Uncovering Iterators

FIBONACCI (2)

$previous = 1;$current = 0;while (true) { echo $current; $oldCurrent = $current; $current += $previous; $previous = $oldCurrent;}

∂ Classic approach

Page 16: Uncovering Iterators

FIBONACCI (3)

$i = new FibonacciIterator();foreach ($i as $value) { echo $value;}

∂ Iterator approach: hides the implementation

Page 17: Uncovering Iterators

FIBONACCI (4)

$i = new FibonacciIterator();$i = new LimitIterator($i, 0, 50);foreach ($i as $value) { echo $value;}

∂ No need to change the original iterator

Page 18: Uncovering Iterators

WORDS (1)

$contents = loadFile(‘http://www.gutenberg…’);$i = new NaiveWordIterator($contents);foreach ($i as $word) { echo $word;}

Page 19: Uncovering Iterators

WORDS (2)

$contents = loadFile(‘http://www.gutenberg…’);$i = new CharacterIterator($contents);$i = new WordIterator($i);foreach ($i as $word) { echo $word;}

Page 20: Uncovering Iterators

WORDS (3)

$contents = loadFile(‘http://www.gutenberg…’);$i = new CharacterIterator($contents);$i = new WordIterator($i);$i = new RegexIterator($i, ‘/god/i’);foreach ($i as $word) { echo $word;}

Page 21: Uncovering Iterators

WORDS (4)

$contents = loadFile(‘http://www.gutenberg…’);$i = new CharacterIterator($contents);$i = new WordIterator($i);$i = new RegexIterator($i, ‘/god/i’);$i = new BigWordsFilterIterator($i, 5);foreach ($i as $word) { echo $word;}

Page 22: Uncovering Iterators

WORDS (5)

$contents = loadFile(‘http://www.gutenberg…’);$i = new CharacterIterator($contents);$i = new WordIterator($i);$i = new WordFrequencyIterator($i);foreach ($i as $word) { echo $word;}

Page 23: Uncovering Iterators

WORDS (6)

$contents = loadFile(‘http://www.gutenberg…’);$i = new CharacterIterator($contents);$i = new WordIterator($i);$i = new BigWordsFilterIterator($i, 10);$i = new WordFrequencyIterator($i);foreach ($i as $word) { echo $word;}

Page 24: Uncovering Iterators

MP3 (1)

∂ Old school recursive directory iteration

function listFiles($path) { $files = array(); $handle = opendir($path); while (false !== ($file = readdir($handle))) { $files[] = $file; if (is_dir($path . ‘/’ . $file)) { $files = array_merge($files, listFiles($path . ‘/’ . $file)); } } return $files;}

Page 25: Uncovering Iterators

MP3 (2)

∂ Lean and mean. Returns SplFileInfo

$i = new Mp3RecursiveDirectoryIterator($path);foreach ($i as $file) { echo $file->getFilename();}

Page 26: Uncovering Iterators

MP3 (3)

$i = new Mp3RecursiveDirectoryIterator($path);

function render(Iterator $i) { echo $i->getDepth(), $i->getFilename(); return true;}

iterator_apply($i, ‘render’, array($i));

Page 27: Uncovering Iterators

MP3 (4)

$i = new Mp3RecursiveDirectoryIterator($path);

echo count($i); // Nopeecho $i->count(); // Nopeecho iterator_count($i);

Page 28: Uncovering Iterators

MP3 (5)

$i = new Mp3RecursiveDirectoryIterator($path);$i = new SongsIterator($i);

foreach ($i as $song) { echo $song->title;}

Page 29: Uncovering Iterators

MP3 (6)

$i = new Mp3RecursiveDirectoryIterator($path);$i = new SongsIterator($i);

foreach ($i as $song) { foreach ($song as $property) { echo $property; // E.g. title, artist }}

Page 30: Uncovering Iterators

MP3 (7)

∂ No need for a toArray()

class Song implements IteratorAggregate { function getIterator() { return new ArrayIterator( get_object_vars($this) ); }}

Page 31: Uncovering Iterators

MP3 (8)

$i = new Mp3RecursiveDirectoryIterator($path);$i = new Mp3ShortSongsFilterIterator($i);

foreach ($i as $file) { echo $file->getFilename();}

Page 32: Uncovering Iterators

MP3 (9)

$i = new Mp3RecursiveDirectoryIterator($path);$i = new Mp3ShortSongsFilterIterator($i);$i = new InfiniteIterator($i);

foreach ($i as $file) { echo $file->getFilename();}

Page 33: Uncovering Iterators

MOVIES (1)

$i = new ImdbTopMoviesIterator();$i = new LimitIterator($i, 1, 10);

foreach ($i as $movie) { echo $movie->rank, $movie->title;}

Page 34: Uncovering Iterators

MOVIES (2)

$i = new ImdbBoxOfficeMoviesIterator($url);$i = new LimitIterator($i, 1, 10);

foreach ($i as $movie) { echo $movie->rank, $movie->title;}

Page 35: Uncovering Iterators

MOVIES (3)

$x = new ImdbTopMoviesIterator();$x = new LimitIterator($x, 1, 10);$y= new ImdbBoxOfficeMoviesIterator($url);$y = new LimitIterator($y, 1, 10);

$i = new MultipleIterator();$i->attachIterator($x);$i->attachIterator($y);

Page 36: Uncovering Iterators

QUESTIONS?

Page 37: Uncovering Iterators

∂ THANKS!