diff --git a/Exercises/1-pipe.js b/Exercises/1-pipe.js index d09882a..0107e2c 100644 --- a/Exercises/1-pipe.js +++ b/Exercises/1-pipe.js @@ -1,5 +1,10 @@ 'use strict'; +const pipe = (...fns) => { + if (!fns.reduce((g, f) => typeof f === 'function' && g, true)) { + throw new Error('Not a function'); + } -const pipe = (...fns) => x => null; + return g => fns.reduce((g, f) => f(g), g); +}; module.exports = { pipe }; diff --git a/Exercises/2-compose.js b/Exercises/2-compose.js index 368e521..d52eace 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -1,5 +1,28 @@ 'use strict'; -const compose = (...fns) => x => null; +const compose = (...fns) => { + const events = { }; + const emit = (event, ...args) => { + events[event](args); + }; + const f = x => { + let res = x; + try { + for (const f of fns.reverse()) { + res = f(res); + } + } catch (e) { + emit('error', e); + return undefined; + } + return res; + }; + + const on = (event, callback) => { + events[event] = callback; + }; + Object.assign(f, { on }); + return f; +}; module.exports = { compose };
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: