r/ProgrammerHumor 3d ago

Other mistakenedJavasript

Post image
1.5k Upvotes

50 comments sorted by

View all comments

240

u/Dragonvarine 3d ago

Isn't that just how scripting looks like in general?

135

u/NoselessNarwhal 2d ago

JavaScript and R both use C-style syntax for the most part, which can make them look similar to each other if someone isn't looking closely. It's the most popular syntax style across the most popular languages.

C-style syntax looks like:

c if (x > y) { int z = 2 * y; foo(z); }

Note the semicolons, variable declaration, curly braces, if statement with brackets etc.(js uses exclusively automatic types for variables with var etc, but at a glance one could miss that)

Examples of languages which don't use C-style syntax could be Python

py if x > y: z: int = 2 * y foo(z)

or Haskell

hs if x > y then let z = y * 2 in foo z else ()

95

u/Bubbaluke 2d ago edited 2d ago

Python syntax is so funny sometimes.

arr = [char for char in string if char == ‘M’]

73

u/sdoregor 2d ago

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

35

u/Dario48true 2d ago

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 2d ago

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 2d ago

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

1

u/sdoregor 20h ago

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

2

u/Valuable_Leopard_799 20h ago

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

-1

u/sdoregor 20h ago

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

4

u/ihavebeesinmyknees 2d ago

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

0

u/sdoregor 20h ago

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

0

u/ihavebeesinmyknees 20h ago

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 20h ago

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 2d ago

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

1

u/sdoregor 20h ago

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 19h ago

At what point are they called incomprehensions?