memorize function test
by Yury
HTML
<h1>Open terminal</h1>
<p>Create your own implementation of memorize function</p>
<p>
memorize should take any other function (in our case: sum and concat), with
different count of params. And return memo version of origin function
</p>
<p>
Memo function should call origin function only if it takes new list of params.
But if we already had such params, it should return already calculated result
without calling original function
</p>
JavaScript
try {
const concat = (a, b, c, d, e, f) => {
console.log('concat: ', a, b, c, d, e, f);
return `${a}${b}${c}${d}${e}${f}`;
};
const sum = (a, b) => {
console.log('sum: ', a, b);
return a + b;
};
const memorize = (f) => {
/*
const cached = [
{
args: [],
result: null
},
{
args: [],
result: null
},
]
return f = (...args) => {
if(args.length) {
}
}
*/
}; // Write your code here
const sumMemo = memorize(sum);
sumMemo(1, 2); // call
sumMemo(1, 2);
sumMemo(1, 2);
sumMemo(2, 1); // call
sumMemo(3, 4); // call
sumMemo(1, 2);
sumMemo(2, 1);
sumMemo(3, 4);
const sumConcat = memorize(concat);
sumConcat('i', 'D', 'e', 'a', 'l', 's'); // call
sumConcat('i', 'D', 'e', 'a', 'l', 's');
sumConcat('i', 'D', 'e', 'a', 'l', 's');
sumConcat('1,2', '3', '4,5', '6', 'A', 'B'); // call
sumConcat('1', '2,3', '4', '5,6', 'A', 'B'); // call
sumConcat('1,2', '3', '4,5', '6', 'A', 'B');
sumConcat('1', '2,3', '4', '5,6', 'A', 'B');
} catch (e) {
console.error(e);
}