JavaScript, inside out · Chapter 22 of 51
Higher-order functions
By the end of this chapter you can pass functions into functions, get new functions back, and line small rules up into one function. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The order flow
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Anjali has offers: a student discount, a monsoon discount, a Friday kulhad offer. Meera wants to stack them on a price ("student discount first, then GST, then round to whole rupees") and says the order will change next week. Ravi wants the orders sorted into piles by rider today and by size tomorrow, and the receipt printer wants a customer's name filled in ahead of time.
With what you know so far, every rule is its own function and every combination is one more: one function for student then GST, another for GST then student, a nested call like round(gst(student(price))) that must be rewritten each time the order changes. Grouping by rider and grouping by size are two loops that differ in one line.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
makeDiscount: a function that makes discount functions
Write makeDiscount(percent). It returns a function. That function takes a price and returns the price with percent off, rounded to whole rupees with Math.round: makeDiscount(10) gives a function where 40 becomes 36 and 25 becomes 23 (22.5 rounds up).
Every call of makeDiscount makes its own function with its own percent, so a 10 percent and a 25 percent discount never mix. makeDiscount(0) leaves a price as it is.
- Task 2
groupBy: orders in piles by any rule
Write groupBy(items, keyFn). Call keyFn once for each item (exactly once: the check counts the calls) and use what it returns as the name of that item's pile. Return an object with one key per pile name, each holding an array of the items in that pile, in their original order. The items are the same objects you were given, not copies.
An empty list gives an empty object.
- Task 3 · interview question
Interview: pipe and compose, price rules in one function
Write pipe(...fns) and compose(...fns). Each takes any number of one-argument functions and returns one new function that takes a value and passes it through all of them, each getting what the one before returned.
pipe runs them left to right: pipe(f, g, h)(x) is h(g(f(x))). compose runs them right to left: compose(f, g, h)(x) is f(g(h(x))).
With no functions at all, the returned function gives back its value unchanged. The returned function can be used many times.
- Task 4
partial: fill in the first arguments now
Write partial(fn, ...preset). It returns a new function. When that function is called with more arguments, it calls fn with the preset arguments first and the new ones after them, and returns what fn returns.
With the line function (customer, item, qty) that makes Ravi: 2 x bun maska, partial(line, 'Ravi')('bun maska', 2) gives 'Ravi: 2 x bun maska'. Any number of arguments can be preset, from none to all of them.
- Task 5 · interview question
Interview: curry, one argument at a time or several
Write curry(fn). It returns a function that collects arguments: each call can pass one argument or several. As soon as it has as many as fn.length, it calls fn with all of them in order and returns fn's result. Until then each call returns a new function that collects more.
So with the three-argument line function, c('Ravi')('bun maska')(2), c('Ravi', 'bun maska')(2), c('Ravi')('bun maska', 2) and c('Ravi', 'bun maska', 2) all give 'Ravi: 2 x bun maska', and fn runs once for each of them.
Half-filled functions must be reusable and never share arguments: const forRavi = c('Ravi') can make many different orders. It works for any number of parameters.
What you will learn
- Callbacks: a general function does the work, and the one part that changes comes in as a function.
- Returning functions: a function that makes ready-to-use functions from a setting.
- pipe and compose: many one-step functions joined into one.
- Partial application and currying: filling in some arguments now and the rest later.
More in The order flow
Build "Higher-order functions" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.