diff --git a/Exercises/1-pipe.js b/Exercises/1-pipe.js index d09882a..37ee6af 100644 --- a/Exercises/1-pipe.js +++ b/Exercises/1-pipe.js @@ -1,5 +1,10 @@ 'use strict'; -const pipe = (...fns) => x => null; +const pipe = (...fns) => { + fns.forEach(x => { + if (typeof x !== 'function') throw new Error('arg must be a function'); + }); + return num => fns.reduce((val, fn) => fn(val), num); +}; module.exports = { pipe }; diff --git a/Exercises/2-compose.js b/Exercises/2-compose.js index 368e521..313baf8 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -1,5 +1,21 @@ 'use strict'; -const compose = (...fns) => x => null; +const compose = (...fns) => { + const events = {}; + const fn = num => fns.reverse().reduce((val, f) => { + try { + if (typeof val === 'undefined') return undefined; + return f(val); + } catch (e) { + const err = events['error']; + if (err) err(e); + return undefined; + } + }, num); + fn.on = (event, callback) => { + events[event] = callback; + }; + return fn; +}; module.exports = { compose };