30 Seconds of Code is a brilliant collection of JavaScript snippets, digestible in ≤ 30 seconds. Anyone looking to master JavaScript should go through the entire thing.
My PR to 30secondsofcode added the reject()
function.
Here’s the code from the official entry:
reject = (pred, array) => array.filter((...args) => !pred(...args));
It works on arrays and denies whatever Array.filter
would’ve kept.
filter
example using numbers:
nums = [1, 2, 3, 4, 5];
nums.filter((x) => x < 4);
// [1, 2, 3]
Copy/pasting that predicate into reject
does the opposite:
reject((x) => x < 4, nums);
// [4, 5]
You’d reject numbers greater than 3 like so:
reject((x) => x > 3, nums);
// [1, 2, 3]
What’s the point?
filter
can do whatever reject
does, so why bother?
The whole point of composition–combining small things into bigger things– is to more easily express ideas in code.
By simply negating filter
’s predicate, we’ve invented a repeatable way to tackle the same problem from a different perspective. Isn’t that amazing?
We now have greater opportunity to reason in terms of what to do not how to do it.
In the real world, humans need to understand the code before the computer tries to. So when talking to another human we can now say “Filter for everyone whose ages are 18 or older”, or “Reject anyone under 18”.
I like the second one better…
And by making pure functions your atomic building blocks, this type of abstraction’s possible on the micro and macro level.
Pure functions buy you the most flexibility.
- Previous state is not a concern
- No side effects
- Same Inputs => Same Outputs
But whether it’s FP or Object-Oriented Programming, we all compose software. It’s the mentality that’s important. Keep on coding.
Until next time!
Take care,
Yazeed Bzadough