import re
def buildMatchAndApplyFunctions((pattern, search, replace)):
matchFunction = lambda word: re.search(pattern, word)
applyFunction = lambda word: re.sub(search, replace, word)
return (matchFunction, applyFunction)
patterns = \
(
('[sxz]$', '$', 'es'),
('[^aeioudgkprt]h$', '$', 'es'),
('(qu|[^aeiou])y$', 'y$', 'ies'),
('$', '$', 's')
)
rules = map(buildMatchAndApplyFunctions, patterns)
def plural(noun):
for matchesRule, applyRule in rules:
if matchesRule(noun):
return applyRule(noun)
if __name__ == '__main__':
import sys
if sys.argv[1:]:
print plural(sys.argv[1])
else:
print __doc__a
buildMatchAndApplyFunctions is a function that builds other functions dynamically. It takes
pattern, search and replace (actually it takes a tuple, but more on that in a minute), and you can build
the match function using the lambda syntax to be a function that takes one parameter (word) and calls
re.search with the pattern that was passed to the buildMatchAndApplyFunctions function, and
the word that was passed to the match function you're building.
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
buildMatchAndApplyFunctions function, and the word that was passed to the apply 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.
Finally, the buildMatchAndApplyFunctions 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
buildMatchAndApplyFunctions.
It takes the list of strings in patterns and turns them into a list of functions. How? By
mapping the strings to the buildMatchAndApplyFunctions 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.
The plural function is completely generic; it takes a list of rule functions and calls them in
order. It doesn't care how the rules are defined. In stage 2, they were defined as seperate named functions. In
stage 3, they were defined as anonymous lambda functions. Now in stage 4, they are built dynamically by
mapping the buildMatchAndApplyFunctions function onto a list of raw strings.
Notice the double parentheses? This function doesn't actually take three parameters; it actually takes one
parameter, a tuple of three elements. But the tuple is expanded when the function is called, and the three
elements of the tuple are each assigned to different variables: pattern, search, and replace.
patterns was a list of tuples, and
each tuple had three elements. When you called map(buildMatchAndApplyFunctions, patterns), that
means that buildMatchAndApplyFunctions is not getting called with three parameters. Using map to map a
single list onto a function always calls the function with a single parameter: each element of the list. In the case of
patterns, each element of the list is a tuple, so buildMatchAndApplyFunctions always gets called with the
tuple, and you use the auto−tuple−expansion trick in the definition of buildMatchAndApplyFunctions to
assign the elements of that tuple to named variables that you can work with.
def buildMatchAndApplyFunctions((pattern, search, replace)):
matchFunction = lambda word: re.search(pattern, word)
applyFunction = lambda word: re.sub(search, replace, word)
return (matchFunction, applyFunction)
patterns = \
(
('[sxz]$', '$', 'es'),
('[^aeioudgkprt]h$', '$', 'es'),
('(qu|[^aeiou])y$', 'y$', 'ies'),
('$', '$', 's')
)
rules = map(buildMatchAndApplyFunctions, patterns)
def plural(noun):
for matchesRule, applyRule in rules:
if matchesRule(noun):
return applyRule(noun)
if __name__ == '__main__':
import sys
if sys.argv[1:]:
print plural(sys.argv[1])
else:
print __doc__a
buildMatchAndApplyFunctions is a function that builds other functions dynamically. It takes
pattern, search and replace (actually it takes a tuple, but more on that in a minute), and you can build
the match function using the lambda syntax to be a function that takes one parameter (word) and calls
re.search with the pattern that was passed to the buildMatchAndApplyFunctions function, and
the word that was passed to the match function you're building.
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
buildMatchAndApplyFunctions function, and the word that was passed to the apply 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.
Finally, the buildMatchAndApplyFunctions 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
buildMatchAndApplyFunctions.
It takes the list of strings in patterns and turns them into a list of functions. How? By
mapping the strings to the buildMatchAndApplyFunctions 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.
The plural function is completely generic; it takes a list of rule functions and calls them in
order. It doesn't care how the rules are defined. In stage 2, they were defined as seperate named functions. In
stage 3, they were defined as anonymous lambda functions. Now in stage 4, they are built dynamically by
mapping the buildMatchAndApplyFunctions function onto a list of raw strings.
Notice the double parentheses? This function doesn't actually take three parameters; it actually takes one
parameter, a tuple of three elements. But the tuple is expanded when the function is called, and the three
elements of the tuple are each assigned to different variables: pattern, search, and replace.
patterns was a list of tuples, and
each tuple had three elements. When you called map(buildMatchAndApplyFunctions, patterns), that
means that buildMatchAndApplyFunctions is not getting called with three parameters. Using map to map a
single list onto a function always calls the function with a single parameter: each element of the list. In the case of
patterns, each element of the list is a tuple, so buildMatchAndApplyFunctions always gets called with the
tuple, and you use the auto−tuple−expansion trick in the definition of buildMatchAndApplyFunctions to
assign the elements of that tuple to named variables that you can work with.
Comments
Post a Comment
https://gengwg.blogspot.com/