CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012.

Slides:



Advertisements
Similar presentations
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advertisements

Chapter 20 Recursion.
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
ZMQS ZMQS
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
CS61A Lecture 24 Infinite Sequences
CS61A Lecture 26 Logic Programming Jom Magrotker UC Berkeley EECS August 1, 2012.
Solve Multi-step Equations
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Turing Machines.
Page 1 of 30 To the Create Assignment Request Online Training Course An assignment request is created by an assignor to initiate the electronic assignment.
Data Structures: A Pseudocode Approach with C
Modern Programming Languages, 2nd ed.
ABC Technology Project
Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
EU Market Situation for Eggs and Poultry Management Committee 21 June 2012.
3 Logic The Study of What’s True or False or Somewhere in Between.
1 Undirected Breadth First Search F A BCG DE H 2 F A BCG DE H Queue: A get Undiscovered Fringe Finished Active 0 distance from A visit(A)
VOORBLAD.
15. Oktober Oktober Oktober 2012.
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
© 2012 National Heart Foundation of Australia. Slide 2.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS 240 Computer Programming 1
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Slippery Slope
Introduction to Computability Theory
Januar MDMDFSSMDMDFSSS
Week 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
Essential Cell Biology
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Towers of Hanoi
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
CS1022 Computer Programming & Principles
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
CS61A Lecture 21 Scheme Jom Magrotker UC Berkeley EECS July 24, 2012.
CS61A Lecture 12 Immutable Trees Jom Magrotker UC Berkeley EECS July 9, 2012.
CS61A Lecture 27 Logic Programming Jom Magrotker UC Berkeley EECS August 2, 2012.
CS61A Lecture 20 Object-Oriented Programming: Implementation Jom Magrotker UC Berkeley EECS July 23, 2012.
CS61A Lecture 9 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 2, 2012.
CS61A Lecture 10 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 3, 2012.
CS61A Lecture 11 Immutable Trees Jom Magrotker UC Berkeley EECS July 5, 2012.
Presentation transcript:

CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012

2 C OMPUTER S CIENCE IN THE N EWS

3 T ODAY Review Iterators Review Streams Another way of defining a sequence of values using delayed evaluation: Generators

4 R EVIEW : I TERATORS Python natively supports iterators. Iterators are objects that give out one item at a time and save the next item until they are asked: this evaluation is lazy.

5 R EVIEW : I TERATORS Python has the following interface for iterators: The __iter__ method should return an iterator object. The __next__ method should: – return a value, or – raise a StopIteration when the end of the sequence is reached, and on all subsequent calls.

6 R EVIEW : I TERATORS What does a for -loop do under the hood? for x in iterable: function(x) is equivalent to iterator = iterable.__iter__() try: while True: element = iterator.__next__() function(element) except StopIteration as e: pass Create an iterator. Try to get an element from the iterator. Apply the function on the element. If we could not get an element, we catch the StopIteration exception and do not apply the function.

7 A NNOUNCEMENTS Homework 12 due Today. Homework 13 due Friday. – Out later today. – Includes Py, Streams, Iterators, and Generators – Also includes the Project 4 contest. Project 4 due Tuesday, August 7. – Partnered project, in two parts. – Twelve questions, so please start early! – Two extra credit questions.

8 A NNOUNCEMENTS Project 4 contest due Friday, August 3. – Generate recursive art using Scheme. – Prizes awarded in two categories: Featherweight: At most 128 words of Scheme. Heavyweight: At most 1024 words of Scheme. – One question on homework 14 will ask you to vote for your favorite drawing. – Extra credit point to the top 3 in each category. – Prize: Logicomix

9 A NNOUNCEMENTS : M IDTERM 2 Scores available on glookup. – Average: 39.9, standard deviation: 6.9. Solutions are available online. – Regrade requests due Tuesday, August 7. Post-midterm de-stress potluck this week. – Food and games. – Come and leave when you want.

10 A NNOUNCEMENTS : F INAL Final is Thursday, August 9. – Where? 1 Pimentel. – When? 6PM to 9PM. – How much? All of the material in the course, from June 18 to August 8, will be tested. Closed book and closed electronic devices. One 8.5 x 11 cheat sheet allowed. No group portion. We will get back to you this week if you have conflicts and have told us. If you havent told us yet, please let us know.

11 R EVIEW : S TREAMS Streams are lazily computed recursive lists that represent (potentially infinite) sequences. Like a recursive list, a stream is a pair: the first element is the first element of the stream, the second element stores how to compute the rest of the stream when needed, and will compute it when asked.

12 How to compute the rest of the stream. R EVIEW : S TREAMS First element of the stream The stream promises to compute the rest when you ask for it.

13 R EVIEW : S TREAMS First element of the stream Second element of the stream

14 R EVIEW : S TREAMS class Stream(object): def __init__(self, first, compute_rest, empty=False): self.first = first self._compute_rest = compute_rest self.empty = empty self._rest = None self._computed = False First element of the stream How to compute the rest of the stream when needed Is this stream empty? Has the rest of this stream already been computed?

15 R EVIEW : S TREAMS class def rest(self): assert not self.empty, \ Empty streams have no rest. if not self._computed: self._rest = self._compute_rest() self._computed = True return self._rest Stream.the_empty_stream = Stream(None, None, True) If the rest of the stream has not been computed yet compute it and remember the result.

16 P RACTICE : S TREAMS What are the first 9 elements of the stream s defined below? >>> s = Stream(1, lambda: add_streams(s, s)) ???

17 Position Computation1 s[0] + s[0] s[1] + s[1] s[2] + s[2] s[3] + s[3] s[4] + s[4] s[5] + s[5] s[6] + s[6] s[7] + s[7] Value P RACTICE : S TREAMS What are the first 9 elements of the stream s defined below? >>> s = Stream(1, lambda: add_streams(s, s)) S S S + S

18 E XAMPLE : S TREAMS Say I wanted to take two input streams and return a stream which alternates between items from each of the input streams. Lets call this interleave(s1, s2). The way it should behave is like this: >>> ones = Stream(1, lambda: ones) >>> ints = Stream(1, lambda: add_streams(ones, ints)) >>> mixed = interleave(ones, ints) >>> show_stream(mixed) 1, 1, 1, 2, 1, 3, 1, 4, 1, 5,...

19 E XAMPLE : S TREAMS Say I wanted to take two input streams and return a stream which alternates between items from each of the input streams. Lets call this interleave(s1, s2). How do we implement it? def interleave(s1, s2): return Stream(s1.first, lambda: interleave(s2, s1.rest)) The result of interleaving s1 and s2 is... the first item of s1... followed by items of s2 interleaved with the remaining items of s1.

20 P RACTICE : S TREAMS Say I wanted to create ab_stream, which is a stream that contains all strings created using some number of a and b strings, including the empty string. Using stream_map and interleave, create ab_stream. add_a = lambda x: x + a add_b = lambda x: x + b ab_stream = Stream(, ???)

21 P RACTICE : S TREAMS Say I wanted to create ab_stream, which is a stream that contains all strings created using some number of a and b strings, including the empty string. Using stream_map and interleave, create ab_stream. add_a = lambda x: x + a add_b = lambda x: x + b ab_stream = \ Stream(, lambda: interleave(stream_map(add_a, ab_stream), stream_map(add_b, ab_stream))) Start with the empty string. Alternate between... items where we add an a... items where we add a b...

22 B REAK

23 G ENERATORS Generators are an elegant and concise way to define an iterator. Example: Make an iterator that returns the sequence [0], [0, 1], [0, 1, 2],... def triangle(): lst, cur = [], 0 while True: yield lst + [cur] lst, cur = lst + [cur], cur + 1 for lst in triangle(): print(lst)

24 G ENERATORS : E XPLAINED...wait, what? Generators use the yield keyword. When a function definition includes a yield statement, Python knows that a call to the function should return a generator object.

25 G ENERATORS : E XPLAINED To produce each item, Python starts executing code from the body of the function. If a yield statement is reached, Python stops the function there and yields the value as the next value in the sequence. When the generator is asked for another value, the function resumes executing on the line after where it left off. When the function returns, then there are no more values in the generator.

26 G ENERATORS >>> def triangle():... lst, cur = [], 0... while True:... yield lst + [cur]... lst, cur = lst + [cur], cur >>> for lst in triangle():... print(lst)... [0] [0, 1] [0, 1, 2]

27 A NOTHER E XAMPLE : G ENERATORS Suppose I wanted to make a new generator based on another generator. Lets write generator_map, which takes a function and a generator and produces the generator which yields the results of applying the function to each item yielded by the input generator. def ints(): cur = 1 while True: yield cur cur += 1 for item in generator_map(lambda x: 2 * x, ints()): print(item)

28 A NOTHER E XAMPLE : G ENERATORS Suppose I wanted to make a new generator based on another generator. Lets write generator_map, which takes a function and a generator and produces the generator which yields the results of applying the function to each item yielded by the input generator. def generator_map(fn, gen): for item in gen: yield fn(item) Go through each item in the original generator. Yield each result of applying fn onto an item from the original generator.

29 P RACTICE : G ENERATORS Write the generator generator_filter, which takes a predicate, pred, and a generator, gen, and creates a generator which yields the items yielded by gen for which pred returns True. >>> odd = lambda x: x % 2 == 1 >>> for item in generator_filter(odd, ints()):... print(item)

30 P RACTICE : G ENERATORS Write the generator generator_filter, which takes a predicate, pred, and a generator, gen, and creates a generator which yields the items yielded by gen for which pred returns True. def generator_filter(pred, gen): for item in gen: if pred(item): yield item For each item in the original generator... yield the item if pred(item) is True.

31 C ONCLUSION In lazy evaluation, expressions are not evaluated until they are needed. Python has built-in support for iterators, or objects that give back a new value each time you ask. Streams allow us to represent infinite sequences using delayed evaluation. Streams are pairs whose first element is the first element of the stream, and whose second element stores how the rest of the stream can be calculated. – This way, only as much of the stream is created as is needed. Generators are a convenient and elegant way to define new iterators using yield statements. Preview: Declarative Programming