January 2020
Intermediate to advanced
470 pages
11h 13m
English
Back in the Getting a property from an object section of Chapter 6, Producing Functions – Higher-Order Functions, we wrote a simple getField() function that could handle getting a single attribute out of an object. (See question 6.5 in that chapter for the missing companion setField() function.) Let's take a look at how we can code this. We can have a straightforward version, as follows:
const getField = attr => obj => obj[attr];
We can even go one better by applying currying so that we have a more general version:
const getField = curry((attr, obj) => obj[attr]);
We could get a deep attribute out of an object by composing a series of applications of getField() calls, but that would be rather cumbersome. Instead, let's ...