[英]javascript function currying, pass a function that passes the higher function parameters with currying
I'm trying to provide a function myFunction
to an object parameter named update
, but I want the function values from update
to be passed easily (or "curried") into myFunction
.我正在尝试将 function myFunction
提供给名为update
的 object 参数,但我希望将update
值轻松传递到myFunction
(或“myFunction”)。
I know I can do this我知道我能做到
const object = {update: (item1, item2) => myFunction(myValue1, myValue2, item1, item2)}
But I would prefer to do something like const object = {update: myFunction(myValue1, myValue2)}
and in myFunction
do something like const myFunction = (item1, item2) => (myValue1, myValue2) => {... do something... }
.但我更愿意做类似const object = {update: myFunction(myValue1, myValue2)}
之类的事情,并在myFunction
中做类似const myFunction = (item1, item2) => (myValue1, myValue2) => {... do something... }
.
How can I do this?我怎样才能做到这一点?
Javascript does not directly support currying like Haskell, for example, but some functional library can be helpful例如,Javascript 不直接支持像 Haskell 这样的柯里化,但是一些函数库可能会有所帮助
eg.例如。 https://ramdajs.com/docs/#curry https://ramdajs.com/docs/#curry
Say if an original function defined as myFunction(myValue1, myValue2, item1, item2) {... }
假设原始 function 定义为myFunction(myValue1, myValue2, item1, item2) {... }
then const curriedMyFn = R.Curry(myFunction)
can be use as然后const curriedMyFn = R.Curry(myFunction)
可以用作
const object = {update: curriedMyFn(myValue1, myValue2)}
Now object.update is myFunction specialized with myValue1 and myValue2.现在 object.update 是 myFunction 专用于 myValue1 和 myValue2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.