Skip to content

Instantly share code, notes, and snippets.

@mrosata
Last active November 19, 2016 16:33
Show Gist options
  • Select an option

  • Save mrosata/35e4d80c2b393267a4069fbf045e22a7 to your computer and use it in GitHub Desktop.

Select an option

Save mrosata/35e4d80c2b393267a4069fbf045e22a7 to your computer and use it in GitHub Desktop.
Getting Curried Away - example 1
// Our friendly work-adjacent coffee man!
function baristaJoe(flavor, creams, sugars) {
const coffee = getCoffee(flavor); // Pour the coffee
coffee.add.cream(creams); // How many creams to add
coffee.add.suger(sugars); // How many sugars to add
return coffee;
}
// If baristaJoe was a curried function, we could create more specific functions
// by "partially applying" an argument at a time.
const farmhouse = baristaJoe('farmhouse blend');
const columbian = baristaJoe('columbian');
const arrrrrggg = baristaJoe('half-caff carmel-mocha pumkin-karaoke');
const farmhouseLight = farmhouse(10);
const farmhouseBlack = farmhouse(0);
// Those 5 lines of code we wrote above to create more specific functions,
// is about the same as writing these 35 lines of code down here... power.
/* So to be clear, we don't have to write these implementations */
function farmhouse(creams, sugars) {
const coffee = getCoffee('farmhouse blend');
coffee.add.cream(creams);
coffee.add.suger(sugars);
return coffee;
}
function columbian(creams, sugars) {
const coffee = getCoffee('columbian');
coffee.add.cream(cream);
coffee.add.suger(sugar);
return coffee;
}
function arrrrrggg(creams, sugars) {
const coffee = getCoffee('half-caff carmel-mocha pumkin-karaoke');
coffee.add.cream(cream);
coffee.add.suger(sugar);
return coffee;
}
function farmhouseLight(sugars) {
const coffee = getCoffee('farmhouse blend');
coffee.add.cream(10);
coffee.add.suger(sugar);
return coffee;
}
function farmhouseBlack(sugars) {
const coffee = getCoffee('farmhouse blend');
coffee.add.cream(0);
coffee.add.suger(sugar);
return coffee;
}
// Now where we once would have written this:
placeOrder([
baristaJoe('farmhouse blend', 10, 10),
baristaJoe('farmhouse blend', 0, 4),
baristaJoe('columbian', 3, 3),
baristaJoe('columbian', 4,3),
baristaJoe('half-caff carmel-mocha pumkin-karaoke', letMeThink(), itDepends())
]);
// We now can write this:
placeOrder([
farmhouseLight(10),
farmhouseDark(4),
columbian(3, 3),
columbian(4, 3),
arrrrrggg(letMeThink(), itDepends())
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment