Last active
February 27, 2024 05:43
-
-
Save artemsites/e251dad0a3dc084639429d642bf1f5c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const compose = (...fns) => { | |
const handlers = []; | |
const composed = (x) => { | |
if (fns.length === 0) return x; | |
const last = fns.length - 1; | |
let res = x; | |
try { | |
for (let i = last; i >= 0; i--) { | |
res = fns[i](res); | |
} | |
return res; | |
} catch (error) { | |
for (const handler of handlers) { | |
handler(error); | |
} | |
return null; | |
} | |
}; | |
composed.on = (name, handler) => { | |
if (name === 'error') handlers.push(handler); | |
}; | |
return composed; | |
}; | |
module.exports = { compose }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/HowProgrammingWorks/Composition/tree/master | |
// https://www.youtube.com/watch?v=xS9FicVrOTI | |
// Multiply | |
'use strict'; | |
/** | |
* Выполнение функций слева на право | |
* На вход принимаем список функций | |
* В каждую функцию будет передаваться результат предыдущей в переменную x | |
* Далее вызываем все функции одна за другой в .reduce() | |
* x - это результат выполнения предыдущей функции | |
* v - сюда кладётся текущий "x" на момент выполнения | |
* f - текущая функция на момент перебора | |
* f(v) - непосредственное выполнение с текущим значением "v" полученным из аккумулятора значений "x" | |
*/ | |
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x); | |
/** | |
* Выполнение функций справа на лево | |
* В остальном это тоже самое что pipe (в pipe.js) | |
*/ | |
const compose = (...fns) => (x) => fns.reduceRight((v, f) => f(v), x); | |
// Loop | |
'use strict'; | |
const compose = (...fns) => (x) => { | |
const last = fns.length - 1; | |
let res = x; | |
for (let i = last; i >= 0; i--) { | |
res = fns[i](res); | |
} | |
return res; | |
}; | |
const pipe = (...fns) => (x) => { | |
let res = x; | |
for (let i = 0; i < fns.length; i++) { | |
res = fns[i](res); | |
} | |
return res; | |
}; | |
// Recursive | |
'use strict'; | |
const compose = (...fns) => (x) => { | |
if (fns.length === 0) return x; | |
const fn = fns.pop(); | |
const res = fn(x); | |
if (fns.length === 0) return res; | |
return compose(...fns)(res); | |
}; | |
const pipe = (...fns) => (x) => { | |
if (fns.length === 0) return x; | |
const fn = fns.shift(); | |
const res = fn(x); | |
if (fns.length === 0) return res; | |
return pipe(...fns)(res); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const pipe = (...fns) => { | |
for (const f of fns) { | |
if (typeof f !== 'function') { | |
throw new Error('All compose arguments should be functions'); | |
} | |
} | |
return (x) => fns.reduce((v, f) => f(v), x); | |
}; | |
module.exports = { pipe }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment