From 88e0cf17e7850d204f64768fb8e20782f296c1f5 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Fri, 25 Sep 2009 22:56:25 -0400 Subject: [PATCH] markup fiddling --- generators.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/generators.html b/generators.html index ecc1a90..504f8c6 100755 --- a/generators.html +++ b/generators.html @@ -232,7 +232,7 @@ def build_match_and_apply_functions(pattern, search, replace):
  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. There’s a slight change here, in the fallback rule. In the previous example, the match_default() function simply returned True, meaning that if none of the more specific rules matched, the code would simply add an s to the end of the given word. This example does something functionally equivalent. The final regular expression asks whether the word has an end ($ matches the end of a string). Of course, every string has an end, even an empty string, so this expression always matches. Thus, it serves the same purpose as the match_default() function that always returned True: it ensures that if no more specific rule matches, the code adds an s to the end of the given word. -
  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(). +
  4. 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 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. @@ -341,6 +341,8 @@ def plural(noun, rules_filename='plural5-rules.txt'):

A Fibonacci Generator

+ +

[download fibonacci.py]

def fib(max):
     a, b = 0, 1          
@@ -353,8 +355,6 @@ def plural(noun, rules_filename='plural5-rules.txt'):
 
  • b is the next number in the sequence, so assign that to a, but also calculate the next value (a + b) and assign that to b for later use. Note that this happens in parallel; if a is 3 and b is 5, then a, b = b, a + b will set a to 5 (the previous value of b) and b to 8 (the sum of the previous values of a and b). - -

    So you have a function that spits out successive Fibonacci numbers. Sure, you could do that with recursion, but this way is easier to read. Also, it works well with for loops.