JavaScript中如何实现函数组合?

发布于 2025-01-16 20:29:05 字数 433 浏览 3 评论 0原文

以下是需要组合并为我们提供输出 30 的三个函数:

const add = (a) => a + 10;
const mul = (a) => a * 10;
const divide = (a) => a / 5; 

// How to implement `compositionFunction`?
compositionFunction(add, mul, divide)(5);
//=> 30

预期输出为 30,因为:

5 + 10 = 15
15 * 10 = 150
150 / 5 = 30

Below are three functions that need to be composed and give us the output 30:

const add = (a) => a + 10;
const mul = (a) => a * 10;
const divide = (a) => a / 5; 

// How to implement `compositionFunction`?
compositionFunction(add, mul, divide)(5);
//=> 30

Expected output is 30 because:

5 + 10 = 15
15 * 10 = 150
150 / 5 = 30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

新人笑 2025-01-23 20:29:05

像这样的东西

const add = (a) =>  a + 10 ;
const mul = (a) =>  a * 10 ;
const divide = (a) => a / 5 ; 
// How to use this function -----

const customComposeFn = (...f) => v => f.reduce((res, f) => f(res), v)

console.log(customComposeFn(add, mul, divide)(5));

Something like this

const add = (a) =>  a + 10 ;
const mul = (a) =>  a * 10 ;
const divide = (a) => a / 5 ; 
// How to use this function -----

const customComposeFn = (...f) => v => f.reduce((res, f) => f(res), v)

console.log(customComposeFn(add, mul, divide)(5));

甜味超标? 2025-01-23 20:29:05

函数组合有两种风格:

  1. 从左到右函数组合又名管道
  2. 从右到左函数组合 aka compose

这是一个仅供娱乐的递归实现:
这假设至少有两个函数要组合

const compose = (...fn) => {
  const [[f, g], x] = [fn.slice(-2), fn.slice(0, -2)];
  const h = a => f(g(a));
  return x.length ? compose(...x, h) : h;
}

const pipe = (...fn) => {
  const [f, g, ...x] = fn;
  const h = a => g(f(a));
  return x.length ? pipe(h, ...x) : h;
}

让我们尝试一下:

const foo = x => x + 'foo';
const bar = x => x + 'bar';
const baz = x => x + 'baz';

pipe(foo, bar, baz)('');
//=> 'foobarbaz'

compose(foo, bar, baz)('');
//=> 'bazbarfoo'

There are two flavours of function composition:

  1. left-to-right function composition aka pipe
  2. right-to-left function composition aka compose

Here's a recursive implementation just for fun:
This assumes that there are at least two functions to compose

const compose = (...fn) => {
  const [[f, g], x] = [fn.slice(-2), fn.slice(0, -2)];
  const h = a => f(g(a));
  return x.length ? compose(...x, h) : h;
}

const pipe = (...fn) => {
  const [f, g, ...x] = fn;
  const h = a => g(f(a));
  return x.length ? pipe(h, ...x) : h;
}

Let's try:

const foo = x => x + 'foo';
const bar = x => x + 'bar';
const baz = x => x + 'baz';

pipe(foo, bar, baz)('');
//=> 'foobarbaz'

compose(foo, bar, baz)('');
//=> 'bazbarfoo'
迷雾森÷林ヴ 2025-01-23 20:29:05
const add = (a) => a + 10;
const mul = (a) => a * 10;
const divide = (a) => a / 5;
const customComposeFn = (...fn) => {
    return function (arg) {
        if (fn.length > 0) {
            const output = fn[0](arg);
            return customComposeFn(...fn.splice(1))(output);
        } else {
            return arg;
        }
    };
};
const res = customComposeFn(add, mul, divide)(5);
console.log(`res`, res);

const add = (a) => a + 10;
const mul = (a) => a * 10;
const divide = (a) => a / 5;
const customComposeFn = (...fn) => {
    return function (arg) {
        if (fn.length > 0) {
            const output = fn[0](arg);
            return customComposeFn(...fn.splice(1))(output);
        } else {
            return arg;
        }
    };
};
const res = customComposeFn(add, mul, divide)(5);
console.log(`res`, res);

笑咖 2025-01-23 20:29:05

这就是我要做的:

function customComposeFn(...funcs) {
  return function(arg) {
    let f, res = arg;
    while (f = funcs.shift()) {
      res = f(res)
    }
    return res;
  }
}

const add = a => a + 10;
const mul = a => a * 10;
const divide = a => a / 5;
// How to use this function -----
console.log(customComposeFn(add, mul, divide)(5));

Here's what I would do:

function customComposeFn(...funcs) {
  return function(arg) {
    let f, res = arg;
    while (f = funcs.shift()) {
      res = f(res)
    }
    return res;
  }
}

const add = a => a + 10;
const mul = a => a * 10;
const divide = a => a / 5;
// How to use this function -----
console.log(customComposeFn(add, mul, divide)(5));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文