💾not(f)

JSvaluefunctionhigher-order ⟩ example ⟩ not(f)

logical negation of a function, that is:

not(f)(...args) === !f(...args)
// ⭐ logical negation of f
// - that is: not(f)(x) returns !f(x)
// - we can't write (!f)(x) directly, but now we can write not(f)(x).
function not(f) {
    return function(...args) {
        const result = f.apply(this, args);  // call f(...args) with `this` context
        return !result;                      // negate the result
    };
}
  • example

const isEven = x => x % 2 === 0;
const isOdd = not(isEven);

[1, 1, 3, 5, 5].every(isOdd)    // true

Last updated