# Compose and Pipe

#### Compose:

Composing or Composition is the idea that any sort of data transformation that we do should be obvious.

In a factory, we have a data that gets processed by a function and outputs a data and processed by another function and outputs a data and so for.

`data --> function --> data --> function -->`

Composability is a system design principle, that deals with the relationship between components.

```javascript
Ex:
const compose = (f, g) => (data) => { f(g(data)) };
const multiplyBy3 = (num) => num * 3;
const makePositive = (num) => Math.abs(num);
const multiplyBy3AndAbsolute = compose(multiplyBy3, makePositive);
 multiplyBy3AndAbsolute(-50); // 150
```

#### Pipe:

Pipe is same as compose, instead going from right to left, it goes left to right.

```javascript
Ex:
const pipe = (f, g) => (data) => { g(f(data)) };
const multiplyBy3 = (num) => num * 3;
const makePositive = (num) => Math.abs(num);
const multiplyBy3AndAbsolute = pipe(multiplyBy3, makePositive);
 multiplyBy3AndAbsolute(-50); // 150
 // copose and pipe gives the same result
```

#### Arity:

Arity is simply the number of arguments that a function takes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascript-1.gitbook.io/javascript/fp/compose-and-pipe.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
