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'
[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.
-re.sub function performs regular expression-based string substitutions.
+[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.
+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.
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) ③
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.
-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.
-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.
+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.
+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.
+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]
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.
-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().
+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)
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.
+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.