2 ms·
const logicGates = { nand(a, b) { return !(a && b); }, not(a) { return this.nand(a, a); }, and(a, b)
by shawndumas 11y ago
const logicGates = {
nand(a, b) {
return !(a && b);
},
not(a) {
return this.nand(a, a);
},
and(a, b) {
return this.not(this.nand(a, b));
},
or(a, b) {
return this.nand(this.not(a), this.not(b));
},
nor(a, b) {
return this.not(this.or(a, b));
},
xor(a, b) {
return this.and(this.nand(a, b), this.or(a, b));
},
xnor(a, b) {
return this.not(this.xor(a, b));
}
};
Object.keys(logicGates)
.map(gate => {
return [
{ gate, a: false, b: false },
{ gate, a: false, b: true },
{ gate, a: true, b: false },
{ gate, a: true, b: true },
];
})
.forEach(x => {
x.map(y => {
const desc = JSON.stringify(y).replace(/(gate)|:|"|\{|\}|,/g, ' ') + '=>';
console.log(desc, (({ gate, a, b }) => logicGates[gate](a, b))(y));
});
});
http://blog.shawndumas.com/post/119694815258/logic-gates-es6-version http://blog.shawndumas.com/post/119694815258/logic-gates-es6...
- abecedarius 11y agoThe NAND To Tetris book requests people not post spoilers for their problem sets. (I violated this myself in putting my code on github, but with a big spoiler warning at the top, at least. I put it up there because my Python HDL might be useful to some others who didn't want to use their language.) It's nice to start seeing ES6 code in the wild, though.