diff --git a/src/09-functions-n-closures-tasks.js b/src/09-functions-n-closures-tasks.js index 5c92e909f1..328d839d04 100644 --- a/src/09-functions-n-closures-tasks.js +++ b/src/09-functions-n-closures-tasks.js @@ -23,8 +23,8 @@ * getComposition(Math.sin, Math.asin)(x) => Math.sin(Math.asin(x)) * */ -function getComposition(/* f, g */) { - throw new Error('Not implemented'); +function getComposition(f, g) { + return (x) => f(g(x)); } @@ -44,8 +44,8 @@ function getComposition(/* f, g */) { * power05(16) => 4 * */ -function getPowerFunction(/* exponent */) { - throw new Error('Not implemented'); +function getPowerFunction(exponent) { + return (number) => number ** exponent; } @@ -62,8 +62,12 @@ function getPowerFunction(/* exponent */) { * getPolynom(8) => y = 8 * getPolynom() => null */ -function getPolynom() { - throw new Error('Not implemented'); +function getPolynom(...args) { + return (x) => args.reduce((out, number, i) => { + const pow = (args.length - 1) - i; + + return out + (number * (x ** pow)); + }, 0); } @@ -81,8 +85,15 @@ function getPolynom() { * ... * memoizer() => the same random number (next run, returns the previous cached result) */ -function memoize(/* func */) { - throw new Error('Not implemented'); +function memoize(func) { + const funcStored = func; + let result; + + return (...args) => { + if (result === undefined) result = funcStored(...args); + + return result; + }; } @@ -101,8 +112,22 @@ function memoize(/* func */) { * }, 2); * retryer() => 2 */ -function retry(/* func, attempts */) { - throw new Error('Not implemented'); +function retry(func, attempts) { + let count = attempts; + + return () => { + while (count) { + count -= 1; + + try { + return func(); + } catch (e) { + // + } + } + + return undefined; + }; } @@ -129,8 +154,16 @@ function retry(/* func, attempts */) { * cos(3.141592653589793) ends * */ -function logger(/* func, logFunc */) { - throw new Error('Not implemented'); +function logger(func, logFunc) { + return (...args) => { + const argsCollection = args.map((arg) => JSON.stringify(arg)).join(','); + + logFunc(`${func.name}(${argsCollection}) starts`); + const result = func(...args); + logFunc(`${func.name}(${argsCollection}) ends`); + + return result; + }; } @@ -147,8 +180,17 @@ function logger(/* func, logFunc */) { * partialUsingArguments(fn, 'a','b','c')('d') => 'abcd' * partialUsingArguments(fn, 'a','b','c','d')() => 'abcd' */ -function partialUsingArguments(/* fn, ...args1 */) { - throw new Error('Not implemented'); +function partialUsingArguments(fn, ...args) { + const argsStorage = [...args]; + + const out = (...argsClosure) => { + out.valueOf = () => fn(...argsStorage); + argsStorage.push(...argsClosure); + + return out; + }; + + return out; } @@ -169,8 +211,15 @@ function partialUsingArguments(/* fn, ...args1 */) { * getId4() => 7 * getId10() => 11 */ -function getIdGeneratorFunction(/* startFrom */) { - throw new Error('Not implemented'); +function getIdGeneratorFunction(startFrom) { + let id = startFrom; + + return () => { + const result = id; + id += 1; + + return result; + }; }