Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created September 13, 2021 21:53
Show Gist options
  • Save prof3ssorSt3v3/ef403c7dbea5d925568511644c0806d7 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/ef403c7dbea5d925568511644c0806d7 to your computer and use it in GitHub Desktop.
Code from video about Closures, Currying, and Partial Application
/**
* Currying vs Partial Application
* Closures
* A function which returns a function that
* can has access to the returned function's scope.
* function example(param){
* let a = 123;
* return function(otherParam){
* //both param and otherParam and a are available here
* }
* }
*
* Partial Application
* Uses closures.
* Returned function is partially applied to a new value,
* meaning that the value passed to the original function
* is already attached to the returned `partially applied` function
*
* Currying
* The process of taking a multiple argument function and
* breaking it up into a series of single argument partially
* applied functions.
*/
const log = console.log;
//Simple demo - the things we would want to curry
function bakeChocolateCupcakeWithVanilla(cakeType, cakeFlavor, icingType) {
return `Made a ${cakeFlavour} ${cakeType} with ${icingType} icing.`;
}
function bakeVanillaCakeWithLemon(cakeType, cakeFlavor, icingType) {
return `Made a ${cakeFlavour} ${cakeType} with ${icingType} icing.`;
}
//curry it
function bake(cakeType) {
//partially applied
return function (cakeFlavour) {
//partially applied
return function (icingType) {
return `Made a ${cakeFlavour} ${cakeType} with ${icingType} icing.`;
};
};
}
let bakeCake = bake('cake');
let bakeCupcake = bake('cupcake');
let bakeMuffin = bake('muffin');
let chocCake = bakeCake('chocolate');
let vanillaCake = bakeCake('vanilla');
let chocCupcake = bakeCupcake('chocolate');
let carrotMuffin = bakeMuffin('carrot');
// log(chocCake('strawberry'));
// log(chocCake('vanilla'));
// log(chocCupcake('chocolate'));
// log(chocCupcake('orange'));
// log(bake('cake')('chocolate')('vanilla'));
// log(bake('cupcake')('chocolate')('cherry'));
// log(bake('muffin')('carrot')('vanilla'));
//More realistic example
import fetch from 'node-fetch';
/**
* Possible resources are posts, comments, albums, photos, todos, and users
* @param {string} endpoint which resource to get from http://jsonplaceholder.typicode.com/
* @returns {function} partially applied function
*/
let jsonPlaceholder = async (endpoint) => {
let url = `http://jsonplaceholder.typicode.com/${endpoint}`;
let resp = await fetch(url);
let data = await resp.json();
return (num) => {
return data
.slice(0, num)
.map((item) => {
let label = item.name || item.title;
return `<p>${endpoint} :: ${label}</p>`;
})
.join('\n');
};
};
const sleep = (milliseconds) => {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};
async function init() {
let posts = await jsonPlaceholder('posts');
let users = await jsonPlaceholder('users');
sleep(3000).then(() => {
//after 3 seconds this runs
log(posts(2));
log(users(4));
});
}
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment