Build Up Complex Functionality by Composing Simple Functions in JavaScript

InstructorKyle Shevlin

Share this video with your friends

Send Tweet

This lesson teaches you the concept of composition, the building up of complex functionality through the combining of simpler functions. In a sense, composition is the nesting of functions, passing the result of one in as the input into the next. But rather than create an indecipherable amount of nesting, we'll create a higher order function, compose(), that takes all of the functions we want to combine, and returns us a new function to use in our app.

chihab
~ 5 years ago

Great explanation @Kyle.

When you mentioned pipe as an alternative name to compose in some libraries I thought of RxJS which is mainly used for reactive programming using observer/observable pattern but never tried to use its provided pipe function outside that context.

It turns out that pipe in rxjs is just compose. And it just happens that it composes functions that manipulate observable values ! That made my day.

import { pipe } from 'rxjs';

const double = x => x * 2;
const tripple = x => x * 3;

const composition1 = pipe(
  double,
  tripple
);

console.log(composition1(3))