I am working with a variety of functions that interact with each other to generate new objects. Some functions produce sets composed of the return values of these other functions. My goal is to ensure that I retrieve the same object reference from these functions when provided with identical arguments, in order to prevent duplicates within the set.
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)); // Desired outcome is 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)); // Expected result is a single element in this list
I've attempted to create a utility function named DB which maintains a map linking the two arguments taken by the functions to the corresponding return value, and then retrieves the cached value on subsequent calls. However, I am still encountering duplicate entries. It's possible that I may need to wrap certain functions or that there is a fundamental flaw in my approach.