Ramda currying:如何将参数应用于多个参数
Ramda currying: how to apply argument to multiple parameters
我遇到了需要这样做的情况:
const f = (obj) => assoc('list', createList(obj), obj)
由于我需要第二个和第三个参数的参数,所以禁止我做类似的事情:
const f = assoc('list', somehowGetObj())
我也试过这个,但没用:
const f = assoc('list', createList(__))
const f = converge(assoc, [createList, identity])
有柯里化的正确方法吗?
const f = converge(assoc('list'), [createList, identity])
另一种选择是
chain(createList, assoc('list'))
您可以在 Ramda REPL.
上看到实际效果
更新
为了进一步解释这是如何工作的,我将使用适用于下一个 Ramda 版本的变体:
chain(assoc('list'), createList)
显示它如何匹配当前签名:
chain :: Chain m => (a -> m b) -> m a -> m b
Ramda 将函数视为 FantasyLand Monads, and therefore thus also as Chains。因此,为了将以上内容专门化为函数,我们有
chain :: (a -> Function x b) -> Function x a -> Function x -> b
但是Function x y
可以更简单地写成x -> y
,所以上面可以更简单地写成
chain :: (a -> x -> b) -> (x -> a) -> (x -> b)
然后你可以使用这些(特殊的)类型:
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)
我遇到了需要这样做的情况:
const f = (obj) => assoc('list', createList(obj), obj)
由于我需要第二个和第三个参数的参数,所以禁止我做类似的事情:
const f = assoc('list', somehowGetObj())
我也试过这个,但没用:
const f = assoc('list', createList(__))
const f = converge(assoc, [createList, identity])
有柯里化的正确方法吗?
const f = converge(assoc('list'), [createList, identity])
另一种选择是
chain(createList, assoc('list'))
您可以在 Ramda REPL.
上看到实际效果更新
为了进一步解释这是如何工作的,我将使用适用于下一个 Ramda 版本的变体:
chain(assoc('list'), createList)
显示它如何匹配当前签名:
chain :: Chain m => (a -> m b) -> m a -> m b
Ramda 将函数视为 FantasyLand Monads, and therefore thus also as Chains。因此,为了将以上内容专门化为函数,我们有
chain :: (a -> Function x b) -> Function x a -> Function x -> b
但是Function x y
可以更简单地写成x -> y
,所以上面可以更简单地写成
chain :: (a -> x -> b) -> (x -> a) -> (x -> b)
然后你可以使用这些(特殊的)类型:
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)