Options
All
  • Public
  • Public/Protected
  • All
Menu

Module io

IO

The monad need to hide some side effect and keep your code clean.

You can use it for working with WebSocket, fetch, console.log, DOM and etc. What could doing some side effect.

import { Either, IO } from 'monad-maniac'

type SideEffectDataType = { [id: number]: string }
let SideEffectData: SideEffectDataType = {
  1: 'Jake',
  2: 'Bob',
  3: 'Alice',
}

const logError = (...args: any[]) => console.error('Got error:', ...args)

const readName = (id: number) => (): Either.Shape<string, string> => {
const name = SideEffectData[id]
return name
  ? Either.right(name)
  : Either.left('Name not found')
}

const writeName = (id: number) => (name: Either.Shape<string, string>) => {
return name.caseOf({
  Left: (error) => {
    logError(error)
    return error
  },
  Right: (name) => SideEffectData[id] = name,
  })
}

const addFired = (name: string) => `${name} was fired!`

// Will get `Jake`
// Changed string to `Jake was fired!`
// Write the string to SideEffectData
// In result will be `Jake was fired!`
const result = IO
  .from(readName(1))
  .map((name) => name.map(addFired))
  .chain(writeName(1))

// Name not found
// Also will show error in console
const resultFailure = IO
  .from(readName(10))
  .map((name) => name.map(addFired))
  .chain(writeName(10))

Index

Classes

Interfaces

Functions

Functions

chain

  • chain<T, U>(fn: (value: ReturnType<T>) => U, io: IO<T>): U
  • chain<T, U>(fn: (value: ReturnType<T>) => U): (io: IO<T>) => U
  • Method like IO.chain but to get IO and call method chain with a function.

    Type parameters

    • T: (...args: any[]) => any

    • U

    Parameters

    • fn: (value: ReturnType<T>) => U
        • (value: ReturnType<T>): U
        • Parameters

          • value: ReturnType<T>

          Returns U

    • io: IO<T>

    Returns U

  • Just curried chain.

    (a -> b) -> IO(a) -> b

    Type parameters

    • T: (...args: any[]) => any

    • U

    Parameters

    • fn: (value: ReturnType<T>) => U
        • (value: ReturnType<T>): U
        • Parameters

          • value: ReturnType<T>

          Returns U

    Returns (io: IO<T>) => U

      • (io: IO<T>): U
      • Parameters

        • io: IO<T>

        Returns U

from

  • from(fn: (...args: any[]) => any): IO<(...args: any[]) => any>
  • Making IO monad from function

    Parameters

    • fn: (...args: any[]) => any
        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    Returns IO<(...args: any[]) => any>

map

  • map<T, U>(fn: (value: ReturnType<T>) => U, io: IO<T>): IO<() => U>
  • map<T, U>(fn: (value: ReturnType<T>) => U): (io: IO<T>) => IO<() => U>
  • Method like IO.map but to get IO and call method map with a function.

    Type parameters

    • T: (...args: any[]) => any

    • U

    Parameters

    • fn: (value: ReturnType<T>) => U
        • (value: ReturnType<T>): U
        • Parameters

          • value: ReturnType<T>

          Returns U

    • io: IO<T>

    Returns IO<() => U>

  • Just curried map.

    (a -> b) -> IO(a) -> IO(b)

    Type parameters

    • T: (...args: any[]) => any

    • U

    Parameters

    • fn: (value: ReturnType<T>) => U
        • (value: ReturnType<T>): U
        • Parameters

          • value: ReturnType<T>

          Returns U

    Returns (io: IO<T>) => IO<() => U>

      • (io: IO<T>): IO<() => U>
      • Parameters

        • io: IO<T>

        Returns IO<() => U>

of

  • of<T>(value: T): IO<() => T>
  • This function are getting some value and contains it in function where the function will returns the value (() => value )

    Type parameters

    • T

    Parameters

    • value: T

    Returns IO<() => T>

run

  • run<T>(io: IO<T>): ReturnType<T>
  • Calling method run from IO instance

    Type parameters

    • T: (...args: any[]) => any

    Parameters

    • io: IO<T>

    Returns ReturnType<T>

toString

  • toString<T>(io: IO<T>): string
  • Calling method toString from IO instance

    Type parameters

    • T: (...args: any[]) => any

    Parameters

    • io: IO<T>

    Returns string

Generated using TypeDoc