Discussion 10: Iterators, Generators and Streams Nancy Shaw - - PowerPoint PPT Presentation

discussion 10
SMART_READER_LITE
LIVE PREVIEW

Discussion 10: Iterators, Generators and Streams Nancy Shaw - - PowerPoint PPT Presentation

Discussion 10: Iterators, Generators and Streams Nancy Shaw (nshaw99@berkeley.edu) Caroline Lemieux (clemieux@berkeley.edu) April 18th, 2019 Iterators and Generators Iterators vs. Iterables s = am a string i = iter(s) Iterators vs.


slide-1
SLIDE 1

Discussion 10:

Iterators, Generators and Streams

Nancy Shaw (nshaw99@berkeley.edu) Caroline Lemieux (clemieux@berkeley.edu)

April 18th, 2019

slide-2
SLIDE 2

Iterators and Generators

slide-3
SLIDE 3

Iterators vs. Iterables

s = “am a string” i = iter(s)

slide-4
SLIDE 4

Iterators vs. Iterables

s = “am a string” i = iter(s)

str is an iterable

slide-5
SLIDE 5

Iterators vs. Iterables

s = “am a string” i = iter(s)

str is an iterable get its iterator with iter()

slide-6
SLIDE 6

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

slide-7
SLIDE 7

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

next(s)

slide-8
SLIDE 8

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

next(s) ERROR

slide-9
SLIDE 9

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

next(s) ERROR next(i)

slide-10
SLIDE 10

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

next(s) ERROR next(i) “a”

slide-11
SLIDE 11

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

next(s) ERROR next(i) “a” next(i)

slide-12
SLIDE 12

Iterators vs. Iterables

s = “am a string” i = iter(s)

“am a string”

i

str is an iterable get its iterator with iter()

next(s) ERROR next(i) “a” “m” next(i)

slide-13
SLIDE 13

How to go through iterables without calling next

for i in iterable goes through all the things in iterable, by calling next Calling list(iterable) makes a list of all the things we get by calling next

slide-14
SLIDE 14

Do 1.1

slide-15
SLIDE 15

Pausing vs. stopping a video

Kind of like yield Kind of like return

slide-16
SLIDE 16

Generators

def generate_up_to(n): for i in range(0, n): yield i

slide-17
SLIDE 17

Generators

def generate_up_to(n): for i in range(0, n): yield i >>> generate_up_to(5) <generator object ...>

slide-18
SLIDE 18

Generators

def generate_up_to(n): for i in range(0, n): yield i >>> generate_up_to(5) <generator object ...> When python sees a yield in a function, calling that function returns a generator

slide-19
SLIDE 19

Generators

def generate_up_to(n): for i in range(0, n): yield i >>> generate_up_to(5) <generator object ...> >>> g = generate_up_to(5) >>> next(g) When python sees a yield in a function, calling that function returns a generator Calling next on the generator “plays” that function, until yield, where it “pauses”

slide-20
SLIDE 20

Generators

def generate_up_to(n): for i in range(0, n): yield i >>> generate_up_to(5) <generator object ...> >>> g = generate_up_to(5) >>> next(g) When python sees a yield in a function, calling that function returns a generator Calling next on the generator “plays” that function, until yield, where it “pauses”

slide-21
SLIDE 21

Generators

def generate_up_to(n): for i in range(0, n): yield i >>> generate_up_to(5) <generator object ...> >>> g = generate_up_to(5) >>> next(g) >>> next(g) 1 When python sees a yield in a function, calling that function returns a generator Calling next on the generator “plays” that function, until yield, where it “pauses”

slide-22
SLIDE 22

Concept check: yield vs return

def generate_up_to(n): for i in range(0, n): return i >>> generate_up_to(5)

slide-23
SLIDE 23

Concept check: yield vs return

def generate_up_to(n): for i in range(0, n): return i >>> generate_up_to(5)

slide-24
SLIDE 24

Concept check: yield vs return

def generate_up_to(n): for i in range(0, n): return i >>> generate_up_to(5) Calling a regular functions “plays” that function, until return, where it “stops”

slide-25
SLIDE 25

Concept check: yield vs return

def generate_up_to(n): for i in range(0, n): return i >>> generate_up_to(5) >>> generate_up_to(5) Calling a regular functions “plays” that function, until return, where it “stops”

slide-26
SLIDE 26

Concept check: yield vs return

def generate_up_to(n): for i in range(0, n): return i >>> generate_up_to(5) >>> generate_up_to(5) Calling a regular functions “plays” that function, until return, where it “stops”

slide-27
SLIDE 27

Concept check: yield vs return

def generate_up_to(n): for i in range(0, n): return i >>> generate_up_to(5) >>> generate_up_to(5) Calling a regular functions “plays” that function, until return, where it “stops” When we call it again, it “plays” from the start

slide-28
SLIDE 28

Yield from

def generate_up_to(n): for i in range(0, n): yield i def generate_up_to(n): yield from range(0,n) Same thing!

slide-29
SLIDE 29

Recursive generator

def generate_down_to_zero(n): if n == 0: yield 0 else: yield n yield from generate_down_to_zero(n-1)

slide-30
SLIDE 30

Do 1.1 (the other 1.1)

slide-31
SLIDE 31

Attendance

links.cs61a.org/caro-disc next(cats)

slide-32
SLIDE 32

Streams (back to scheme)

slide-33
SLIDE 33

An infinite natural number generator in Python

(demo)

slide-34
SLIDE 34

An infinite natural number generator… in scheme?

(demo)

slide-35
SLIDE 35

What’s a stream:

A “lazy” scheme list

  • Lazy because it evaluates its first element….
  • … but then is lazy and doesn’t evaluate the second
slide-36
SLIDE 36

How do I make a stream?

(cons-stream <operand1> <operand2>) Another special form! 1. Evaluate operand1 to get val1 2. Construct promise containing operand2 3. Return a pair (val1, promise of operand2)

slide-37
SLIDE 37

How do I make a stream?

(cons-stream <operand1> <operand2>) Another special form! 1. Evaluate operand1 to get val1 2. Construct promise containing operand2 3. Return a pair (val1, promise of operand2) (demo)

slide-38
SLIDE 38

How do I make a stream?

(cons-stream <operand1> <operand2>) Another special form! 1. Evaluate operand1 to get val1 2. Construct promise containing operand2 3. Return a pair (val1, promise of operand2) Need special (cdr-stream s) to get the cdr properly Important: cdr-stream evalutes its value once, then saves that for later calls

slide-39
SLIDE 39

How do I make a stream?

(cons-stream <operand1> <operand2>) Another special form! 1. Evaluate operand1 to get val1 2. Construct promise containing operand2 3. Return a pair (val1, promise of operand2) Need special (cdr-stream s) to get the cdr properly Important: cdr-stream evalutes its value once, then saves that for later calls (demo)

slide-40
SLIDE 40

Stream Recap

1. nil is the empty stream 2. cons-stream constructs a stream 3. car gets the first element of the stream 4. cdr-stream computes and returns the rest of the stream (it only computes

  • nce, and saves the value)

a. Promise is “forced” if we’ve computed its value