diff --git a/advanced-iterators.html b/advanced-iterators.html index b7149e4..b7280ab 100644 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -154,21 +154,33 @@ AssertionError

Generator expressions

-

FIXME +

A generator expression is like a generator function without the function.

 >>> unique_characters = {'E', 'D', 'M', 'O', 'N', 'S', 'R', 'Y'}
->>> gen = (ord(c) for c in unique_characters)
->>> gen
+>>> gen = (ord(c) for c in unique_characters)  
+>>> gen                                        
 <generator object <genexpr> at 0x00BADC10>
->>> next(gen)
+>>> next(gen)                                  
 69
 >>> next(gen)
 68
->>> tuple(ord(c) for c in unique_characters)
+>>> tuple(ord(c) for c in unique_characters)   
 (69, 68, 77, 79, 78, 83, 82, 89)
+
    +
  1. A generator expression is like an anonymous function that yields values. The expression itself looks like a list comprehension [FIXME have we introduced this yet?], but it’s wrapped in parentheses instead of square brackets. +
  2. The generator expression returns… an iterator. +
  3. Calling next(gen) returns the next value from the iterator. +
  4. If you like, you can iterate through all the possible values and return a tuple, list, or set, by passing the generator expression to tuple(), list(), or set(). +
-

FIXME +

Here’s another way to accomplish the same thing, using a generator function: + +

def ord_map(a_string):
+    for c in a_string:
+        yield ord(c)
+
+gen = ord_map(unique_characters)

Calculating Permutations… The Lazy Way!