-
What is compose? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
` // 普通写法 // compose写法 |
Beta Was this translation helpful? Give feedback.
-
What is compose? |
Beta Was this translation helpful? Give feedback.
-
` // 普通写法 // compose写法 |
Beta Was this translation helpful? Give feedback.
`
const space = (str) => str.split(' ')
const len = (arr) => arr.length
// 普通写法
console.log(len(space('i am linsanxin'))) // 3
console.log(len(space('i am 23 year old'))) // 5
console.log(len(space('i am a author in juejin'))) // 6
// compose写法
const compose = (...fn) => value => {
return fn.reduce((value, fn) => {
return fn(value)
}, value)
}
const computedWord = compose(space, len)
console.log(computedWord('i am linsanxin')) // 3
console.log(computedWord('i am 23 year old')) // 5
console.log(computedWord('i am a author in juejin')) // 6
`