1.5.1 • Published 3 years ago

fp-utils-types v1.5.1

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

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)

MethodsArgumentReturn
mapunaryLeft or Right
chainunaryLeft or Right (chain itself returns the value of Left or Right so your unary needs to return Left or Right
concatLeft or RightLeft or Right (concat if both of them are Right. Otherwise keep Left)
foldunary, unaryFirst 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

MethodsArgumentReturn
mapunaryIdentity
chainunaryIdentity (chain itself returns the value of Identity so your unary needs to return Identity
concatIdentityIdentity
foldunaryvalue
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

MethodsSignature
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
1.5.1

3 years ago

1.5.0

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago