Skip to content

Instantly share code, notes, and snippets.

@vsona
Last active March 26, 2018 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsona/3c31c2def3d0eed1d088f890ffe31391 to your computer and use it in GitHub Desktop.
Save vsona/3c31c2def3d0eed1d088f890ffe31391 to your computer and use it in GitHub Desktop.
js redux compose
const f1 = () => console.log('f', 1)
const f2 = () => console.log('f', 2)
const f3 = () => console.log('f', 3)
const f4 = () => console.log('f', 4)
const f5 = () => console.log('f', 5)
/**
* 合并单参数方法
*/
function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
let c = compose(f1, f2, f3, f4, f5)
c()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment