def foo(x):
def lambda():
print x
lambda;bar(callback=lambda);baz(42,lambda)
def lambda():
print x * x
lambda;bar(callback=lambda);baz(42,lambda)
It'd stop this silly complaint. I just end up using 'def lambduh'Slightly annoying that you need to define the function on the line before, can't inline it, but not nearly as bad as people make it out to be.
It's considered opaque.
Personally, I like to fiddle with Clojure in my free time.
Still not sure whether this is a good or bad thing.
I've both written and read code in non-functional languages where the "easy" way of doing it is thrown aside for the "cool functional" way of doing it. Leading to code which was more fun to write, looks cooler, but is also hard to read and written in a different idiom to the actual programming language. Bad.
I'm 100% guilty of this, although I try to stay vigilant. The other day I refactored out a gnarly bit of a Python API, but caught myself feeling sad because it meant deleting a bunch of clever lambda functions I was using to work around it in the first place. Bad!
That said, the other side of the double-edged sword is great - things that lead to giant tangled messes of procedural code can be turned into very nice functional-style constructs. Also, I love having list comprehensions in Python.
Map:
>>> [ord(char) for char in "Hello"]
[72, 101, 108, 108, 111]
Filter: >>> [x for x in xrange(10) if x % 2]
[1, 3, 5, 7, 9]
For laziness, use parenthesis to turn the above list comprehensions into a generator comprehension. Though I don't demonstrate it, you can work over infinite sequences of objects this way with constant memory usage (without allocating all of them upfront). >>> gen = (x for x in range(10) if x % 2)
>>> gen
<generator object <genexpr> at 0xb729f6bc>
>>> for element in gen:
... print element
...
1
3
5
7
9
Alice, the rabbit hole begins here --> http://docs.python.org/library/itertools.htmlNot against list comprehensions per se, really. They're just a convenient subset for most commonly idiomatic sequence-mangling-spells, but that's most of what they're good for.
Is there a way to link to a readability version of a document? Like http://readability.org/?url=example.com
> Date: 01 Mar 2001
Some of this advice is deprecated.