# fp-utils-types

> FP utilities and types

Latest version **1.5.1** (published 2021-01-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install fp-utils-types
pnpm add fp-utils-types
yarn add fp-utils-types
bun add fp-utils-types
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.1 |
| Published | 2021-01-28 |
| First published | 2020-09-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Takuya Hirata |
| Maintainers | h.takuya |
| Keywords | FP, Functional Programming |

## Links

- npm: https://www.npmjs.com/package/fp-utils-types
- Repository: https://github.com/Takuya-H23/fp-utils-types
- Homepage: https://github.com/Takuya-H23/fp-utils-types#readme
- Issues: https://github.com/Takuya-H23/fp-utils-types/issues
- npm.io page: https://npm.io/package/fp-utils-types

## Recent versions

- 1.5.1 (latest) — 2021-01-28
- 1.5.0 — 2021-01-16
- 1.4.2 — 2021-01-07
- 1.4.1 — 2021-01-07
- 1.4.0 — 2020-12-28
- 1.3.0 — 2020-11-04
- 1.2.3 — 2020-10-21
- 1.2.2 — 2020-09-13
- 1.2.1 — 2020-09-10
- 1.2.0 — 2020-09-10
- 1.1.1 — 2020-09-06
- 1.1.0 — 2020-09-06
- 1.0.0 — 2020-09-04

## README

# Functional Programming utils and types

Hi! I am Takuya. Functional Programming utils and types provides you utilities and types that are well documented and easy to use.

## Documentations

### Compose

**(functions) => composed function**

```
import { compose } from 'fp-utils-types'

const composed = compose(smile, exclaim, toUpper)
composed('hello') // 'HELLO! :)'
```

### CurryN

**(number, function) => curried function**

```
import { curryN } from 'fp-utils-types'

const obj = { name: 'foo' }
const prop = (key, obj, defaultValue) => (obj[key] ? obj[key] : defaultValue)

const curried1 = curryN(3, prop)('name', example)('hey')
const curried2 = curryN(3, prop)('name')(example)('hey')
curried1({ name: 'foo' }) // 'foo'
curried2({ name: 'foo' }) // 'foo'
```

### FilterReducer

**(a -> b) => ((a, b) -> c) => ((a, b) -> c)**

```
import { filterReducer } from 'fp-utils-types'

const ns = [1, 2, 3]
const is1 = x => x === 1

ns.reduce(filterReducer(is1)(combinerByConcat), []) // [1]

```

### MapReducer

**(a -> b) => ((a, b) -> c) => ((a, b) -> c)**

```
import { compose, curryN, mapReducer } from 'fp-utils-types'

const ns = [1, 2, 3]
const add = curryN(2, (x, y) => x + y)
const add1 = add(1)
const combinerByConcat = (acc, x) => acc.concat(x)

const transducer = compose(mapReducer(add1))

ns.reduce(transducer(combinerByConcat), []) // [2, 3, 4]
ns.reduce(transducer(add), 0) // 9

```

### Pipe

**(functions) => piped function**

```
import { pipe } from 'fp-utils-types'

const piped = pipe(toUpper, exclaim, smile)
composed('hello') // 'HELLO! :)'
```

### Either

**(any) => Left(any) | Right(any)**

| Methods | Argument      | Return                                                                                                     |
| ------- | ------------- | ---------------------------------------------------------------------------------------------------------- |
| map     | unary         | Left or Right                                                                                              |
| chain   | unary         | Left or Right (chain itself returns the value of Left or Right so your unary needs to return Left or Right |
| concat  | Left or Right | Left or Right (concat if both of them are Right. Otherwise keep Left)                                      |
| fold    | unary, unary  | First unary is for when the value is Left. Second unary runs when the value is Right                       |

```
import { Either, id } from "fp-utils-types"
const { Left, Right } = Either

const isString = s => typeof(s) === 'string' ? Right(s) : Left(s)

const sayItNicely = s =>
  Either.fromNullable(s).map(toUpper).map(compose(smile, exclaim))

sayItNicely('hello').fold(() => "Left", id) // "HELLO! :)"
sayItNicely(null).fold(() => "Left", id) // "Left"
```

### Identity

| Methods | Argument | Return                                                                                      |
| ------- | -------- | ------------------------------------------------------------------------------------------- |
| map     | unary    | Identity                                                                                    |
| chain   | unary    | Identity (chain itself returns the value of Identity so your unary needs to return Identity |
| concat  | Identity | Identity                                                                                    |
| fold    | unary    | value                                                                                       |

```
import { Identity } from "fp-utils-types"

Identity.of("hello")
      .map(toUpper)
      .map(exclaim)
      .map(smile)
      .fold(id) // "HELLO! :)"

Identity.of("hello")
      .map(toUpper)
      .concat(Identity(" world").map(exclaim).map(smile))
      .fold(id) // "HELLO world! :)"

Identity.of(["hello", "world"])
      .map(x => x.map(toUpper))
      .concat(Identity(["!"]).map(xs => xs.map(smile)))
      .fold(id) // ["HELLO", "WORLD", "! :)"]
```

### IO

a -> IO a

| Methods         | Signature           |
| --------------- | ------------------- |
| map             | (a -> b) -> IO a    |
| chain           | (a -> IO b) -> IO c |
| unsafePerformIO | \_ -> a             |

```
import { IO } from "fp-utils-types"

IO.of(2).map(add1).map(add1).unsafePerformIO()) // 4
IO.of(2)
  .map(x => x * 100)
  .chain(result =>
    IO.of([result, 25]).map(x => x.reduce((acc, y) => add(acc, y)))
   )
  .unsafePerformIO() // 225
```

---
_Source: https://npm.io/package/fp-utils-types · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
