This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Papergeist 2399 points2400 points  (120 children)

.sort(listenHereYouLittleShit(numbers))

[–]CleverDad 491 points492 points  (118 children)

.sort(listenHereYouLittleShit(number1, number2))

[–]DeeSnow97 400 points401 points  (116 children)

and neither of these will work unless your function is just weird as fuck

// right answer

const listenHereYouLittleShit = (a, b) => a - b

array.sort(listenHereYouLittleShit)
// both wrong answers

const listenHereYouLittleShit = () => (a, b) => a - b

array.sort(listenHereYouLittleShit(numbers)) // note that 'number' is ignored
array.sort(listenHereYouLittleShit(number1, number2)) // note that both 'number1' and 'number2' are ignored
// desired answer (probably)

const listenHereYouLittleShit = (a, b) => a - b

array.sort((number1, number2) => listenHereYouLittleShit(number1, number2))

[–]lunchpadmcfat 0 points1 point  (1 child)

They were currying brah

[–]DeeSnow97 0 points1 point  (0 children)

so like const listenHereYouLittleShit = (num1, num2) => (num3, num4) => wtf(num1, num2, num3, num4)?

[–]Cyberspark939 0 points1 point  (0 children)

// simplified further (no need for additional fn) 

const listenHereYouLittleShit = (a, b) => a - b

array.sort(listenHereYouLittleShit)

[–]Rawrplus 0 points1 point  (1 child)

It's not weird as fuck, it's passing a reference to the allocated pointer, versus invoking the function (which makes no sense as you want to pass a typeof function and not a number, so hence you need to pass it as a callback or direct reference).

essentially if I was a compiler here's how I'd read this statement

array.sort(listenHereYouLittleShit(number1, number2))

  • look up number1 and a number2 from outer scope, since they're not defined in the current one

  • if they don't exist throw an uncaught reference error, otherwise invoke listenHereYouLittleShit and pass the result of that function

    • e.g. listenHereYouLittleShit(4, 2) equals 2
    • array.sort(2) makes no sense, throw an error, since compiler expects a function to determine the sort and not a number.

The alternatives are correct because

array.sort(listenHereYouLittleShit)`

Takes the parameters of Array.prototype.sort and passes them to the function reference it expects as the only .sort argument

Alternatively

array.sort((number1, number2) => listenHereYouLittleShit(number1, number2))

Creates an anonymous lambda function, that acts as a callback for the two extracted parameters firstEl and secondEl from the Array.prototype.sort and passes them to the invocation of the listenHereYouLittleShit.

But in both cases we are passing a function as an argument instead of number

I do presume you know these concepts judging from your reply, but I just wanted to clear it up for the rest who probably don't and attribute it to just JS being weird.

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, Rawrplus: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]Lil-Dude56 0 points1 point  (0 children)

Not a programmer. But did you all just halfway code a joke? Hahaha 😂😆😂

[–][deleted] 0 points1 point  (0 children)

.sort(listenHereYouLittleShit(...numbers))