Inverting Boolean functions

Posted on

We can invert Boolean values with an exclamation mark. That does not work for functions. We can’t invert functions in the shorthand syntax of Array.prototype.filter() or Array.prototype.map().

We can wrap those in a helper function to have them return the opposite of what they would return normally.

const numbers = [0, 1, 2, 3, 4, 5]
const isEven = n => n % 2 === 0
 
// the long and short form of this do the same
numbers.filter(number => isEven(number))  // ⇒ [0, 2, 4]
numbers.filter(isEven)                    // ⇒ [0, 2, 4]
 
// `!` can flip the Boolean value, but it only works with the long form
numbers.filter(number => !isEven(number))  // ⇒ [1, 3, 5]
numbers.filter(!isEven)                    // TypeError (not a function)
 
// this (curried) helper makes functions return a flipped result
const not = callback => value => !callback(value)
 
// we can use `not` like this, in both the long and short form
numbers.filter(number => not(isEven)(number))  // ⇒ [1, 3, 5]
numbers.filter(not(isEven))                    // ⇒ [1, 3, 5]
const numbers = [0, 1, 2, 3, 4, 5]
const isEven = n => n % 2 === 0
 
// the long and short form of this do the same
numbers.filter(number => isEven(number))  // ⇒ [0, 2, 4]
numbers.filter(isEven)                    // ⇒ [0, 2, 4]
 
// `!` can flip the Boolean value, but it only works with the long form
numbers.filter(number => !isEven(number))  // ⇒ [1, 3, 5]
numbers.filter(!isEven)                    // TypeError (not a function)
 
// this (curried) helper makes functions return a flipped result
const not = callback => value => !callback(value)
 
// we can use `not` like this, in both the long and short form
numbers.filter(number => not(isEven)(number))  // ⇒ [1, 3, 5]
numbers.filter(not(isEven))                    // ⇒ [1, 3, 5]
Debug
none
Grid overlay