diff options
author | bnewbold <bnewbold@robocracy.org> | 2011-03-17 18:24:40 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2011-03-17 18:24:40 -0400 |
commit | 1ae60fdb963645fa2146ff8778784cc04899fec2 (patch) | |
tree | 337ff0083e0205e3b1597cb632fbed8449d64f42 /software | |
parent | 235ef4f4d230af58daf69adcd8942804794098f5 (diff) | |
download | knowledge-1ae60fdb963645fa2146ff8778784cc04899fec2.tar.gz knowledge-1ae60fdb963645fa2146ff8778784cc04899fec2.zip |
fixed some lambdas after testing
Diffstat (limited to 'software')
-rw-r--r-- | software/functional programming.page | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/software/functional programming.page b/software/functional programming.page index 7fa1358..2858a99 100644 --- a/software/functional programming.page +++ b/software/functional programming.page @@ -24,31 +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): + def how_long(x): l = 0 - while x.has_next() + 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() + def how_long_recurse(x): + if x.has_next(): x.pop() - return how-long-recurse(x) + 1 - else + return how_long_recurse(x) + 1 + else: return 0 Using the collector paradigm, we could do:: - def add1(x): return a+1; - def how-long-col(x, col): - if x.has_next() + 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 + else: x.pop() - return how-long-col(x, lambda a: col(add1(a))) + 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 |