/logic_gates.js Secret
Last active
November 16, 2019 14:17
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