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
addInner
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.
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.
ReplyDeleteWeb 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
ReplyDeleteWe’ll ensure that just about all information is aligned directly to your needs and app companies provide the appropriate assistance.
ReplyDeleteHi I was searching for the blogs for many times, now I have reached at the right place.
ReplyDeletebrand firm San Francisco
This text may be value everyone’s attention. How will I learn more?
ReplyDeletelogo designing services
I'd like to take the power of thanking you for that specialized guidance I've constantly enjoyed viewing your blog.
ReplyDeletetop web designing company
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.
ReplyDeleteThis is excellent blog here. A great read. Thank you for sharing this fascinating info
ReplyDeleteweb design development company