discussion 10
play

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.


  1. Discussion 10: Iterators, Generators and Streams Nancy Shaw (nshaw99@berkeley.edu) Caroline Lemieux (clemieux@berkeley.edu) April 18th, 2019

  2. Iterators and Generators

  3. Iterators vs. Iterables s = “am a string” i = iter(s)

  4. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s)

  5. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter()

  6. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i “am a string”

  7. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i next(s) “am a string”

  8. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i next(s) ERROR “am a string”

  9. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i next(s) ERROR “am a string” next(i)

  10. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i next(s) ERROR “am a string” next(i) “a”

  11. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i next(s) ERROR “am a string” next(i) “a” next(i)

  12. Iterators vs. Iterables s = “am a string” str is an iterable i = iter(s) get its iterator with iter() i next(s) ERROR “am a string” next(i) “a” next(i) “m”

  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

  14. Do 1.1

  15. Pausing vs. stopping a video Kind of like yield Kind of like return

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

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

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

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

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

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

  22. Concept check: yield vs return def generate_up_to(n): for i in range (0, n): return i >>> generate_up_to(5)

  23. Concept check: yield vs return def generate_up_to(n): for i in range (0, n): return i >>> generate_up_to(5) 0

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

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

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

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

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

  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)

  30. Do 1.1 (the other 1.1)

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

  32. Streams (back to scheme)

  33. An infinite natural number generator in Python (demo)

  34. An infinite natural number generator… in scheme? (demo)

  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

  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)

  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)

  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

  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)

  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 once, and saves the value) a. Promise is “forced” if we’ve computed its value

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend