On Sat, Mar 5, 2022 at 4:33 AM Paul Moore email@example.com wrote:
for thing in filter(is_interesting, this_collection): ...
That seems pretty non-clunky. Is the issue here that "filter" is not sufficiently well-known? Or that you don't want to name the is_interesting function, and lambdas are "too clunky"? This feels like another case where the general dislike of lambda results in people wanting special-case syntax so they can avoid either writing a throwaway function, or using lambda.
Speaking for me -- it's not a dislike of lambda -- it's a dislike of the map / filter approach vs comprehensions in general. I guess you could say I have a dislike for lambda -- but really it's a dislike for having to create a function when an expression will do ;-) -- and lamda only supports expressions, so it will always do.
This entire conversation -- at least from the side of the skeptics -- seems to be ignoring comprehensions:
If we all thought that map and filter were perfectly adequate, comprehensions never would have been added at all. And not only were comprehensions added, but both a looping and filtering mechanism was added at the same time. Let's imagine for the moment that the filtering was not yet added -- then say the way to run a filtered loop in a comprehension would be:
[expr for thing in filter(lambda thing: expr, an_iterable)]
Would anyone really largure that there would be no point in adding the filter explicitly to get to (what we do have now):
[expr1 for thing in an_iterable if expr2]
The other relevant point is that adding an `if` to the for loop is new syntax, yes, but it mirrors the comprehension syntax very closely -- so is not nearly the cognitive load that most syntax additions are.
In fact, I imagine if newbies were to learn about comprehensions first, they'd be surprised that there was no if allowed in for loops.
Yes, this is a fairly minor improvement, but to me it's not adding syntax to "save a line" "or not have to use lambdas" -- but rather it's adding syntax to make it possible to express a particular concept in a way that is clear, obvious, and more consistent with the rest of the language (i.e. comprehensions).
I was +0, but after this discussion, now +1.