Last active
July 7, 2022 21:40
-
-
Save renesansz/dd27153af3a52ac3647c621c82855f93 to your computer and use it in GitHub Desktop.
A functional programming utility that composes functions from right to left.
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
/** | |
* A functional programming utility that composes functions from right to left. | |
* | |
* @param fns The functions to compose. Each function is expected to accept a single parameter. | |
* @returns The final function obtained by composing the given functions from right to left. | |
*/ | |
export const compose = (...fns) => | |
fns.reduceRight( | |
(prevFn, nextFn) => | |
(...args) => | |
nextFn(prevFn(...args)), | |
(value) => value | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment