Last active
February 16, 2018 21:26
-
-
Save IsTheJack/3580edc1df2bbe28e714c5d277d9daba to your computer and use it in GitHub Desktop.
Implementing NOT, AND, NAND, OR, NOR, XOR and XNOR operators in JS with Underscore.js and Babel
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
'use strict'; | |
let NOT = (arg) => !arg; | |
let AND = (...args) => _.reduce(args, (acc, next) => next? acc: next, true); | |
let NAND = _.compose(NOT, AND); | |
let OR = (...args) => _.reduce(args, (acc, next) => NOT(next)? acc: next, false); | |
let NOR = _.compose(NOT, OR); | |
let XOR = (...args) => | |
_.reduce(args, (acc, next) => OR(AND(NOT(acc), next), AND(acc, NOT(next))),false); | |
let XNOR = _.compose(NOT, XOR); | |
console.log(AND(true, false)); // => false | |
console.log(OR(true, false)); // => true | |
console.log(XOR(false, false, false, true)); // => true | |
console.log(NAND(true, false)); // => true | |
console.log(NOR(true, false)); // => false | |
console.log(XNOR(false, false, false, true)); // => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment