Currying and Partially Applied Functions

A 'curry' function is one that returns a function that can be invoked at a later time. The returned function relies upon some information that was provided to the curry function. The returned function is often referred to as a 'partially applied' because it has some of the information it needs (provided by the curry function) but might still need more information (via parameters) when it is actually invoked. John Resig described it like this: Filling in the first couple arguments of a function (and returning a new function) is typically called currying.

 

This example demonstrates a curried function at it's most basic level:

function displayText(el){
return function(text){
el.innerHTML = text;
}
}
// returns a function that inserts text into a specific element,
// the element is provided by the curried function

 

Here's how you might use the partially applied function:

var displayInInfoDiv = displayText(document.getElementById("infoDiv"));

document.getElementById("helpBtn").oclick = function(){
displayInfoDiv("Help....");
};

document.getElementById("contactBtn").oclick = function(){
displayInfoDiv("Contact...");
};

 

Hopefully this demonstrates how you might use currying as a convenience, and to reduce duplicate code.

 

Add a Comment...

 

Comments

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil": captcha solution of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? There are a lot of introducing videos about XEvil in YouTube. - 7/22/2017

Revolutional update of SEO/SMM package "XRumer 16.0 + XEvil 3.0": captchas regignizing of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another size-types of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of introducing videos about XEvil in YouT - 7/22/2017

best mattress in 2017 best memory foam mattresses 2017 the best mattress of 2017 best mattresses in 2017 best mattresses in 2017 the best mattress of 2017 best rated mattresses 2017 best rated mattresses 2017 mattresses 2017 best memory foam mattress 2017 best mattresses in 2017 top rated mattress 2017 best memory foam mattress 2017 the best mattresses 2017 best mattress of 2017 for side sleeper casper coupons online casper coupon code casper coupons online promo code leesa leesa mat - 7/22/2017

Absolutely NEW update of SEO/SMM software "XRumer 16.0 + XEvil 3.0": captchas breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another size-types of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? You can find a lot of impessive videos about XEvil in Yo - 7/21/2017

Revolutional update of SEO/SMM software "XRumer 16.0 + XEvil": captcha breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another categories of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? You can find a lot of introducing videos about XEvil in YouTube. - 7/21/2017

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil": captchas recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another categories of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? You can find a lot of impessive videos about XEvil in YouT - 7/18/2017

Revolutional update of SEO/SMM software "XRumer 16.0 + XEvil": captchas breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another types of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? There are a lot of introducing videos about XEvil in YouTube. Go - 7/17/2017

Revolutional update of SEO/SMM package "XRumer 16.0 + XEvil 3.0": captcha solution of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another types of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? You can find a lot of demo videos about XEvil in YouTube. See - 7/14/2017