Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

1–10 of 202 posts

Re: Stop writing lambda expressions in Python

#3
So in Python, imperative style is often more idiomatic than code that uses lambda expressions.

While in some functional languages (Haskell) and array programming languages (J), tacit or point-free style is often more idiomatic than lambda expressions (or equivalent).

There's something funny about that.

Re: Stop writing lambda expressions in Python

#4
all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language.

the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical:

  let test1 = fun x -> x * x

  let test2 x = x * x
both return:

  x:int -> int
are they really different in python or is it just a weird naming problem? the article isn't explicit on the actual differences other than what is reported by the REPL.

also, you can't pass operators as functions in python? in f#, you simply wrap them in parentheses.

  let test3 f x y = f x y

  > test3 (*) 2 3 ;;

  val it : int = 6
i read an interview with hal abelson where he called python's lambda broken. seems so.

edit: also, this guy's thoughts on map and filter, in my opinion, show the python community's backwoods thinking regarding functional programming. yes, it seems generator expressions are nice (they are called sequence expressions in f#), but map and filter, even for his simple examples are much more clear. they communicate better what is actually happening. the fact that he never uses map and filter at all and then goes on to basically say map and filter aren't even needed in python says a lot.

Re: Stop writing lambda expressions in Python

#5
> When I encounter a lambda expression in the wild, I often find that removing it improves code readability.

I have never seen a code that wasn't made clearer when moving from a three-liner non-lambda somewhere upper in the code, to a one-liner lambda used at the right place

Re: Stop writing lambda expressions in Python

#7
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

Amen, brother. I'm not proficient in Python, but from my (admittedly, limited) experience with it, it feels like programming with a straightjacket on.

Re: Stop writing lambda expressions in Python

#8
The first two situations where he recommends avoiding lambda I would agree with. But when he assigns `colors_by_length`, that's a perfectly valid usage of lambda. It's actually an ideal example of why it exists. His `length_and_alphabetical` is not an improvement. The tuple unpacking is something that destructuring in lambda syntax would improve, as it did with JavaScript. In ES6 it would be:

    const points = [[[1, 2], 'red'], [[3, 4], 'green']]
    const points_by_color = points
      .sort(([point, color]) => color)
I know it's kind of futile to argue against a point using what-ifs, but personally I wish Python embraced this style of functional programming a little bit more and adapted its syntax to it, rather than coming across articles where people recommend against this style just because the language isn't (currently) as suitable to it.

Re: Stop writing lambda expressions in Python

#9
post #3

So in Python, imperative style is often more idiomatic than code that uses lambda expressions. While in some functional languages (Haskell) and array programming languages (J), tacit or point-free style is often more idiomatic than lambda expressions (or equivalent). There's something funny about that.

Many of the suggested alternatives to lambdas in the article are non-imperative though... they just don't use 'lambda'.

Re: Stop writing lambda expressions in Python

#10
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

In F#, can you ask a function object what its original name was? If so, what is the original name of "test1" and "test2" in your example?

That's what python is doing and why the two are different.

(I don't think a language should track that sort of thing, but that's a different story).

Post reply on HN