DEV Community

Discussion on: Haskell Quicksort in JavaScript

Collapse
 
amn3s1a2018 profile image
Amn3s1a2018

It's a cool refactor example, and its an example for a common error ...
The ternary operator nice replacement for the overload, but the correct way would

surprise, surprise
x !== undefined ? etc.

'cause this way qs filters out all falshy element from the array and drops there tail too.

Collapse
 
sethcalebweeks profile image
Caleb Weeks

Good catch! Without explicitly comparing against undefined, my version would fail for arrays containing zeros. Since this is a sorting algorithm, I'll assume that an array of numbers is being passed in. I'll update to correct it!

Collapse
 
amn3s1a2018 profile image
Amn3s1a2018 • Edited

Sorry, i just replaced your original bug with a bigger one. And maybe i didn't emphasized enough, that those zeros are not just the zeros. Boolean false, empty string and even the document.all (in browser it's an array or object with type of "undefined" – what?) is falsy...

Thread Thread
 
sethcalebweeks profile image
Caleb Weeks

Yeah, that's true. But I guess what should you expect if you try to put a boolean, empty string, or an object in a number sorting algorithm... There is a reason why Array.sort coerces all items in the array to strings.