liveBook · Manning

Chapter 12: Functional iteration

What you will learn in chapter 12 of Grokking Simplicity

The chapter introduces map(), filter(), and reduce() as useful alternatives to for loops over arrays.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces map(), filter(), and reduce() as useful alternatives to for loops over arrays.

Key terms: map(), filter(), reduce(), for loops, arrays

12.1 One code smell and two refactorings

The section reviews a code smell and two refactorings for turning hard-coded values and behavior into first-class arguments.

Key terms: implicit argument in function name, code smell, express implicit argument, explicit argument, first-class, replace body with callback, before, body, and after, higher-order functions, first-class function

12.2 MegaMart is creating a communications team

MegaMart’s recurring customer-email requests motivate a brief introduction to map() as a way to transform an array.

Key terms: subsets of customers, for loops, customer communications team, map(), forEach(), higher-order function, new array

12.3 Deriving map() from examples

The section derives map() by extracting a shared array-iteration pattern and making the code that generates each new element a callback.

Key terms: map(), replace body with callback, before, body, and after, callback

12.4 Functional tool: map()

This section explains how map() applies a function to each array element and how the function passed to it affects whether the map() call is a calculation or an action.

Key terms: map(), array of X, array of Y, calculation, action, inline function, anonymous function, customer

12.5 Three ways to pass a function

This section explains three ways to define and pass functions in JavaScript: globally, locally, or inline.

Key terms: globally defined, locally defined, inline function, anonymous function, local scope

12.6 Example: Email addresses of all customers

The section applies map() to customer data, warns that it preserves null results, and explains why selecting a subset calls for filter() instead.

Key terms: map(), Array of customers, null, undefined, filter(), subset, higher-order function

12.7 Deriving filter() from examples

The section derives filter() by extracting a shared array-iteration pattern and passing the element-selection test as a callback.

Key terms: filter(), test expression, callback, replace body with callback

12.8 Functional tool: filter()

This section explains how filter() uses a predicate to select elements from an array.

Key terms: filter(), predicate, Boolean

12.9 Example: Customers with zero purchases

The section applies filter() to select customers, shows how filtering can remove nulls from mapped results, and introduces reduce() for accumulating a value.

Key terms: filter(), subset, customer.id, remainder operator, map(), nulls, reduce(), accumulate, previous total, higher-order function

12.10 Deriving reduce() from examples

The section derives reduce() by identifying the common iteration in examples that repeatedly combine an accumulated value with an array element.

Key terms: reduce(), initialization, combining operation, current element

12.11 Functional tool: reduce()

This section explains how reduce() accumulates a value across an array using a function supplied by the caller.

Key terms: reduce(), accumulator, current element, functional tools

12.12 Example: Concatenating strings

Uses string concatenation to show how reduce() combines an array, then explains argument order, initial values, and boundary cases for functional tools.

Key terms: reduce(), accumulated string, current string, initial value, empty array, business rule, argument order, array first, function last, map()

12.13 Things you can do with reduce()

The section shows how reduce() can reconstruct state from a history of interactions and how it can express map() and filter(), including implementations that mutate only local values.

Key terms: reduce(), initial value, user interactions, current state, audit trails, map(), filter(), calculations, local value

12.14 Three functional tools compared

This section compares map(), filter(), and reduce() by what each does with an array.

Key terms: map(), filter(), reduce()

12.15 Conclusion

The conclusion connects map(), filter(), and reduce() to common iteration patterns and the functional-programming practice of using small, focused abstractions.

Key terms: map(), filter(), reduce(), functional tools, iteration patterns

12.16 Summary

12.17 Up next…

Previews the next chapter’s use of chained functional tools to answer more complex questions about customer data.

Where this is used later

Concepts explained in this chapter

← Chapter 11: First-class functions: Part 2 Chapter 13: Chaining functional tools →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.