DEV Community

loading...
Cover image for Front-End Analogies: Closure's Coffee — Currying

Front-End Analogies: Closure's Coffee — Currying

Kevin K. Johnson
A Front-End Developer with a passion for learning how people work, the efficacy of good design, and a growing interest in the complexities of functional programming.
・2 min read

Roasting Coffee

Currying

Kona is struggling with a coffee roaster. It's so old all the instructions wore off. Every time she puts in a pound of beans: poof. Burnt to a crisp.

Finally, management buys a roaster that isn't so terrible. Probably said it was their idea, too. One pound at a time? Sure thing, boss. It'll even tell you how much is left the the load, if you're real nice.


"use strict";

// I didn't tell you this, but:
// Currying is the same as unary, accepts one parameter…

const coffeeRoaster_Multiary = (_scoop1, _scoop2, _scoop3, _scoop4, _scoop5, _scoop6, _scoop7) => {
    console.log ("_scoop1, _scoop2, _scoop3, _scoop4, _scoop5, _scoop6, _scoop7");
    return (`Roastin' time.`);
}

let coffeeRoaster_Unary = _scoop1 => {
    console.log(`6 more pounds.`);

    return coffeeRoaster_Unary = _scoop2 => {
        console.log(`5 more pounds.`);

        return coffeeRoaster_Unary = _scoop3 => {
            console.log(`4 more pounds.`);

            return coffeeRoaster_Unary = _scoop4 => {
                console.log(`3 more pounds.`);

                return coffeeRoaster_Unary = _scoop5 => {
                    console.log(`2 more pounds.`);

                    return coffeeRoaster_Unary = _scoop6 => {
                        console.log(`1 more pound.`);

                        return coffeeRoaster_Unary = _scoop7 => {
                            console.log(`All ${_scoop7} pounds loaded.`);
                            return (`Roastin' time.`);
                        }
                    }
                }
            }
        }
    }
}

coffeeRoaster_Multiary("1");
// "1, undefined, undefined, undefined, undefined, undefined, undefined"

// That one was incinerated, like Starbucks…

coffeeRoaster_Multiary("1", "2", "3", "4", "5", "6", "7");
// "1, 2, 3, 4, 5, 6, 7"

// Finally works if you're able to put all 7 pounds in at once.
// That's one big scoop.

coffeeRoaster_Unary("1")("2");
// "6 more pounds"
// "5 more pounds"

coffeeRoaster_Unary("3")("4");
// "4 more pounds"
// "3 more pounds"

coffeeRoaster_Unary("5")("6")("7");
// "2 more pounds."
// "1 more pound."
// "All 7 pounds loaded."

// With this newfangled machine, you can do it at your leisure.
// We were able to put a man on the moon,
// but this is so hard?

Discussion (0)