Add logical complement

This commit is contained in:
Angelos Chalaris
2020-05-13 11:28:33 +03:00
parent b71ef81a4a
commit 30b8623259

19
snippets/complement.md Normal file
View File

@ -0,0 +1,19 @@
---
title: complement
tags: function,logic,beginner
---
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`.
```js
const complement = fn => (...args) => !fn(...args);
```
```js
const isEven = num => num % 2 === 0;
const isOdd = complement(isEven);
isOdd(2); // false
isOdd(3); // true
```