Iterators - Programming With Anthony

Iterators – Programming With Anthony

Articles , , , , , 13 Comments

in general an iterator is nothing more than a sequence generator but what is a sequence at its most basic level the sequence is an ordered list of values values can be repeated in the list multiple times and their order does not need to be sorted the most basic type of sequence in programming is the array array elements can be any value and they can be positioned in any order but once they are set the array has a defined order if we loop over that array each loop will have the next item in the sequence until we run out of items that is if we looped over an array manually we would write a for loop let's see what's happening in that for loop the first step we set I to 0 then we check to see if I is less than the length of the array then we execute the body of the loop finally we increment I and go back to step 2 without realizing it every time you write a for loop like this you're building an iterator an iterator is just an abstraction of this for loop we have a rewind method which is basically the same as setting I to 0 we have a valid method which is the same as checking if I is less than the length of the array we have a next method which is the same as incrementing I and finally we have two functions useful in the body of the loop the key method which translates to I and the current method which gets the value of the array at the current position so our for loop can be rewritten as a for loop with iterators in reality that's all for each does internally anyway it except that for each exposés key and value automatically for us so our original for each call becomes this note that it looks identical to the first for each when using for each arrays and iterators are interchangeable so let's build our first actual iterator let's build the for loop but as an iterator instead first we need to make our class implement the iterator interface this interface defines the five methods that we will need to use but it also enables some magic behind the scenes that lets us use our object in for each statements other than that the rest of this class really looks nothing more than a verbose for statement it's worth noting that in practice we wouldn't write this class custom but would instead use the core array iterator class which basically does the same thing so now that we have the basic iterator concept let's talk about what we can do with it one of the benefits of using an iterator is that you can save significant amounts of memory by only generating values that are needed when they are needed for example let's say you want to generate a large integer sequence similar to the range call but we want it to be dynamic we want to save memory so we can build a iterator called X range which creates a sequence exactly like range except that it's only ever using a very small amount of fixed memory big benefit here is that the iterator can produce an infinite number of values while using constant memory another benefit of using an iterator is that we can decorate that iterator to provide additional functionality for example let's say we want to take our previous array and iterate over only even values with arrays we would need to use array filter and actually duplicate the array but with iterators we can just use the core callback filter iterator the cool part of this is that the original array structure is never changed or duplicated the iterator will just keep hitting the next method on the core iterator until it finds a value that's allowed PHP is SPL library contains a number of these decorators that you can use to fine tune in it orders behavior now one thing you likely noticed is that iterators are not typically small pieces of code their implementation is fairly verbose the interesting thing though is that to make your class traversable in a for each you can implement iterator but you can also implement it order aggregate the iterator aggregate interface allows you to instantiate a different iterator from your class so let's say your class has an array that you want to allow iteration over instead of building a new iterator in your class simply implement iterator aggregate and return an array iterator from the get iterator method we can now use an instance of our class in a for each iterators are a very powerful concept that if used correctly can lead to very efficient flexible and clean code thank you for listening and don't forget to subscribe new videos every Wednesday at noon eastern

13 thoughts on “Iterators – Programming With Anthony

Leave a Reply

Your email address will not be published. Required fields are marked *