Created
February 25, 2022 22:43
-
-
Save fronterior/0f04852e6b98619f0a135bbbd7d15445 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 p1Cache = f => { | |
const store = new Map; | |
console.log(store); | |
return a => store.has(a) ? store.get(a) : store.set(a, f(a)).get(a); | |
}; | |
const p2Cache = f => { | |
const store = new Map; | |
return (a, b) => { | |
console.log(a, b, store); | |
return store.has(a) | |
? store.get(a).has(b) | |
? store.get(a).get(b) | |
: store.get(a).set(b, f(a, b)).get(b) | |
: store.set(a, new Map).get(a).set(b, f(a, b)).get(b); | |
}; | |
}; | |
const p3Cache = f => { | |
const store = new Map; | |
return (a, b, c) => { | |
console.log(store); | |
return store.has(a) | |
? store.get(a).has(b) | |
? store.get(a).get(b).has(c) | |
? store.get(a).get(b).get(c) | |
: store.get(a).get(b).set(c, f(a, b, c)).get(c) | |
: store.get(b).set(b, new Map).get(b).set(c, f(a,b,c)) | |
: store.set(a, new Map).get(a).set(b, new Map).get(b).get(c, f(a, b, c)); | |
}; | |
}; | |
const pNCache = f => { | |
const store = new Map; | |
return (...p) => { | |
const len = p.length; | |
console.log(store) | |
const cp = [...p]; | |
let chn = store.has(len) ? store.get(len) : store.set(len, new Map).get(len); | |
let prop = cp.shift(); | |
do { | |
chn = chn.has(prop) ? chn.get(prop) : chn.set(prop, cp.length ? new Map : f(...p)).get(prop); | |
} while (prop = cp.shift()); | |
return chn; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment