# Composition Everywhere

TL;DR

By "composition," we mean, "Assemble a few low-level reusable pieces into a higher-level piece." Here are some examples:

• (Classic example) Legos. Using small blocks of plastic, people can create all sorts of interesting things.
• Furniture. Using wood, metal, fabric, glass, and nails, people can create tables, chairs, desks, cabinets, etc.

Composition makes FP code easy to refactor because we can always reassemble the smaller pieces into something new or different.

But what kinds of things do we compose? In Functional Programming, we compose types (called `algebraic data types`) and functions.

## Composing Types Algebraically

Algebraic Data Types (ADTs) use Algebra to define the total number of values a given type (i.e. named Set) can have.

There are two videos worth watching in this regard. The table and visualizations that follow merely summarize their points, except for the ideas behind the `List` and `Tree` types in the second video.

NameMath OperatorLogic OperatorPureScript TypeIdea
Product Type`x * y`AND`Tuple`"One value from type `x` AND one value from type `y`"
Sum Type`x + y`OR`Either`"One value from type `x` OR one value from type `y`"
Exponential Type`y^x`???`InputType -> OutputType`???

## Composing Functions

Similar to types, functions also compose but in a slightly different way. Look over the below image and then watch the video at the end (if you haven't seen it already).