[英]Function composition using Ramda
I have a 2 functions and 1 variable which when combined take the form 我有2个函数和1个变量,当合并时采用以下形式
const value = f(g(x))(x)
That is, f(g(x))
returns a function taking x
again. 也就是说, f(g(x))
再次返回一个取x
的函数。 I don't like this redundancy and it's preventing me from declaring my function pointfree. 我不喜欢这种冗余,这使我无法声明我的函数是无点的。
What Ramda function do I need to transform this into R.something(f, g)(x)? 我需要什么Ramda函数将其转换为R.something(f,g)(x)?
Here's a working example, testable in http://ramdajs.com/repl/?v=0.24.1 , 这是一个工作示例,可以在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)
You can use R.chain
您可以使用R.chain
const something = chain(conc, triple)
You can see this in action on the Ramda repl . 您可以在Ramda repl上看到这一点。
you can make a function result which does 你可以做一个函数结果
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.