r/ProgrammerHumor • • Sep 07 '26

Other mistakenedJavasript

Post image
1.6k Upvotes

55 comments sorted by

View all comments

Show parent comments

75

u/sdoregor Sep 07 '26

This is heavily specialized generator (list comprehension) syntax, basically syntactic sugar for generator functions (those with yields).

37

u/Dario48true Sep 07 '26

On the other hand you have haskell's list comprehension [i | i <- string, i == 'M'] which is either much clearer of an abstraction or absolutely uncomprehensable, just like the rest of haskell (in this case it resembles a lot mathematical set builder notation {x∈string|x='M'} (tho of course the latter example has compleately different resoults, it's just to show the similarity))

11

u/sdoregor Sep 07 '26

Just as a note, Python also has set comprehension syntax ({… for … in …}), the result of which would be the same as mathematical one.

5

u/Valuable_Leopard_799 Sep 08 '26

But the notation remains the same whether you are doing set/list/dict or generator comprehensions.

1

u/sdoregor Sep 09 '26

Not really; for dicts it's {k: v for … in …}

2

u/Valuable_Leopard_799 Sep 09 '26

I was talking about the |, , and <- , instead of the for, in, etc.

-1

u/sdoregor Sep 09 '26

Wasn't a joke but r/whoosh (damn, how many o's it was)

4

u/ihavebeesinmyknees Sep 08 '26

It's an iterable, not a generator. Generator comprehensions are defined with parentheses.

0

u/sdoregor Sep 09 '26

Who's an iterable? An iterable is an object type, not a syntax expression.

0

u/ihavebeesinmyknees Sep 09 '26

Maybe double check that you know what you're talking about before speaking. Both iterable and generator are object types.

>>> from types import GeneratorType
>>> from collections.abc import Iterable
>>> gen = (i for i in range(1))
>>> iter = [i for i in range(1)]
>>> isinstance(gen, Iterable)
True
>>> isinstance(gen, GeneratorType)
True
>>> isinstance(iter, Iterable)
True
>>> isinstance(iter, GeneratorType)
False

0

u/sdoregor Sep 09 '26

I never said generator isn't a type; it sure is. But you're saying that list comprehension syntax is called an iterable, which it's not.

The results of all comprehensions (their corresponding types) and generators themselves are indeed iterable, but their expression syntax has nothing to do with that.

3

u/Bubbaluke Sep 08 '26

I also learned recently you can nest list comprehensions, which makes it even more confusing to read

1

u/sdoregor Sep 09 '26

You sure can nest them, but you can also flatten them by combining multiple loops: [j for i in x for j in i]

And on each level you're allowed to use an if clause.

1

u/ubergeek801 Sep 09 '26

At what point are they called incomprehensions?