-
-
Save nbgoodall/1f51b259e6a01a5809df739087489655 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NOT = x => | |
typeof x === 'function' | |
? (...args) => NOT(x(...args)) | |
: ~x + 2 | |
const AND = (a, b) => a & b | |
const OR = (a, b) => a | b | |
const XOR = (a, b) => a ^ b | |
const NAND = NOT(AND) | |
const NOR = NOT(OR) | |
const XNOR = NOT(XOR) | |
const logicGate = (gate, ...args) => ( | |
{ | |
AND, | |
OR, | |
NOT, | |
NAND, | |
NOR, | |
XOR, | |
XNOR | |
}[gate](...args) | |
) | |
logicGate('AND', 1, 1) | |
// => 1 | |
logicGate('NOT', 1) | |
// => 0 | |
logicGate('NAND', 1, 0) | |
// => 1 | |
logicGate('NOR', 0, 0) | |
// => 1 | |
const nth_negative = n => x => new Array(n).fill(0).reduce(fn => NOT(fn), x) | |
const not_666 = nth_negative(666) | |
not_666(0) | |
// => 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment