Iterator Pattern Damian Gordon Iterator Pattern The iterator

  • Slides: 22
Download presentation
Iterator Pattern Damian Gordon

Iterator Pattern Damian Gordon

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • The iterator pattern is a design pattern in which an iterator

Iterator Pattern • The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements.

Iterator Pattern • In a programming language without patterns, an iterator would have a

Iterator Pattern • In a programming language without patterns, an iterator would have a next() method and a done() method, and the iterator loops across all the containers using these methods.

Iterator Pattern • In a programming language without patterns, an iterator would have a

Iterator Pattern • In a programming language without patterns, an iterator would have a next() method and a done() method, and the iterator loops across all the containers using these methods. WHILE NOT(iterator. done()) DO item = iterator. next() # do more stuff ENDWHILE;

Iterator Pattern • The iterator pattern in Python has the following features: Name _

Iterator Pattern • The iterator pattern in Python has the following features: Name _ _next_ _() Stop. Iteration() _ _iter_ _() Description The next method returns the next element from the container. The Stop. Iteration is an exception that is raised when the last element is reached. The iter method makes the object iterable, and returns an iterator.

Iterator Pattern • The iterator pattern has two parts: – An iterable class (to

Iterator Pattern • The iterator pattern has two parts: – An iterable class (to create the iterator object) – An iterator class (to traverse a container)

Iterable Pattern class ITERABLE: def __init__(self, VALUE): self. VALUE = VALUE # END Init

Iterable Pattern class ITERABLE: def __init__(self, VALUE): self. VALUE = VALUE # END Init def __iter__(self): return ITERATOR(self. VALUE) # END Iter # END ITERABLE.

Iterator Pattern class ITERATOR: def __init__(self, VALUE): self. VALUE = VALUE self. index =

Iterator Pattern class ITERATOR: def __init__(self, VALUE): self. VALUE = VALUE self. index = 0 # END Init def __iter__(self): return self # END Iter def __next__(self): if CONDITION: VALUE = SELF. VALUE self. index = self. index + 1 return VALUE else: raise Stop. Iteration() # ENDIF; # END Next # END ITERATOR.

Iterator Pattern • Let’s look at a simple example that counts numbers, there’ll be

Iterator Pattern • Let’s look at a simple example that counts numbers, there’ll be three parts: – The Iterable part – The Iteration part – The program execution part

Iterator Pattern class My. Count. Iterable: def __init__(self, Value): self. Value = Value #

Iterator Pattern class My. Count. Iterable: def __init__(self, Value): self. Value = Value # END Init def __iter__(self): return My. Count. Iteration(self. Value) # END Iter # END My. Count. Iterable.

Iterator Pattern class My. Count. Iteration: def __init__(self, Value): self. Index = 0 self.

Iterator Pattern class My. Count. Iteration: def __init__(self, Value): self. Index = 0 self. Value = Value # END Init def __iter__(self): # Iterators are iterables too. return self # END Iter def __next__(self): if self. Index < self. Value: # THEN Index = self. Index += 1 return Index else: raise Stop. Iteration() # ENDIF; # END Next # END My. Count. Iteration.

Iterator Pattern First. Count = My. Count. Iterable(5) list(First. Count) First. Count. Iter =

Iterator Pattern First. Count = My. Count. Iterable(5) list(First. Count) First. Count. Iter = iter(First. Count) while True: # DO try: print(next(First. Count. Iter)) except Stop. Iteration: break # ENDWHILE

Iterator Pattern First. Count = My. Count. Iterable(5) list(First. Count) First. Count. Iter =

Iterator Pattern First. Count = My. Count. Iterable(5) list(First. Count) First. Count. Iter = iter(First. Count) while True: # DO try: print(next(First. Count. Iter)) except Stop. Iteration: break # ENDWHILE 0 1 2 3 4

Iterator Pattern • Hang on… • Couldn’t we just do: for Counter in range(0,

Iterator Pattern • Hang on… • Couldn’t we just do: for Counter in range(0, 5): print(Counter) # ENDFOR; • YES!

etc.

etc.