Ramda 柯里化:如何将参数应用于多个参数 [英] Ramda currying: how to apply argument to multiple parameters

查看:38
本文介绍了Ramda 柯里化:如何将参数应用于多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要这样做的情况:

I have a situation where I need to do this:

const f = (obj) => assoc('list', createList(obj), obj)

由于我需要第二个和第三个参数的参数,因此禁止我执行以下操作:

Due to the fact that I need the argument for the second and the third parameter, prohibits me from doing something like:

const f = assoc('list', somehowGetObj())

我也试过这个,但没用:

I also tried this, but that didn't work:

const f = assoc('list', createList(__))
const f = converge(assoc, [createList, identity])

有没有通过柯里化来做到这一点的正确方法?

Is there a proper way to do this by currying?

推荐答案

另一个选项是

chain(createList, assoc('list'))

您可以在 Ramda REPL.

which you can see in action on the Ramda REPL.

更新

为了进一步解释这是如何工作的,我将使用适用于下一个 Ramda 版本的变体:

For further explanation of how this works, I'll use the variation which will work with the next release of Ramda:

chain(assoc('list'), createList)

显示它如何匹配当前签名:

to show how it matches the current signature:

chain :: Chain m => (a -> m b) -> m a -> m b

Ramda 将函数视为 FantasyLand Monads,因此也称为 .因此,为了将上述专门用于函数,我们有

Ramda treats functions as FantasyLand Monads, and therefore thus also as Chains. So to specialize the above to functions, we have

chain :: (a -> Function x b) -> Function x a -> Function x -> b

但是 Function x y 可以更简单地写成 x ->y,所以上面可以更简单地写成

but Function x y can be written more simply as x -> y, so the above can written more simply as

chain :: (a -> x -> b) -> (x -> a) -> (x -> b)

然后你可以使用这些(专门的)类型:

Then you can use these (specialized) types:

createList :: OriginalData -> YourList                              (x -> a)
assoc :: String -> YourList -> OriginalData -> EnhancedData
assoc('list') :: YourList -> OriginalData -> EnhancedData           (a -> x -> b)

因此

chain(assoc('list'), createList) :: OriginalData -> EnhancedData    (x -> b)

这篇关于Ramda 柯里化:如何将参数应用于多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆