Last active
April 16, 2024 23:06
-
-
Save Rudxain/2565b4f187d6af228b22674d83174fe6 to your computer and use it in GitHub Desktop.
list all words of the form "_i_e" where "_" is a consonant and "e" is optional, using 2 paradigms
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
const divTrunc = (n: number, d: number) => Math.trunc(n / d) | |
const createArray = <T,>(length: number, filler: (i: number) => T) => | |
Array.from({ length }, (_, i) => filler(i)) | |
const CONSONANTS = 'bcdfghjklmnpqrstvwxyz' | |
/** imperative */ | |
function procedural() { | |
const out: string[] = [] | |
for (const pre of CONSONANTS) | |
for (const c of CONSONANTS) { | |
const tmp = pre + 'i' + c | |
// for simplicity, we're using dynamic allocation | |
out.push(tmp) | |
out.push(tmp + 'e') | |
} | |
return out | |
} | |
const proc = procedural() | |
console.log(proc) | |
/** declarative */ | |
const functional = () => createArray(CONSONANTS.length ** 2 * 2, i => | |
// for the sake of optimization, | |
// we precompute the length, | |
// instead of doing recursion with dynamic array allocation | |
CONSONANTS[divTrunc(i, 2 * CONSONANTS.length)] | |
+ 'i' | |
+ CONSONANTS[divTrunc(i, 2) % CONSONANTS.length] | |
+ (i % 2 ? 'e' : '') | |
) | |
const fn = functional() | |
console.log(fn) | |
const del = ' ' | |
console.assert(proc.join(del) === fn.join(del)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote this code to check if english is phonetically consistent with respect to the pronunciation of these words, lol. So far, I haven't found any inconsistency, it seems there's a pattern