Skip to content

Instantly share code, notes, and snippets.

@nbgoodall
Last active November 16, 2019 14:17
Show Gist options
  • Save nbgoodall/1f51b259e6a01a5809df739087489655 to your computer and use it in GitHub Desktop.
Save nbgoodall/1f51b259e6a01a5809df739087489655 to your computer and use it in GitHub Desktop.
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