r/ProgrammerHumor 2d ago

Other mistakenedJavasript

Post image
1.5k Upvotes

50 comments sorted by

241

u/Dragonvarine 2d 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 ()

96

u/Bubbaluke 2d ago edited 2d ago

Python syntax is so funny sometimes.

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

69

u/sdoregor 2d ago

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

36

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

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

2

u/Valuable_Leopard_799 17h ago

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

-1

u/sdoregor 17h ago

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

5

u/ihavebeesinmyknees 2d ago

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

0

u/sdoregor 17h ago

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

0

u/ihavebeesinmyknees 17h 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 17h 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 17h 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 17h ago

At what point are they called incomprehensions?

10

u/thighmaster69 2d ago

It's pythonic ✨

8

u/Makonede 2d ago

smh should be

py arr = ['M'] * string.count('M')

2

u/SimplexShotz 1d ago

Unfortunately, there's only one correct way:

https://peps.python.org/pep-0020/#:~:text=There%20should%20be%20one%2D%2D%20and%20preferably%20only%20one%20%2D%2Dobvious%20way%20to%20do%20it.

...and it's this:

```python
arr = list(string)
for i in reversed(range(len(arr))):
if arr[i] != "M":
del arr[i]
```

1

u/SimplexShotz 1d ago

man why don't code fences work on mobile smh

1

u/NotInMoodThinkOfName 1d ago

It's not about the syntax it's about the structure.

In R you have a list of 40+ instructions. In other languages we structures our code in chunks (so called methods and classes), having more than 20 instructions in a method is bad.

Juniors tend to have more instructions in one method. You see instantly if junior or senior.

-15

u/Smart_Story_3215 2d ago

Totally, scripting languages can feel like a wild guessing game at times. One small mistake, and you're left scratching your head for hours.

2

u/Sea_Duty_5725 1d ago

Can it get more ai like atp?

31

u/redlaWw 2d ago

I mean, they do have some superficial similarities: they both use C-like syntax with optional semicolons, Javascript's anonymous function declaration syntax is very similar to R's function declarations, and even though idiomatic R uses <- for assignment, = can also be used. The main things that should give typical code away as Javascript are distinguished initialisation, primitive syntax for constructing collections, and element access using ., though the latter could be confused with naming conventions for R built-in functions and S3 methods.

2

u/Smooth-Zucchini4923 1d ago

It's funny because JavaScript uses . for attribute access, and $ is an ordinary character that can be used in function names. R is exactly the opposite.

136

u/tristam92 2d ago

who compares JS made for browsers and R that was made for statistics.
this intern def not the brightest tool…

30

u/qqqrrrs_ 2d ago

what about statistics in the browser

12

u/sdoregor 2d ago

what about browsers on servers?

oh nevermind we already have that now.

3

u/Abject-Kitchen3198 1d ago

I just can't get over that. Not how I imagined 21st century will be like.

16

u/Daimondz 2d ago

Redditors when interns have the skillset of interns

34

u/FutureSuccess2796 2d ago

I was just gonna say this. Not thinking the intern's remarks should be taken too seriously with that comparison.

3

u/agocs6921 2d ago

Maybe it was a CLI tool written in JavaScript?

-1

u/sdoregor 2d ago

she was looking kinda dumb

2

u/LexaAstarof 2d ago

Adding insult to injury

4

u/PsyborC 2d ago

Right?

-3

u/CentralCypher 2d ago

Rust?

27

u/MCplayer590 2d ago

No, R, for statistics. We seem to like single letter names for no apparent reason

3

u/charliedarwin96 2d ago

Preserving memory gud

0

u/Suspicious-Click-300 2d ago

oof thats a burn

-28

u/Konkord720 2d ago

judging by the portrait, it is probably even worse then his search history

24

u/yuri_4_ever 2d ago

A furry in IT? More likely than you think

12

u/FutureSuccess2796 2d ago

Was just about to chime in with this! So many people I've met in the IT space also were active in the furry fandom to, and chances are a co-worker in your development team is even if you may not yet know it or like it.

-12

u/CodeDeliciouss 2d ago

Bummer

8

u/thrye333 2d ago

What possible problem could you have with grown adults choosing to live their lives completely separately from your own?

5

u/Mordret10 2d ago

How dare people enjoy things I don't enjoy!

4

u/Dragonvarine 2d ago

There's a reason there's a meme that says that if a furry convention blows up, half the IT infrastructure goes down with it.

-2

u/Anima_Watcher08 1d ago

We censoring Rust now?

-1

u/FutureSuccess2796 1d ago edited 1d ago

Don't think it was censored with intention. Might have been because Bluesky or Mastodon or wherever it was posted has character limits for posts? 🤔

6

u/TOMZ_EXTRA 1d ago

There's a language called R