this post was submitted on 08 Nov 2022
5 points (100.0% liked)

Programmer Humor

32070 readers
819 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

If an element is out of order, remove it from the array.

An example in everyone's (least) favorite language:

const americanSort = (list, compare) => list.reduce((acc, cur) => { const lastElement = acc.length > 0 ? acc[acc.length - 1] : null; return lastElement ? compare(lastElement, cur) < 1 ? [...acc, cur] : acc : [cur]; }, []);

console.log(americanSort([5, 2, 6, 5, 20, 10, 40], (a, b) => a - b)) outputs Array(4) [ 5, 6, 20, 40 ]

top 4 comments
sorted by: hot top controversial new old
[–] kevincox@lemmy.ml 4 points 2 years ago* (last edited 2 years ago) (2 children)

Off-topic: Why is const americanSort = (list, compare) => { ... } such a common pattern these days? Isn't it simpler to just do function americanSort(list, compare) { ... }

[–] CannotSleep420@lemmygrad.ml 1 points 2 years ago

It makes me feel cool and I'm already used to doing it, so doing it the function way becomes slower from being so used to doing it the other way.

[–] Ephera@lemmy.ml 1 points 2 years ago

Yeah, I really don't get that one. I code in multiple languages where one could use anonymous functions like that, but in all of those languages, it's universally agreed upon that you basically always use named functions, unless you're directly passing it as a parameter.

My best guess for this being a common pattern in JS is that people have less strong feelings about code readability and are often untrained, so may just develop these weird habits without outside feedback...

[–] muad_dibber@lemmygrad.ml 3 points 2 years ago

I feel like the americanSort would be to remove all the numbers and replace them with the same number repeating forever.

americanSort([55, 331, 26, 89]) -> [1, 1, 1, 1, 1, 1, 1, ...]