ITERATORS AND GENERATORS 10

Similar documents
TAIL CALLS, ITERATORS, AND GENERATORS 11

ITERATORS AND STREAMS 9

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Iterators & Generators

ITERATORS, GENERATORS, GENERIC FUNCTIONS

ITERATORS, GENERATORS, GENERIC FUNCTIONS

CS61A Lecture 36. Soumya Basu UC Berkeley April 15, 2013

MEMOIZATION, RECURSIVE DATA, AND SETS

Advanced topics, part 2

Fall 2017 December 4, Scheme. Instructions

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

61A Lecture 32. Wednesday, November 14

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

Fall 2017 December 4, 2017

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Solutions to CS 61A Challenge Problems: Midpoint Review

STREAMS, ITERATORS, AND BINARY TREES 10

Object Oriented Programming in Python 3

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

Chapter 5 Conditional and Iterative Statements (Part-II) To carry out repetitive task, python provides following iterative/looping statements:

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

CircuitPython 101: Working with Lists, Iterators and Generators

Python Iterators. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. October 19, 2011

3.4. FOR-LOOPS 65. for <v a r i a b l e > in < sequence >:

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Python iterators and generators

Structure and Interpretation of Computer Programs

Lecture 17A: Finite and Infinite Iterators

LINKED LISTS AND MIDTERM REVIEW

TUPLES AND RECURSIVE LISTS 5

DSC 201: Data Analysis & Visualization

LINKED LISTS AND MIDTERM REVIEW 6

Python: Functions and Generators. Giuseppe Attardi

Programming Paradigms

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 24 Infinite Sequences LAZY EVALUATION AGENDA 8/7/2012. Jom Magrotker UC Berkeley EECS July 30, 2012

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

CS61A Lecture 24 Infinite Sequences. Jom Magrotker UC Berkeley EECS July 30, 2012

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

Control Structures 1 / 17

DSC 201: Data Analysis & Visualization

How async and await ended up in Python. EuroPython 2018, Edinburgh

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

PIC 16: Iterators and Generators

CS450 - Structure of Higher Level Languages

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

IAP Python - Lecture 2

Last week. Last week: We learned how to do simple procedural programming Python. This week: ow of control. for i in range(0,10): print i

Working with Lists 4

dnaseq.py Introduction to Algorithms Recitation 9b October 12, 2011

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Structure and Interpretation of Computer Programs Spring 2019 Midterm 2

RLISTS AND THE ENVIRONMENT MODEL 9

Introduction to Python! Lecture 2

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Structure and Interpretation of Computer Programs

TAIL RECURSION, SCOPE, AND PROJECT 4 11

INTERPRETERS AND TAIL CALLS 9

callback, iterators, and generators

STREAMS AND REVIEW 12

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CS 61A Orders of Growth & Linked Lists Spring 2018 Discussion 6: March 7, Warmup

Python Evaluation Rules

CSI33 Data Structures

CSI33 Data Structures

Be careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.

ECS Baruch Lab 3 Spring 2019 Name

CS61A Notes Week 13: Interpreters

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Conditionals and Recursion. Python Part 4

Control Flow: Loop Statements

Visualize ComplexCities

CSI33 Data Structures

Name & Recitation Section:

Structure and Interpretation of Computer Programs

Introduction to Python programming, II

AXIOMS FOR THE INTEGERS

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Structure and Interpretation of Computer Programs

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators

SCHEME AND CALCULATOR 5b

CONTROL AND ENVIRONMENTS 1

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Decorators in Python

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Structure and Interpretation of Computer Programs

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Lessons on Python Functions

CIS192 Python Programming

Midterm Exam 2B Answer key

CIS192 Python Programming

OOP and Scripting in Python Advanced Features

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Iterators and Generators

Structure and Interpretation of Computer Programs Summer 2014 Midterm 2

Transcription:

ITERATORS AND GENERATORS COMPUTER SCIENCE 6A July 23, 25 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only good for one pass through the sequence. The following is an example of a class that implements Python s iterator interface. This iterator calculates all of the natural numbers one-by-one, starting from zero: class Naturals(): def init (self): self.current = result = self.current self.current += return result return self An iterator is an object that has a next and an iter method.. next The next method checks if it has any values left in the sequence; if it does, it computes the next element. To return the next value in the sequence, the next method keeps track of its current position in the sequence. If there are no more values left to compute, it must raise an exception called StopIteration. This signals the end of the sequence. Note: the next method defined in the Naturals class does not raise StopIteration because there is no last natural number.

DISCUSSION : ITERATORS AND GENERATORS Page 2.2 iter The iter method returns an iterator object. If a class implements both a next method and an iter method, its iter method can simply return self as the class itself is an iterator. In fact, the Python docs require that all iterators iter methods must return self..3 Implementation When defining an iterator, you should always keep track of current position in the sequence. In the Naturals class, we use self.current to save the position. Iterator objects maintain state. Each successive call to next will return the next element, which may be different, so next is considered non-pure. Python has built-in functions called next and iter that call next and iter respectively. For example, this is how we could use the Naturals iterator: >>> nats = Naturals() >>> next(nats) >>> next(nats) >>> next(nats) 2.4 Questions. Define an iterator whose ith element is the result of combining the ith elements of two input iterators using some binary operator, also given as input. The resulting iterator should have a size equal to the size of the shorter of its two input iterators. >>> from operator import add >>> evens = IteratorCombiner(Naturals(), Naturals(), add) >>> next(evens) >>> next(evens) 2 >>> next(evens) 4

DISCUSSION : ITERATORS AND GENERATORS Page 3 class IteratorCombiner(object): def init (self, iterator, iterator2, combiner): 2. What is the result of executing this sequence of commands? >>> nats = Naturals() >>> doubled_nats = IteratorCombiner(nats, nats, add) >>> next(doubled_nats) >>> next(doubled_nats).5 Extra Question. Create an iterator that generates the sequence of Fibonacci numbers. class FibIterator(object): def init (self): return self

DISCUSSION : ITERATORS AND GENERATORS Page 4 2 Iterables An iterable object represents a sequence. Examples of iterables are lists, tuples, strings, and dictionaries. The iterable class must implement an iter method, which returns an iterator. Note that since all iterators have an iter method, they are all iterable. In general, a sequence s iter method will return a new iterator every time it is called. This is because an iterator cannot be reset. Returning a new iterator allows us to iterate through the same sequence multiple times. In the following example, we ve defined a simple iterable Range class, which represents the integers from to stop. class Range: def init (self, stop): self.stop = stop class RangeIterator: def init (self, stop): self.current = self.stop = stop return self return RangeIterator(self.stop) curr = self.current if curr >= self.stop: raise StopIteration self.current += return curr Iterables can be used in for loops and as arguments to functions that require a sequence (e.g. map and zip). For example: >>> for n in Range(2):... print(n)... This works because the for loop implicitly creates an iterator using the iter method. Python then repeatedly calls next repeatedly on the iterator, until it raises StopIteration. In other words, the loop above is (basically) equivalent to: range_iterator = iter(range(2)) is_done = False while not is_done: try: val = next(range_iterator) print(val) except StopIteration: is_done = True

DISCUSSION : ITERATORS AND GENERATORS Page 5 2. Questions. What would Python display in an interactive session? >>> range3 = Range(3) >>> for i in range3:... print(i)... >>> list(range3) >>> iterator3 = iter(range3) >>> list(iterator3) >>> list(iterator3) 2. To make the Link class iterable, implement the LinkIterator class. class Link: empty = () def init (self, first, rest=empty): self.first = first self.rest = rest return LinkIterator(self) class LinkIterator: def init (self, link):

DISCUSSION : ITERATORS AND GENERATORS Page 6 3 Generators A generator function is a special kind of Python function that uses a yield statement instead of a return statement to report values. When a generator function is called, it returns an iterable object. The following is a function that returns an iterator for the natural numbers: def generate_naturals(): current = while True: yield current current += Calling generate_naturals() will return a generator object, which you can use to retrieve values. >>> gen = generate_naturals() >>> gen <generator object gen at...> >>> next(gen) >>> next(gen) Think of a generator object as containing an implicit next method. This means, by definition, a generator object is an iterator. 3. yield The yield statement is similar to a return statement. However, while a return statement closes the current frame after the function exits, a yield statement causes the frame to be saved until the next time next is called, which allows the generator to automatically keep track of the iteration state. Once next is called again, execution resumes where it last stopped and continues until the next yield statement or the end of the function. A generator function can have multiple yield statements. Including a yield statement in a function automatically tells Python that this function will create a generator. When we call the function, it returns a generator object instead of executing the the body. When the generator s next method is called, the body is executed until the first yield statement.

DISCUSSION : ITERATORS AND GENERATORS Page 7 3.2 Implementation Because generators are technically iterators, you can implement iter methods using them. For example: class Naturals(): def init (self): self.current = while True: yield self.current self.current += Naturals s iter method now returns a generator object. The behavior of Naturals is exactly the same as before: >>> nats = Naturals() >>> nats_iterator = iter(nats) >>> next(nats_iterator) >>> next(nats_iterator) There are a couple of things to note: No next method in Naturals. iter only needs to return an iterator, and a generator is an iterator nats is a Naturals object and nats_iterator is a generator Generator objects are iterators, so they can be used in for loops 3.3 Questions. Define a generator that yields the sequence of perfect squares. The sequence of perfect squares looks like:, 4, 9, 6... def perfect_squares():

DISCUSSION : ITERATORS AND GENERATORS Page 8 2. To make the Link class iterable, implement the iter method using a generator. class Link: empty = () def init (self, first, rest=empty): self.first = first self.rest = rest 3.4 Extra Questions. Write a generator function that returns all subsets of the positive integers from to n. Each call to this generator s next method will return a list of subsets of the set [, 2,..., n], where n is the number of times next was previously called. def generate_subsets(): """ >>> subsets = generate_subsets() >>> for _ in range(3):... print(next(subsets))... [[]] [[], []] [[], [], [2], [, 2]] """