2. SPOILER
Almost everything you can do with generators can be done with an Iterator
- Generators are just a handy way to create an iterator
- YIELD has the power
- Some magic
3. me!Simone Di Maulo
You can find me at @toretto460
Software Engineer
[“AdEspresso” | [“Kataskopeo” | [“Terravision” | [“Freelance”]]]]
7. We need to maintain internal index to track the current and next
pointer
rewind() just resets the index so that current() and next() will work
as expected.
Keys don’t have to be numeric!
Iterator implementation
8. “Generators provide an
easy way to implement
simple iterators without
the overhead or complexity
of implementing a class
that implements the
Iterator interface.”
generators
13. Coroutines are computer program components that
generalize subroutines for non preemptive multitasking,
by allowing multiple entry points for suspending and
resuming execution at certain locations.
COROUTINES
14. $gen = call_user_func(function (array $data) {
foreach ($data as $item) {
// do something before
yield $item;
// do something after
}
});
foreach ($gen as $item) {
doSomethingWith($item);
}
before $item0
doSomething($item0)
after $item0
before $item1
doSomething($item1)
after $item1