Lesson + Exercises

Getting functions to work together is function composition, and how FPers prefer to structure their programs. (3 min. read, then exercise)

Function Composition

Compose means to create by combining things.

A Toyota’s roughly 30,000 parts. Composing those useless parts gives you a useful method of transportation.

Same thing with software. Our functions are useless until we compose them with other functions.

Uppercase and Reverse

Here’s our user, bobo.

const bobo = {
  firstName: 'Bobo',
  lastName: 'Flakes'
};

Let’s uppercase and reverse bobo's first name. What functions must you create and compose?

Your end result should be a function upperAndReverseFirstName that takes bobo and returns the correct output.

Press + to interact
const bobo = {
firstName: 'Bobo',
lastName: 'Flakes'
};
const result = upperAndReverseFirstName(bobo);
console.log({ result });
Press + to interact
const upperAndReverseFirstName = (user) => {
// your code here
};

Upper and Reverse for All!

After that, how can you make upperAndReverseFirstName work for Bobo and his friends?

const users = [{
  firstName: 'Bobo',
  lastName: 'Flakes'
}, {
  firstName: 'Lawrence',
  lastName: 'Shilling'
}, {
  firstName: 'Anon',
  lastName: 'User'
}];

// ['OBOB', 'ECNERWAL', 'NONA']
Press + to interact
// paste your previous solution here
const upperAndReverseFirstNames = (users) => {
// new code here
}