shfshanyue / Daily-Question

互联网大厂内推及大厂面经整理,并且每天一道面试题推送。每天五分钟,半年大厂中
https://q.shanyue.tech
4.92k stars 508 forks source link

【Q657】实现一个 composeLeft/flow(从左向右) 函数,进行函数合成 #675

Open shfshanyue opened 3 years ago

shfshanyue commented 3 years ago

实现一个 composeLeft/flow(从左向右) 函数,进行函数合成,类似于 lodash.flow

const add10 = x => x + 10
const mul10 = x => x * 10
const add100 = x => x + 100

// (10 + 10) * 10 + 100 = 300
flow(add10, mul10, add100)(10)

相关问题: 【Q181】如何实现 compose 函数,进行函数合成

ovarte commented 3 years ago

// 我这个好理解,不够优雅

function compose(fn){
    let args = [].slice.call(arguments)
    return function(){
        let sum = 0
        let params = [].slice.call(arguments)[0]
        for(let i = 0; i< args.length; i++){
            let f = args[i]
            sum += f(params)
        }
        return sum;
    }
}
shfshanyue commented 3 years ago

@ovarte 使用 markdown 语法做代码高亮吧,这个 Issue 可以重复编辑,多余的 Issue 可以删掉。另外,这个问题和以前的重复了,我改为了从左到右执行了

shfshanyue commented 3 years ago
const flow = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)))
heretic-G commented 3 years ago
function compose (...funArr) {
    return function (...args) {
        let result = args
        for (let i = 0;i < funArr.length;i++) {
            if (typeof funArr[i] === 'function') {
                result = [funArr[i](...result)]
            }
        }
        return result.length === 1 ? result[0] : result
    }
}
haotie1990 commented 3 years ago
/**
 * 创建一个函数。 返回的结果是调用提供函数的结果,
 * this 会绑定到创建函数。 每一个连续调用,
 * 传入的参数都是前一个函数返回的结果
 * @param {Function|Function[]} funcs 要调用的函数
 */
function flow(funcs) {
  if (!Array.isArray(funcs)) {
    funcs = [funcs];
  }
  const context = this;
  return function() {
    let args = [].slice.call(arguments, 0);
    return funcs.reduce((acc, func) => {
      acc = Array.isArray(acc) ? acc : [acc];
      return func.apply(context, acc);
    }, args);
  }
}

const add10 = x => x + 10
const mul10 = x => x * 10
const add100 = x => x + 100

console.log(flow([add10, mul10, add100])(10));
hwb2017 commented 2 years ago
const lodashFlow = (...fns) => (...args) => fns.reduce((a, b) => b(+a), args.map(a => +a))

const add10 = x => x + 10
const mul10 = x => x * 10
const add100 = x => x + 100
console.log(lodashFlow(add10, mul10, add100)(10))