Last active
February 9, 2022 11:01
-
-
Save supasympa/abe83a635c998ed296198f601bf1ac2e to your computer and use it in GitHub Desktop.
useful functional programming snippets (in Typescript)
This file contains hidden or 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 set of useful functional programming snippets | |
*/ | |
/** pipe | |
* @param fns Function[] - array of functions to execute | |
* | |
*/ | |
const pipe = (...fns: Function[]) => init => fns.reduce((acc, fn) => fn(acc), init); | |
/* example: | |
const addOne = (x: number) => x + 1 | |
const multiplyByTen = (x: number) => x * 10 | |
const divideByTwo = (x: number) => x / 2 | |
const result = pipe( | |
addOne, | |
multiplyByTen, | |
divideByTwo, | |
)(10); //55 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment