I was looking to write an "all-or-nothing" function over an array. One approach is, of course, to leverage Array.prototype.every and do something like
function allOrNothing(arr, f, fInv) {
return arr.every(f) || arr.every(fInv);
}
where f and fInv are indicator functions, and fInv is an "inverse" function of f. For example, if f = (x) => x != null, then fInv = (x) => x == null.
This leads me to think: is it possible to get the function fInv from f?
>Solution :
This function, given a function f as an input, returns its boolean inverse:
let invert = (f) => (x) => !f(x)
e.g.
let invert = (f) => (x) => !f(x)
let eq_five = (x) => x == 5
let neq_five = invert(eq_five)
console.log(eq_five(5)) // true
console.log(neq_five(5)) // false