【问题标题】:Function composition using Ramda使用 Ramda 的函数组合
【发布时间】:2018-03-13 21:24:21
【问题描述】:

我有 2 个函数和 1 个变量,它们组合起来的形式是

const value = f(g(x))(x)

也就是说,f(g(x)) 再次返回一个采用x 的函数。我不喜欢这种冗余,它阻止我声明我的函数 pointfree。

我需要什么 Ramda 函数将其转换为 R.something(f, g)(x)?

这是一个工作示例,可在 http://ramdajs.com/repl/?v=0.24.1 中测试,

const triple = x => x * 3
const conc = x => y => x + " & " + y

const x = 10

conc(triple(x))(x)

// I'm looking for R.something(conc, triple)(x)

【问题讨论】:

    标签: javascript functional-programming ramda.js pointfree


    【解决方案1】:

    您可以使用R.chain

    const something = chain(conc, triple)
    

    您可以在 Ramda repl 上看到这一点。

    【讨论】:

    • 对于那些摸不着头脑为什么chain 在这种特定情况下工作的人,您需要阅读有关 Function monad - 很好的答案,Scott
    • @naomik:还有来自 scott-christoper 的 excellent answer 清楚地解释了这一点。
    【解决方案2】:

    你可以做一个函数结果

    const triple = x => x * 3
    const conc = x => y => x + " & " + y
    
    let result = (a, b) => e => a(b(e))(e)
    
    const x = 10
    
    console.log(conc(triple(x))(x));
    
    // I'm looking for R.something(conc, triple)(x)
    
    console.log(result(conc, triple)(x));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2017-04-14
      • 2018-12-15
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2017-05-10
      相关资源
      最近更新 更多