Last active
December 23, 2015 06:19
-
-
Save Swizec/6593317 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function filter (predicate, list) { | |
return list.map(function (a) { | |
return predicate(a) ? [a] : null; | |
}).reduce(function (prev, current) { | |
return current ? prev.concat(current) : prev; | |
}, []); | |
} | |
function filter2 (predicate, list) { | |
return list.reduce(function (prev, current) { | |
return predicate(current) ? prev.concat(current) : prev; | |
}, []); | |
} | |
function quicksort (list) { | |
if (list.length == 0) { | |
return []; | |
} | |
var x = list[0], | |
xs = list.slice(1); | |
return quicksort(filter2(function (a) { return a <= x; }, xs)) | |
.concat(x) | |
.concat( | |
quicksort(filter2(function (a) { return a > x; }, xs))); | |
} | |
console.log( | |
filter(function (a) { return a < 5; }, [2,4,1,5,8,3,5]) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment