Created
April 22, 2022 15:54
-
-
Save vindard/fad02e2b26edcba56c7c32b917de46cd to your computer and use it in GitHub Desktop.
Test the conditional equivalence of 2 conditional statements
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
// Live run here: https://replit.com/@vindard/Conditional-Equivalence-TS | |
type fn = (a: boolean, b: boolean) => boolean | |
const test = (fn1: fn, fn2: fn) => { | |
const check = (a: boolean, b: boolean) => fn1(a, b) === fn2(a, b) | |
const cases: {[key: string]: [boolean, boolean]} = { | |
ft: [false, true], | |
ff: [false, false], | |
tt: [true, true], | |
tf: [true, false], | |
} | |
const checkRanArr: boolean[] = [] | |
const checkRanObj: {[key: string]: boolean} = {} | |
for (const label in cases) { | |
const checkRan = check(...cases[label]) | |
checkRanArr.push(checkRan) | |
checkRanObj[label] = checkRan | |
} | |
return checkRanArr.every(el => !!el) ? true : checkRanObj | |
} | |
// <-- ADD FUNCTIONS TO TEST HERE | |
const test1 = (a, b) => a === b | |
const test2 = (a, b) => !a !== b | |
const xnor1 = (a, b) => !(!a != !b) | |
const xnor2 = (a, b) => !a === !b | |
// --> | |
// <-- SET TEST PAIR HERE | |
const testPair = [xnor1, xnor2] | |
// --> | |
console.log(...testPair) | |
console.log(test(...testPair)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment