ramdajs-如何组合管道和功能以更新对象

发布于 2025-02-11 07:42:49 字数 532 浏览 2 评论 0原文

我有一个接受两个参数的函数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2025-02-18 07:42:49

我认为Ramda没有真正内置的东西。我们可以在几个ramda函数上写下它:

const myfunction = (xyz) => async (param) => {
   return "some value " + xyz + param .a
}

const foo = (param) => 
  myfunction ('xyz') (param) .then (flip (assoc ('b')) (param))


foo ({a: 'a value'}) .then (console .log)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script> const {flip, assoc} = R </script>

但这并不清楚这比这样的香草JS版本

const foo = (param) => 
  myfunction ('xyz') (param) .then (v => ({...param, b: v}))

更好 这是其中任何一个有趣的抽象,以创建我们自己的组合器,该组合可以处理(f,g)=&gt的总体流程; (x)=&gt; f(x)。然后(g(x))。因为它感觉相对接近管道,并且因为有一个 a>在鸟类之后的命名组合者,我称此piper,缩短了“ Sandpiper”。抽象出来,我们可以简化foo看起来像这样:

const piper = (f, g) => (p) => f (p) .then (g (p))

const myfunction = (xyz) => async (param) => {
   return "some value " + xyz + param .a
}

const foo = piper (myfunction ('xyz'), flip (assoc ('b')))

foo ({a: 'a value'}) .then (console .log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script> const {flip, assoc} = R </script>

这可能是一种非常强大的技术,可以将我们功能的逻辑骨架抽象出来,以在多个位置使用。当然,它只能为这样的单个函数添加任何内容,但它使我们可以找到具有未来代码的共同点。

I don't think there's anything really built into Ramda for this. We can write it atop several Ramda functions, like this:

const myfunction = (xyz) => async (param) => {
   return "some value " + xyz + param .a
}

const foo = (param) => 
  myfunction ('xyz') (param) .then (flip (assoc ('b')) (param))


foo ({a: 'a value'}) .then (console .log)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script> const {flip, assoc} = R </script>

But it's not at all clear that this is in any way better than a vanilla JS version such as this:

const foo = (param) => 
  myfunction ('xyz') (param) .then (v => ({...param, b: v}))

(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 to pipeing and because there is a strong tradition of naming combinators after birds, I will call this piper, short for "sandpiper". Abstracting out that, we can simplify foo to look like this:

const piper = (f, g) => (p) => f (p) .then (g (p))

const myfunction = (xyz) => async (param) => {
   return "some value " + xyz + param .a
}

const foo = piper (myfunction ('xyz'), flip (assoc ('b')))

foo ({a: 'a value'}) .then (console .log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
<script> const {flip, assoc} = R </script>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文