DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Front-End Analogies: Closure's Coffee β€” Partial Application
Kevin K. Johnson
Kevin K. Johnson

Posted on

Front-End Analogies: Closure's Coffee β€” Partial Application

Ordering Coffee

Partial Application

Joe has a coffee addiction. It's not in the DSM-5, so who cares? Time to make a profit.

What do we know about this bozo? His name's Joe. He always gets two cups. One is going to be coffee with milk, but we'll say it in Frenchβ€”cafΓ© au laitβ€”to charge more. The second one, he likes to mix that up. Could be anything.


"use strict";

customerName = (_personName) => {
    return `${_personName} wants `;
}

buyTwoDrinks = (_firstCoffeeName) => {
    const secondDrink = (_secondCoffeeName) => {
        return `${_firstCoffeeName} and ${_secondCoffeeName}.`;
    }

    return secondDrink;
}

let Joe = customerName('Joe');
// What kinda clown's name is this?
// Prolly doesn't know his order yet cuz he spent too much time honkin' his nose.

let JoesUsual = buyTwoDrinks('Cafe au Lait');
// Usual baristas [functions] are like,
// "I want both these drink orders [parameters] right damn now."
// This bro is chill about it. We've got the first one on-deck,
// but we know we have to wait a little bit.

// When Joe comes in over the course of the week to order,
// it might look a little like this:

console.log( Joe + JoesUsual('Espresso') );
// Joe wants Cafe au Lait and Espresso.

console.log( Joe + JoesUsual('Nitro Cold Brew') );
// Joe wants Cafe au Lait and Nitro Cold Brew.

// This is a lot easier and less error-prone than writing:

console.log( customerName('Joe') + buyTwoDrinks('Cafe au lait')('Chai latte') );
// Joe wants Cafe au Lait and Chai latte.

console.log( customerName('Joe') + buyTwoDrinks('Cafe au lait')('Flat white') );
// Joe wants Cafe au Lait and Flat white.

// Gotta save time. You ever seen Joe without his caffeine for too long?
// It's less clown college and more Dark Knight Returns.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.