http://www.artima.com/weblogs/viewpost.jsp?thread=147358
This is one of my (very few!) gripes with FP in Python.
http://www.artima.com/weblogs/viewpost.jsp?thread=147358
This is one of my (very few!) gripes with FP in Python.
>>> a = range(12)
>>> [a.pop(i) for i in range(3)]
[0, 2, 4]
>>> a
[1, 3, 5, 6, 7, 8, 9, 10, 11]
As long as list methods have methods that change state of the object, even when processing said object, Python handles FP at a very superficial syntactic level. >>> a = tuple(3*x for x in range(5))
>>> a[2]
6
>>> map(lambda x: str(x), a) #can be iterated
['0', '3', '6', '9', '12']
>>> a[2] = 9
TypeError: 'tuple' object does not support item assignment
>>> a.pop()
AttributeError: 'tuple' object has no attribute 'pop'In Haskell, the only places (that occur to me right now) where you'll have multiple lines are:
* Pattern matching function arguments
* Giving things names with `let` or `where`
* In a monadic `do` block, which is just sugar that reduces to single-expression chain of lambdas
And all of these are language design decisions that require more than just multi-statement lambdas.
Random other tidbit that doesn't deserve its own comment:
In Python, instance.method is the same as doing ClassName.method(instance). This can be helpful when using map/filter, because you get to do map(ClassName.method,lst) instead of map(lambda x: x.method,lst).
----
I don't know if this is a problem if you're going hardcore with your FP. When we're talking about "lines" in Python, one line is exactly one statement, and if you're doing FP, that statement is an expression. Multiple statements are going to be either stateful, or declaring a temporary name for an intermediate value.
I am probably not being sufficiently imaginative right now, but I can't think of any fully-fledged functional pattern which would require multiple statements in a lambda, which would not be better abstracted out and given a name, and I would genuinely like to hear some examples of cases where a multi-statement lambda make for "better" code (or to hear how my presentation of this problem might be flawed, because I haven't done any serious Python programming recently enough to be very close to the issue).
----
In Haskell, a language where the syntax has been exhaustively designed to facilitate FP, the only places where you'll see multiple lines (I'm pretty sure this is exhaustive, without using GHC extensions, eg. Arrow `proc` blocks) are:
* Pattern matching function arguments
* Giving things names with `let` or `where`
* In a monadic `do` block -- which is just sugar that reduces to single-expression chain of lambdas, and are not equivalent to multi-statement lambdas