-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from hckrnews/feature/refactor-truth-table
Refactor the truth table, extend it for every logic type
- Loading branch information
Showing
24 changed files
with
191 additions
and
150 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
|
||
class AndTruthTable extends TruthTable { | ||
generateOutput() { | ||
this.output = this.inputs.map(row => row.every(Boolean)); | ||
this.result = this.inputs.map(row => [row.every(Boolean), ...row]); | ||
} | ||
} | ||
|
||
export default AndTruthTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Helper from './Helper'; | ||
import InvalidInputError from './InvalidInputError'; | ||
|
||
class TruthTable { | ||
constructor(propositions) { | ||
if (!Number.isInteger(propositions)) { | ||
throw new InvalidInputError( | ||
propositions, | ||
'propositions isnt a number' | ||
); | ||
} | ||
|
||
this.propositions = propositions; | ||
this.inputs = []; | ||
this.output = []; | ||
this.result = []; | ||
} | ||
|
||
get combinations() { | ||
return 2 ** this.propositions; | ||
} | ||
|
||
generateInputs() { | ||
const inputs = Array(this.combinations).fill(false); | ||
|
||
this.inputs = inputs.map((value, index) => this.addRow(index)); | ||
} | ||
|
||
generateOutput() {} | ||
|
||
addRow(row) { | ||
const binary = Helper.dec2bin(row, this.propositions); | ||
const inputs = Array(this.propositions).fill(false); | ||
|
||
return inputs.map((value, index) => binary.substr(index, 1) === '1'); | ||
} | ||
|
||
static create(propositions) { | ||
const table = new this(propositions); | ||
|
||
table.generateInputs(); | ||
table.generateOutput(); | ||
|
||
return table; | ||
} | ||
} | ||
|
||
export default TruthTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
|
||
class NandTruthTable extends TruthTable { | ||
generateOutput() { | ||
this.output = this.inputs.map(row => !row.every(Boolean)); | ||
this.result = this.inputs.map(row => [!row.every(Boolean), ...row]); | ||
} | ||
} | ||
|
||
export default NandTruthTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
|
||
class NorTruthTable extends TruthTable { | ||
generateOutput() { | ||
this.output = this.inputs.map(row => !row.some(Boolean)); | ||
this.result = this.inputs.map(row => [!row.some(Boolean), ...row]); | ||
} | ||
} | ||
|
||
export default NorTruthTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
|
||
class OrTruthTable extends TruthTable { | ||
generateOutput() { | ||
this.output = this.inputs.map(row => row.some(Boolean)); | ||
this.result = this.inputs.map(row => [row.some(Boolean), ...row]); | ||
} | ||
} | ||
|
||
export default OrTruthTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,17 @@ | ||
import Helper from './Helper'; | ||
import InvalidInputError from './InvalidInputError'; | ||
|
||
class TruthTable { | ||
constructor(propositions) { | ||
if (!Number.isInteger(propositions)) { | ||
throw new InvalidInputError( | ||
propositions, | ||
'propositions isnt a number' | ||
); | ||
} | ||
|
||
this.propositions = propositions; | ||
this.inputs = []; | ||
} | ||
|
||
get combinations() { | ||
return 2 ** this.propositions; | ||
} | ||
|
||
generate() { | ||
const inputs = Array(this.combinations).fill(false); | ||
|
||
this.inputs = inputs.map((value, index) => this.addRow(index)); | ||
} | ||
|
||
addRow(row) { | ||
const binary = Helper.dec2bin(row, this.propositions); | ||
const inputs = Array(this.propositions).fill(false); | ||
|
||
return inputs.map((value, index) => binary.substr(index, 1) === '1'); | ||
} | ||
|
||
get and() { | ||
return this.inputs.map(row => row.every(Boolean)); | ||
} | ||
|
||
get nand() { | ||
return this.inputs.map(row => !row.every(Boolean)); | ||
} | ||
|
||
get or() { | ||
return this.inputs.map(row => row.some(Boolean)); | ||
} | ||
|
||
get nor() { | ||
return this.inputs.map(row => !row.some(Boolean)); | ||
} | ||
|
||
get xor() { | ||
return this.inputs.map(row => Helper.totalTrueInputs(row) % 2 === 1); | ||
} | ||
|
||
get xnor() { | ||
return this.inputs.map(row => Helper.totalTrueInputs(row) % 2 === 0); | ||
} | ||
|
||
get andWithInputs() { | ||
return this.inputs.map(row => [row.every(Boolean), ...row]); | ||
} | ||
|
||
get nandWithInputs() { | ||
return this.inputs.map(row => [!row.every(Boolean), ...row]); | ||
} | ||
|
||
get orWithInputs() { | ||
return this.inputs.map(row => [row.some(Boolean), ...row]); | ||
} | ||
|
||
get norWithInputs() { | ||
return this.inputs.map(row => [!row.some(Boolean), ...row]); | ||
} | ||
|
||
get xorWithInputs() { | ||
return this.inputs.map(row => [ | ||
Helper.totalTrueInputs(row) % 2 === 1, | ||
...row, | ||
]); | ||
} | ||
|
||
get xnorWithInputs() { | ||
return this.inputs.map(row => [ | ||
Helper.totalTrueInputs(row) % 2 === 0, | ||
...row, | ||
]); | ||
} | ||
|
||
static create(propositions) { | ||
const table = new TruthTable(propositions); | ||
|
||
table.generate(); | ||
|
||
return table; | ||
} | ||
} | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
import AndTruthTable from './AndTruthTable.mjs'; | ||
import NandTruthTable from './NandTruthTable.mjs'; | ||
import OrTruthTable from './OrTruthTable.mjs'; | ||
import NorTruthTable from './NorTruthTable.mjs'; | ||
import XorTruthTable from './XorTruthTable.mjs'; | ||
import XnorTruthTable from './XnorTruthTable.mjs'; | ||
|
||
export default TruthTable; | ||
export { | ||
AndTruthTable, | ||
NandTruthTable, | ||
OrTruthTable, | ||
NorTruthTable, | ||
XorTruthTable, | ||
XnorTruthTable, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
import Helper from './Helper'; | ||
|
||
class XnorTruthTable extends TruthTable { | ||
generateOutput() { | ||
this.output = this.inputs.map( | ||
row => Helper.totalTrueInputs(row) % 2 === 0 | ||
); | ||
this.result = this.inputs.map(row => [ | ||
Helper.totalTrueInputs(row) % 2 === 0, | ||
...row, | ||
]); | ||
} | ||
} | ||
|
||
export default XnorTruthTable; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import TruthTable from './BaseTruthTable.mjs'; | ||
import Helper from './Helper'; | ||
|
||
class XorTruthTable extends TruthTable { | ||
generateOutput() { | ||
this.output = this.inputs.map( | ||
row => Helper.totalTrueInputs(row) % 2 === 1 | ||
); | ||
this.result = this.inputs.map(row => [ | ||
Helper.totalTrueInputs(row) % 2 === 1, | ||
...row, | ||
]); | ||
} | ||
} | ||
|
||
export default XorTruthTable; |
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
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
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
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
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
Oops, something went wrong.