【问题标题】:Why does function composition compose from right to left in Javascript?为什么函数组合在 Javascript 中是从右到左组合的?
【发布时间】:2016-06-09 14:15:22
【问题描述】:

函数组成从右到左:

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...dec。因此,函数以与传递给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

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

【问题讨论】:

  • "因此函数的求值顺序与其应用程序的相反" - 嗯,什么?
  • 您似乎在问为什么在函数调用(左)之前评估参数(右),不是吗?
  • 如果seq 写成comp(comp(dec)(sqr))(inc) 对你来说更有意义吗? (函数组合是可交换的)
  • @Bergi 我改写了有问题的句子
  • @Bergi 嗯,这不会改变事实,comp 从右到左进行评估。我没有任何问题,我只是问为什么?

标签: javascript functional-programming composition associative function-composition


【解决方案1】:

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

  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

结论:从右到左的顺序是传统/常规但不直观。

【讨论】:

    【解决方案2】:

    您的问题实际上是关于函数组合运算符定义中的参数顺序,而不是右或左关联性。在数学中,我们通常写“f o g”(相当于您定义中的 comp(f)(g))来表示接受 x 并返回 f(g(x)) 的函数。因此“f o (g o h)”和“(f o g) o h”是等价的,都表示将每个参数 x 映射到 f(g(h(x))) 的函数。

    也就是说,我们有时会写 f;g(相当于代码中的 compl(f)(g))来表示将 x 映射到 g(f(x)) 的函数。因此,(f;g);h 和 f;(g;h) 都表示将 x 映射到 h(g(f(x))) 的函数。

    参考:https://en.wikipedia.org/wiki/Function_composition#Alternative_notations

    【讨论】:

    • 好吧,我把交换律和结合律混淆了。谢谢!
    • 至于替代符号 (";"):我们还发现像 >>>|> 这样的“管道”运算符,它们可以更好地可视化“从左到右”的概念,IMO。
    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 2021-09-24
    • 1970-01-01
    相关资源
    最近更新 更多