javascript - 为什么函数组合在 Javascript 中是从右到左组合的?

标签 javascript functional-programming composition associative function-composition

函数组合从右到左组合:

const comp  = f => g => x => f(g(x));
const inc = x => x + 1;
const dec = x => x - 1;
const sqr = x => x * x;
let seq = comp(dec)(comp(sqr)(inc));

seq(2); // 8

seq(2) 转化为dec(sqr(inc(2))) 应用顺序为inc(2)...sqr ...十二月。因此,函数的调用顺序与传递给 comp 的顺序相反。这对于 Javascript 程序员来说并不直观,因为他们习惯于从左到右的方法链接:

o = {
  x: 2,
  inc() { return this.x + 1, this },
  dec() { return this.x - 1, this },
  sqr() { return this.x * this.x, this }
}

o.dec().sqr().inc(); // 2

我认为这令人困惑。这是一个颠倒的组合:

const flipped = f => g => x => g(f(x));
let seql = flipped(dec)(flipped(sqr)(inc));

seql(2); // 2

函数组合从右到左有什么原因吗?

最佳答案

回答最初的问题:为什么函数组合是从右到左组合的?

  1. 所以传统上它是用数学制作的
  2. comp(f)(g)(x)f(g(x)) 的顺序相同
  3. 创建反向或正向组合很简单(参见示例)

前向函数组合:

const comp = f => g => x => f(g(x));
const flip = f => x => y => f(y)(x);
const flipped = flip(comp);

const inc = a => a + 1;
const sqr = b => b * b;

   comp(sqr)(inc)(2); // 9, since 2 is first put into inc then sqr
flipped(sqr)(inc)(2); // 5, since 2 is first put into sqr then inc

这种调用函数的方式称为柯里化(Currying),其工作方式如下:

// the original:
comp(sqr)(inc)(2); // 9

// is interpreted by JS as:
( ( ( comp(sqr) ) (inc) ) (2) ); // 9 still (yes, this actually executes!)

// it is even clearer when we separate it into discrete steps:
const compSqr = comp(sqr); // g => x => sqr(g(x))
compSqr(inc)(2);   // 9 still
const compSqrInc = compSqr(inc); // x => sqr(x + 1)
compSqrInc(2);     // 9 still
const compSqrInc2 = compSqrInc(2); // sqr(3)
compSqrInc2;       // 9 still

因此函数是从左到右组合和解释(由 JS 解释器)的,而在执行时,它们的值从右到左流过每个函数。简而言之:先由外而内,然后由内而外。

但是flip有一个限制,就是翻转后的组合不能与自身组合形成“高阶组合”:

const comp2 = comp(comp)(comp);
const flipped2 = flipped(flipped)(flipped);
const add = x => y => x + y;

   comp2(sqr)(add)(2)(3); // 25
flipped2(sqr)(add)(2)(3); // "x => f(g(x))3" which is nonsense

结论:从右到左的顺序是传统/约定俗成的,但不直观。

关于javascript - 为什么函数组合在 Javascript 中是从右到左组合的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37728493/

相关文章:

javascript - 循环遍历 getElementsByClassName

java - 抽象类作为功能接口(interface)

c# - UWP Composition - 带圆角的网格 DropShadow

javascript - 如何使用 AngularJS 将数据传递到不同的页面?

javascript - Twitter 如何立即显示我的个人资料?

javascript - 如何在另一个窗口中检查打开的 URL?

perl - 我应该使用继承还是组合?

scala - 更新元组列表

syntax - 为什么在 OCaml 的 List.map 中有一个 let ?

具有抽象类的 C++ 组合