Skip to content

Composition tasks #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"linebreak-style": [
"error",
"unix"
"windows"
],
"quotes": [
"error",
Expand Down Expand Up @@ -262,4 +262,4 @@
"never"
]
}
}
}
9 changes: 8 additions & 1 deletion Exercises/1-pipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'use strict';

const pipe = (...fns) => x => null;
const pipe = (...fns) => {
fns.forEach(fn => {
if (typeof fn !== 'function') throw Error('not a Function');
});

return x => fns.reduce((acc, fn) => acc = fn(acc), x);
};


module.exports = { pipe };
28 changes: 27 additions & 1 deletion Exercises/2-compose.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
'use strict';

const compose = (...fns) => x => null;
const compose = (...fns) => {
const errors = [];

const fn = x => {
if (fns.length === 0) return x;

const start = fns.length - 1;
let res = x;

try {
for (let i = start; i >= 0; i--) {
res = fns[i](res);
}
return res;
} catch (err) {
errors.forEach(e => e(err));
}
};

fn.on = (name, callback) => {
if (name === 'error') {
errors.push(callback);
}
};

return fn;
};

module.exports = { compose };
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy