Skip to content

Instantly share code, notes, and snippets.

@aleksandar-b
Last active January 18, 2020 10:43
Show Gist options
  • Save aleksandar-b/153ff057012099571b1d2dc3094329d6 to your computer and use it in GitHub Desktop.
Save aleksandar-b/153ff057012099571b1d2dc3094329d6 to your computer and use it in GitHub Desktop.
const filter = ([head, ...rest] = [], callback) => {
if(!head) return [];
return [
...callback(head) ? [head] : [],
...filter(rest, callback)
];
};
console.log(filter([], () => true).length === 0);
console.log(filter([1, 2, 3], (val) => val === 1).length === 1);
console.log(filter([1, 2, 3], (val) => val > 1).length === 2);
console.log(filter([1, 2, 3], (val) => val > 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment