Skip to content

Commit

Permalink
feat: functions n closures tasks completed
Browse files Browse the repository at this point in the history
  • Loading branch information
art-sh committed Dec 12, 2020
1 parent 0c24f5d commit 127bd08
Showing 1 changed file with 65 additions and 16 deletions.
81 changes: 65 additions & 16 deletions src/09-functions-n-closures-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand All @@ -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;
}


Expand All @@ -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);
}


Expand All @@ -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;
};
}


Expand All @@ -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;
};
}


Expand All @@ -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;
};
}


Expand All @@ -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;
}


Expand All @@ -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;
};
}


Expand Down

0 comments on commit 127bd08

Please sign in to comment.