6 ms·
I don't know if this is a problem if you're going hardcore with your FP -- one line is one expression, and multiple expressions are probably going to be statefu
by jewbacca 14y ago
I don't know if this is a problem if you're going hardcore with your FP -- one line is one expression, and multiple expressions are probably going to be stateful, which should probably be factored out into something with a name.
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.
- samdk 14y ago...or anywhere you're using a 'let' statement. I write a lot of OCaml, and I write a lot of multi-line anonymous functions that aren't stateful. I think it's less common to do this in Haskell, but that's more because it's Haskell than because there aren't any side effects. 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).
- koenigdavidmj 14y agoOr you can do operator.methodgetter("method") and not need to know the class name.
- ville 14y agooperator.methodcaller('method') #ftfy
- jewbacca 14y agoNOTE: Ooops, it seems the 'edit' window expired on my comment. Expanding, unfortunately, in a separate comment: ---- 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