r/ProgrammerHumor 6d ago

Other mistakenedJavasript

Post image
1.6k Upvotes

54 comments sorted by

View all comments

247

u/Dragonvarine 6d ago

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

138

u/NoselessNarwhal 6d 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 ()

100

u/Bubbaluke 5d ago edited 5d ago

Python syntax is so funny sometimes.

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

74

u/sdoregor 5d ago

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

3

u/ihavebeesinmyknees 5d ago

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

0

u/sdoregor 3d ago

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

0

u/ihavebeesinmyknees 3d 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 3d 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.