Ramda currying: how to apply argument to multiple parameters

自闭症网瘾萝莉.ら 提交于 2019-12-01 07:19:07

问题


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?


回答1:


Another option is

chain(createList, assoc('list'))

which you can see in action on the Ramda REPL.

Update

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 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

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)

and hence

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



回答2:


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


来源:https://stackoverflow.com/questions/40197523/ramda-currying-how-to-apply-argument-to-multiple-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!