Skip to content

Instantly share code, notes, and snippets.

@Swizec
Last active December 23, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swizec/6593317 to your computer and use it in GitHub Desktop.
Save Swizec/6593317 to your computer and use it in GitHub Desktop.
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