Tuesday, June 7, 2016

What is the Difference Between Currying and Partial Application?

Currying is the act of breaking apart functions with multiple parameters into multiple one parameter functions.

Here is a simple function with multiple parameters that is not curried.
// multiple parameter function
let add x y = x + y 

Here is the same function but curried.
// curried function
let add x = 
  let addInner y = x + y 

As you can see, the curried function is made up of another function with the first parameter baked in. Because this function is curried it can now be partially applied (you actually get this automatically with any multi parameter function in F#). Partial application is a powerful technique in functional programming if used correctly. For instance, here is a simple partial application example for the function above.
// partially applied functions
let add4 = add 4 
let add5 = add 5 

add4 1 // returns 5
add5 1 // returns 6

The add4 function partially applies the add function, which means it returns the addInner function with 'x' baked in. So the add4 function becomes:
addInner y = 4 + y

This example is trivial, how could this be used in a useful way in production code? Often in our production code we have to retrieve something from the database, like a customer, and then continue with the business operation

Business logic function:
let businessLogic (dbFun:int->Customer) customerId = 
  let customer = dbFun customerId
  // other logic

This business logic function takes as a parameter a function that retrieves a customer from the database. But the database function below needs a connection string as well (passing connection string info in makes managing connection strings easier).

Database function:
let getCustomerFromDb connectionString customerId = 
  // get customer from real database

This function has the function signature of string->int->Customer. We use partial application to turn it into an int->int so it can be used in the businessLogic function.
let customerId = 5

// partially applied get customer function
let entryPoint = businessLogic (getCustomerFromDb "RealConnection") customerId 

In a non functional language it is necessary to use dependency injection to achieve this same thing. Partial application achieves the same goal but is simpler and doesn't require extra setup code.


  1. I've had the truly amazing enjoyment of working with a service nyc web design firm with lots of my own clients to solve their entire social internet marketing as well as digital strategy strategies.

  2. Web design schools offer many courses in web designing. If you want a challenging career that can prove lucrative, you should take a web design course. Because most companies are going online, they all have a need for a website. Social Media Creation, Design, Management and Optimisation

  3. We’ll ensure that just about all information is aligned directly to your needs and app companies provide the appropriate assistance.

  4. Hi I was searching for the blogs for many times, now I have reached at the right place.
    brand firm San Francisco

  5. This text may be value everyone’s attention. How will I learn more?
    logo designing services

  6. I'd like to take the power of thanking you for that specialized guidance I've constantly enjoyed viewing your blog.
    top web designing company

  7. Thanks for such a pleasant post. This post loaded with lots of useful information. Keep it up. If you are looking for the best information and suggestions related to hyip templatethen visit Web Shine Global.

  8. I will immediately snatch your RSS feed as I can’t in finding your e-mail subscription link or e-newsletter service.
    Do you’ve any? Kindly permit me to understand in order that I may subscribe. Thanks.