# Chapter 05: Coding by Composing

The book heavily relies on a function called `compose` which combines functions `Right -> Left`. `fp-ts` provides a similar way to compose functions via `flow` except that it operates on functions from `Left -> Right`.

{% tabs %}
{% tab title="book" %}

```javascript
const toUpperCase = x => x.toUpperCase();
const exclaim = x => `${x}!`;
const shout = compose(exclaim, toUpperCase);

shout('send in the clowns'); // "SEND IN THE CLOWNS!"
```

{% endtab %}

{% tab title="ts" %}

```typescript
import { flow, identity } from "fp-ts/function";
const toUpperCase = (x: string) => x.toUpperCase();
const exclaim = (x: string) => `${x}!`;
// note: order here is reversed
const shout = flow(toUpperCase, exclaim); 

shout('send in the clowns'); // "SEND IN THE CLOWNS!"
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="book" %}

```javascript
const dasherize = compose(
  intercalate('-'),
  map(toLower),
  split(' '),
  replace(/\s{2,}/ig, ' '),
);

dasherize('The world is a vampire'); // 'the-world-is-a-vampire'
```

{% endtab %}

{% tab title="ts" %}

```typescript
import { map, Foldable } from "fp-ts/Array";
import * as S from "fp-ts/string";
import { intercalate } from "fp-ts/Foldable";

const replace = (search: string | RegExp) => (replace: string) => (s: string) =>
  s.replace(search, replace);
const split = (search: string | RegExp) => (s: string) => s.split(search);
const toLower = (s: string) => s.toLocaleLowerCase();

const stringIntercalculate = (sep: string) => (s: string[]) =>
  intercalate(S.Monoid, Foldable)(sep, s);

const dasherize = flow(
  replace(/\s{2,}/gi)(" "),
  split(" "),
  map(toLower),
  stringIntercalculate("-")
);

let output = dasherize("The world is a vampire"); // 'the-world-is-a-vampire'
```

{% endtab %}
{% endtabs %}

### Pipe

`fp-ts` also provides a function called `pipe` which operates in a similar manor, except the first parameter acts as the input:

```typescript
let output = pipe(
  'send in the clowns', 
  toUpperCase, 
  exclaim
);  // "SEND IN THE CLOWNS!"
```


---

# 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://cjonas.gitbook.io/mostly-adequate-fp-ts/chapter-5.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.
