[Python-ideas] Ruby-style Blocks in Python Idea
denis.spir at free.fr
Mon Mar 9 21:22:25 CET 2009
Le Mon, 9 Mar 2009 09:43:03 -0700,
average <dreamingforward at gmail.com> s'exprima ainsi:
> While there are several complaining that all this can be done with a
> def, there's an critical distinction being overlooked. Passing around
> code blocks is a very different style of programming that Python or
> most languages like it have ever experimented with.
This style of programming is very common in stack-based languages, or rather in concatenative languages in general.
:square dup * # def square(x): return x*x
:squares [square] map # def squares(l): return [square(x) for x in l]
# using equivalent of first class func
:squares [dup *] map # using anonymous func def
[1 2 3] squares ==> [1 4 9]
In the latter form, the func literal expression -- often called 'quotation' because it 'quotes' code without executing it -- can be whatever and as long as needed, which is easy due to the linear style of stack-based programming. "Higher order" functions like map, called combinators, are very frequent and (unlike in other paradigms) make the code clearer. But they are not really higher order functions, rather they take several kinds of data as input, one of which happens to be code that will be 'unquoted', i.e. run -- closer to Lisp's eval.
see http://www.latrobe.edu.au/philosophy/phimvt/joy/faq.html for a nice introduction (but rather distinctive to FP) esp section #10
la vita e estrany
More information about the Python-ideas