Skip to content

Instantly share code, notes, and snippets.

@supasympa
Last active February 9, 2022 11:01
Show Gist options
  • Save supasympa/abe83a635c998ed296198f601bf1ac2e to your computer and use it in GitHub Desktop.
Save supasympa/abe83a635c998ed296198f601bf1ac2e to your computer and use it in GitHub Desktop.
useful functional programming snippets (in Typescript)
/*
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