Question

I need to call a function with two parameters from many places in my code.

hash(itemToHash, algorithm) { ... }

I don't want to pass the algorithm parameter in each of the function calls.

I could create a function with one parameter that would delegate the call:

md5hash(itemToHash) {
   hash(itemToHash, 'md5')
}

Or I could use partial application to bind the algorithm parameter.

The way I would go about this would be to create a higher order function that returns the partially applied hash function and I would call this higher order function from many places in the code to get the single parameter function.

However, isn't that unnecessary complicated? I don't really see any benefit in the second approach compared to simple call delegation.

Was it helpful?

Solution

Say you have some function partial that partially applies arguments to other functions. You don't need to call this partial function everywhere in your code that want to make use of the partially applied function.

In fact you have to call it only once. To generate a partially applied function and then say, bind it to a name.

md5Hash := partialRight(hash, 'md5')

As for my understanding even

md5Hash(itemToHash) {
    hash(itemToHash, 'md5')
}

is some kind of partial application. Albeit a static one. The benefit of a partial function is the ability to create partially applied functions at runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top