diff options
author | bnewbold <bnewbold@robocracy.org> | 2011-09-21 00:42:50 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2011-09-21 00:42:50 -0400 |
commit | 226b1e9aabc4a557cd522f4488d9e03492e367f0 (patch) | |
tree | 9725f4b4fc9fe08d046f4e9e2938d8b96b3acda5 /software/functional programming.page | |
parent | 36492bceeec70927e4f20f03ce981852a3813174 (diff) | |
download | knowledge-226b1e9aabc4a557cd522f4488d9e03492e367f0.tar.gz knowledge-226b1e9aabc4a557cd522f4488d9e03492e367f0.zip |
formatting fixes
Diffstat (limited to 'software/functional programming.page')
-rw-r--r-- | software/functional programming.page | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/software/functional programming.page b/software/functional programming.page index 2858a99..8252d20 100644 --- a/software/functional programming.page +++ b/software/functional programming.page @@ -24,32 +24,32 @@ or variables in layer after layer of functions and just holding on to the outermost layer. For instance, the typical way to write a ``length`` function in python would be:: - def how_long(x): - l = 0 - while x.has_next(): - l = l+1; - x.pop() - return l + def how_long(x): + l = 0 + while x.has_next(): + l = l+1; + x.pop() + return l Using recursion, we could do:: - def how_long_recurse(x): - if x.has_next(): - x.pop() - return how_long_recurse(x) + 1 - else: - return 0 + def how_long_recurse(x): + if x.has_next(): + x.pop() + return how_long_recurse(x) + 1 + else: + return 0 Using the collector paradigm, we could do:: - def add1(x): return x+1; - def how_long_col(x, col): - """call this as how_long_col(<collection>, lambda b: b)""" - if not x.has_next(): - return col(0) - else: - x.pop() - return how_long_col(x, lambda a: col(add1(a))) + def add1(x): return x+1; + def how_long_col(x, col): + """call this as how_long_col(<collection>, lambda b: b)""" + if not x.has_next(): + return col(0) + else: + x.pop() + return how_long_col(x, lambda a: col(add1(a))) The first two ways, the plus one operation is actually executed at any given time, while with the collector implementation we're really creating a |