Python provides a special syntax to define mini-function on the fly, which is called lambda functions.
In [1]:
g = lambda x:x+3
print g(3)
Lambda functions take an arbitrary number of arguments as input and output the value of a single expression. There is analgous method in objective-C called blocks. Both of them borrowed the idea from Lisp lambda.
We can use lambda and and-or trick to create a selective string-collapse function as follows (same as boolean?A:B expression in C):
In [5]:
collapse = True
process_func = collapse and (lambda s: " ".join(s.split())) or (lambda s:s)
print process_func("Hello World!")
collapse = False
process_func = collapse and (lambda s: " ".join(s.split())) or (lambda s:s)
print process_func("Hello World!")