函数组合从右到左组合:
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
函数组合从右到左有什么原因吗?
最佳答案
回答最初的问题:为什么函数组合是从右到左组合的?
- 所以传统上它是用数学制作的
comp(f)(g)(x)
与f(g(x))
的顺序相同- 创建反向或正向组合很简单(参见示例)
前向函数组合:
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/