Composition

We discussed “composition” in the context of types—creating new types by combining other types. Now let’s talk about function composition, combining functions by connecting the output of the first to the input of the second.

For example, here are two functions. The first takes an apple as input and outputs a banana. The second takes a banana as input and outputs some cherries. The output of the first is the same type as the input of the second, and therefore we can compose them together like this:

images/fn_compose_before.png

After we have composed them together, we have a new function:

An important aspect of this kind of composition is information hiding. You ...

Get Domain Modeling Made Functional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.