From 87b1b711b94fd2a727448a01a13ab99b50ff264c Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Wed, 8 Jul 2009 17:02:06 -0400 Subject: [PATCH] expanded on 'mapping' concept a bit --- generators.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators.html b/generators.html index 0cad642..3fb0dc9 100644 --- a/generators.html +++ b/generators.html @@ -233,7 +233,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 tuple of tuples 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 sequence of strings in patterns and turns them into a sequence 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 functionally equivalent to the previous example: a list of tuples, where each inner tuple is a pair of functions. 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 sequence of strings in patterns and turns them into a sequence of functions. How? By “mapping” the strings to the build_match_and_apply_functions() function. That is, it takes each triplet of strings and calls the build_match_and_apply_functions() function with those three strings as arguments. The build_match_and_apply_functions() function returns a tuple of two functions. This means that rules ends up being functionally equivalent to the previous example: a list of tuples, where each inner tuple is a pair of functions. 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.