I have a bunch of functions that combines each other to return new objects. Some functions return sets composed of return values of these other functions. I want to get the same object reference out of these functions for same arguments, so the set doesn't contain duplicates.
type A = { a: number, b: number };
type A2 = { c: A, d: A };
function makeA(a: number, b: number): A {
return {
a, b
};
}
function makeA2(c: A, d: A): A2 {
return { c, d };
}
console.log(makeA(2, 3) === makeA(2, 3)); // I want this to be true
let res = [
makeA2(makeA(1,1), makeA(1,2)),
makeA2(makeA(1,1), makeA(1,2)),
makeA2(makeA(1,1), makeA(1,2)),
makeA2(makeA(1,1), makeA(1,2))
];
console.log(new Set(res)); // so I get a single element in this list
I've tried building a utility function called DB that keeps a map the two arguments that the functions take to the return value of the function, and on second call return from the cached value, but I am still getting duplicates, I think I am missing to wrap some functions, or there is a fundamental flaw in my logic.