ramdajs-如何组合管道和功能以更新对象
我有一个接受两个参数的函数。
const myfunction = (xyz) => async(param) => {
return "some value " + xyz + param.a;
}
和具有对象为参数的管道,
R.pipe(
)({"a":"a value"});
如何使用返回的函数返回值向对象添加新属性?
预期输出:
{
"a": "a value",
"b": "some value xyz a value"
}
尝试以下,但参数错误
R.pipe(
R.assoc("b", myfunction("xyz")),
)({ "a" :"a value"});
R.pipe(
R.assoc("b", myfunction("xyz")(R.identity)),
)({ "a" :"a value"});
I have a function that accepts two parameters.
const myfunction = (xyz) => async(param) => {
return "some value " + xyz + param.a;
}
and a pipe with an object as parameter,
R.pipe(
)({"a":"a value"});
How to add new attribute to the object with the function returned value?
Expected output:
{
"a": "a value",
"b": "some value xyz a value"
}
Tried the below, but parameter error
R.pipe(
R.assoc("b", myfunction("xyz")),
)({ "a" :"a value"});
R.pipe(
R.assoc("b", myfunction("xyz")(R.identity)),
)({ "a" :"a value"});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为Ramda没有真正内置的东西。我们可以在几个ramda函数上写下它:
但这并不清楚这比这样的香草JS版本
:
更好 这是其中任何一个有趣的抽象,以创建我们自己的组合器,该组合可以处理
(f,g)=&gt的总体流程; (x)=> f(x)。然后(g(x))
。因为它感觉相对接近管道
,并且因为有一个 a>在鸟类之后的命名组合者,我称此piper
,缩短了“ Sandpiper”。抽象出来,我们可以简化foo
看起来像这样:这可能是一种非常强大的技术,可以将我们功能的逻辑骨架抽象出来,以在多个位置使用。当然,它只能为这样的单个函数添加任何内容,但它使我们可以找到具有未来代码的共同点。
I don't think there's anything really built into Ramda for this. We can write it atop several Ramda functions, like this:
But it's not at all clear that this is in any way better than a vanilla JS version such as this:
(and I'm a founder and big fan of Ramda; it simply doesn't try to solve all problems.)
However, there is an interesting abstraction of either of these, to create our own combinator which handles the overall flow of
(f, g) => (x) => f (x) .then (g (x))
. As it feels relatively close topipe
ing and because there is a strong tradition of naming combinators after birds, I will call thispiper
, short for "sandpiper". Abstracting out that, we can simplifyfoo
to look like this:This can be a very powerful technique, abstracting out the logical skeleton of our function to be used in multiple places. Of course it adds nothing to just a single function like this, but it lets us find commonalities with future code.