Skip to content

Instantly share code, notes, and snippets.

@Rowadz
Created October 9, 2020 17:10
Show Gist options
  • Save Rowadz/f7b7846466df656aeb71e14e2b1d7fdf to your computer and use it in GitHub Desktop.
Save Rowadz/f7b7846466df656aeb71e14e2b1d7fdf to your computer and use it in GitHub Desktop.
const func01 = (name) => `${name}-func01`
const func02 = (name) => `${name}-func02`
const func03 = (name) => `${name}-func03`
const func04 = (name) => `${name}-func04`
const func05 = (name) => `${name}-func05`
const result = func01(func02(func03(func04(func05('rowadz')))))
console.log(result)
// outputs
// rowadz-func05-func04-func03-func02-func01
const compose = (...funcs) => (param) =>
funcs.reduceRight((prevVal, currFunc) => currFunc(prevVal), param)
const composeFunc = compose(func01, func02, func03, func04, func05)
console.log(composeFunc('rowadz'))
// outputs
// rowadz-func05-func04-func03-func02-func01
// ps: an arrow function without a body has an implicit return
import { niceFunc } from 'cool-package'
const compose = (...funcs) => (...args) =>
funcs.reduce(
(prevVal, fun) =>
fun.prototype === niceFunc.prototype ? fun(prevVal) : fun(...prevVal),
args
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment