From 35cbc5f288e7fcdb5e65ae7924e735c0c331c9d7 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Fri, 17 Apr 2009 09:45:10 -0400 Subject: [PATCH] fixed inconsistent text references to functions, and a few other typos [h/t G.P.] --- iterators-and-generators.html | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/iterators-and-generators.html b/iterators-and-generators.html index 77a4c61..7c72251 100644 --- a/iterators-and-generators.html +++ b/iterators-and-generators.html @@ -44,8 +44,8 @@ def plural(noun): else: return noun + 's'
    -
  1. This is a regular expression, but it uses a syntax you didn’t see in Regular Expressions. The square brackets mean “match exactly one of these characters.” So [sxz] means “s, or x, or z”, but only one of them. The $ should be familiar; it matches the end of string. Combined, this regular expression is tests whether noun ends with s, x, or z. -
  2. This re.sub function performs regular expression-based string substitutions. +
  3. This is a regular expression, but it uses a syntax you didn’t see in Regular Expressions. The square brackets mean “match exactly one of these characters.” So [sxz] means “s, or x, or z”, but only one of them. The $ should be familiar; it matches the end of string. Combined, this regular expression tests whether noun ends with s, x, or z. +
  4. This re.sub() function performs regular expression-based string substitutions.

Let’s look at regular expression substitutions in more detail. @@ -177,7 +177,7 @@ def plural(noun): if match_default(noun): return apply_default(noun) -

The benefit here is that that plural function is now simplified. It takes a list of rules, defined elsewhere, and iterates through them in a generic fashion. +

The benefit here is that the plural() function is now simplified. It takes a list of rules, defined elsewhere, and iterates through them in a generic fashion.

  1. Get a match rule @@ -187,7 +187,7 @@ def plural(noun):

    The rules could be defined anywhere, in any way. The plural() function doesn’t care. -

    Now, was adding this level of abstraction worth it? Well, not yet. Let’s consider what it would take to add a new rule to the function. In the first example, it would require adding an if statement to the plural function. In this second example, it would require adding two functions, match_foo() and apply_foo(), and then updating the rules list to specify where in the order the new match and apply functions should be called relative to the other rules. +

    Now, was adding this level of abstraction worth it? Well, not yet. Let’s consider what it would take to add a new rule to the function. In the first example, it would require adding an if statement to the plural() function. In this second example, it would require adding two functions, match_foo() and apply_foo(), and then updating the rules list to specify where in the order the new match and apply functions should be called relative to the other rules.

    But this is really just a stepping stone to the next section. Let’s move on… @@ -205,9 +205,9 @@ def build_match_and_apply_functions(pattern, search, replace): return re.sub(search, replace, word) return (matches_rule, apply_rule)

      -
    1. build_match_and_apply_functions is a function that builds other functions dynamically. It takes pattern, search and replace, then defines a matches_rule() function which calls re.search() with the pattern that was passed to the build_match_and_apply_functions() function, and the word that was passed to the matches_rule() function you’re building. Whoa. -
    2. Building the apply function works the same way. The apply function is a function that takes one parameter, and calls re.sub() with the search and replace parameters that were passed to the build_match_and_apply_functions function, and the word that was passed to the apply_rule() function you’re building. This technique of using the values of outside parameters within a dynamic function is called closures. You’re essentially defining constants within the apply function you’re building: it takes one parameter (word), but it then acts on that plus two other values (search and replace) which were set when you defined the apply function. -
    3. Finally, the build_match_and_apply_functions function returns a tuple of two values: the two functions you just created. The constants you defined within those functions (pattern within matchFunction, and search and replace within applyFunction) stay with those functions, even after you return from build_match_and_apply_functions. That’s insanely cool. +
    4. build_match_and_apply_functions() is a function that builds other functions dynamically. It takes pattern, search and replace, then defines a matches_rule() function which calls re.search() with the pattern that was passed to the build_match_and_apply_functions() function, and the word that was passed to the matches_rule() function you’re building. Whoa. +
    5. Building the apply function works the same way. The apply function is a function that takes one parameter, and calls re.sub() with the search and replace parameters that were passed to the build_match_and_apply_functions() function, and the word that was passed to the apply_rule() function you’re building. This technique of using the values of outside parameters within a dynamic function is called closures. You’re essentially defining constants within the apply function you’re building: it takes one parameter (word), but it then acts on that plus two other values (search and replace) which were set when you defined the apply function. +
    6. Finally, the build_match_and_apply_functions() function returns a tuple of two values: the two functions you just created. The constants you defined within those functions (pattern within matchFunction, and search and replace within applyFunction) stay with those functions, even after you return from build_match_and_apply_functions(). That’s insanely cool.

    If this is incredibly confusing (and it should be, this is weird stuff), it may become clearer when you see how to use it. @@ -224,7 +224,7 @@ def build_match_and_apply_functions(pattern, search, replace): for (pattern, search, replace) in patterns]

    1. Our pluralization rules are now defined as a list of lists of strings (not functions). The first string in each group is the regular expression pattern that you would use in re.search() to see if this rule matches. The second and third strings in each group are the search and replace expressions you would use in re.sub() to actually apply the rule to turn a noun into its plural. -
    2. This line is magic. It takes the list of strings in patterns and turns them into a list of functions. How? By mapping the strings to the build_match_and_apply_functions function, which just happens to take three strings as parameters and return a tuple of two functions. This means that rules ends up being exactly the same as the previous example: a list of tuples, where each tuple is a pair of functions, where the first function is the match function that calls re.search(), and the second function is the apply function that calls re.sub(). +
    3. This line is magic. It takes the list of strings in patterns and turns them into a list of functions. How? By mapping the strings to the build_match_and_apply_functions() function, which just happens to take three strings as parameters and return a tuple of two functions. This means that rules ends up being exactly the same as the previous example: a list of tuples, where each tuple is a pair of functions, where the first function is the match function that calls re.search(), and the second function is the apply function that calls re.sub().

    Rounding out this version of the script is the main entry point, the plural() function. @@ -234,7 +234,7 @@ def build_match_and_apply_functions(pattern, search, replace): if matches_rule(noun): return apply_rule(noun)

      -
    1. Since the rules list is the same as the previous example (really, it is), it should come as no surprise that the plural() function hasn’t changed at all. It’s completely generic; it takes a list of rule functions and calls them in order. It doesn’t care how the rules are defined. In the previous example, they were defined as seperate named functions. Now they are built dynamically by mapping the output of the build_match_and_apply_functions() function onto a list of raw strings. It doesn’t matter; the plural function still works the same way. +
    2. Since the rules list is the same as the previous example (really, it is), it should come as no surprise that the plural() function hasn’t changed at all. It’s completely generic; it takes a list of rule functions and calls them in order. It doesn’t care how the rules are defined. In the previous example, they were defined as seperate named functions. Now they are built dynamically by mapping the output of the build_match_and_apply_functions() function onto a list of raw strings. It doesn’t matter; the plural() function still works the same way.

    A File Of Patterns