On Mon, Aug 1, 2011 at 4:41 PM, Baishampayan Ghose <b.gh...@gmail.com> wrote: > On Mon, Aug 1, 2011 at 4:38 PM, Dhananjay Nene <dhananjay.n...@gmail.com> > wrote: >> I also find map much more atomic and portable construct to think in - >> after all every list comprehension is syntactic sugar around map + >> filter, and map/reduce/filter are far more omnipresent than list >> comprehensions. > > The above will especially make sense to someone who programs in > multiple programming languages in his day job. > > Regards, > BG > Actually there was one more aspect I didn't touch upon - why atomic functions are helpful. So its easier to construct bigger building smaller building blocks. And one doesn't need to work in different languages - its doable in python as well.
Functions compose. I stole a python compose function from http://en.wikipedia.org/wiki/Function_composition_%28computer_science%29#First-class_composition (though I reversed the order of functions to help it make a little easier on the eyes) If we take the earlier functions double, increment and then want to also sum up the results, here's how it looks in python using function compositon (note the [::-1] - thats the only change I made) from functools import partial def compose(*funcs, **kfuncs): return reduce(lambda f, g: lambda *args, **kaargs: f(g(*args, **kaargs)), funcs[::-1]) def double(x) : return x * 2 def increment(x) : return x + 1 print compose(partial(map,double), partial(map,increment), partial(reduce,lambda x,y : x + y))(range(5)) Notice the sequential application of the double, increment and then a sum operator The same again in scala (since it is easier in syntax - it will help relate what the function above is doing println(0 to 4 map {_ * 2} map {_ + 1} reduce {_ + _}) (the _ refer to the arguments passed to the function - point free programming). Dhananjay _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers