30秒学会 JavaScript 片段 · 2018年11月6日

30秒学会 JavaScript 片段 – complement

Returns a function that is the logical complement of the given function, fn.

Use the logical not (!) operator on the result of calling fn with any supplied args.

代码片段

const complement = fn => (...args) => !fn(...args);

使用样例

const isEven = num => num % 2 === 0;
const isOdd = complement(isEven);
isOdd(2); // false
isOdd(3); // true