I still prefer to user list comprehensions, and don't think it would be so bad to have map, filter, reduce live in the itertools module. This would stylistically match what is done with the comparison operator functions living in their own module, which you can import if you need to pass the functions around.
Common Lisp has something similar, called LOOP. Implemented as a macro, it's a within-Lisp DSL for expressing looping constructs, e.g.
(loop for i from 1 to 10 sum i) => 55
(loop for c across "bar" collect c) => (#\b #\a #\r)
It's controversial within the CL community, because the loop language looks much more like traditional languages than Lisp.
It's an influential language, for sure, but I prefer Clojure for a number of reasons.
That CL contains TAGBODY is a brilliant thing. It amazes me that a language can be at once so high-level and so low-level. I haven't had occasion to use it yet, but the fact that it's there, and that the higher-level abstractions are built in terms of it, is a thing of beauty to me. (I don't know about PROGV.)
I'd prefer to code in CL over Java/Blub, but I prefer Clojure over CL, and probably Haskell or ML over Clojure. (Lisp has better syntax, and macros are very cool, but static typing wins for large projects, in my opinion.)
That's what inner functions are for. This isn't the best example, but you get the idea:
def foo(mylist):
def n_to_the_n(n):
if n > 1:
return n**n
return 1
return [n_to_the_n(i) for i in mylist]I'm used to Lisps, where def/defun/define/defn imply internment. For example, in Clojure:
(defn foo [list]
(defn n-to-nth [n] (Math/pow n n))
(map n-to-nth list))
Would intern the symbol 'n-to-nth in the current namespace. So every time I see "def", I think of something that has that (mild, almost always innocuous) side effect. >>> def foo(mylist):
... def n_to_the_n(n):
... if n > 1:
... return n**n
... return 1
... return [n_to_the_n(i) for i in mylist]
...
>>> print foo([1,2,3,4,5])
[1, 4, 27, 256, 3125]
>>> foo
<function foo at 0x6e5f0>
>>> n_to_the_n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n_to_the_n' is not defined
>>>
Note that I only made the inner function longer than necessary for illustrative purposes :-)With regard to 'define', it doesn't work like that in Scheme. A define in a function body behaves just like a let.
Also, I like map and filter because they aren't just functions in the procedural sense but combinators: you can pass them around, using them as arguments and returning them. It's much harder to pass around a syntactic entity like a list comprehension (although I'm glad list comprehensions exist; as shorthand they are great in source code.)
All this said, I haven't used Python in 3 years, in favor of purely functional languages such as Clojure and ML, so I might be way out of date.