简体   繁体   中英

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 .

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

How can I do this?

Javascript does not directly support currying like Haskell, for example, but some functional library can be helpful

eg. https://ramdajs.com/docs/#curry

Say if an original function defined as myFunction(myValue1, myValue2, item1, item2) {... }

then const curriedMyFn = R.Curry(myFunction) can be use as

const object = {update: curriedMyFn(myValue1, myValue2)}

Now object.update is myFunction specialized with myValue1 and myValue2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM