kojima/README.md
2025-03-26 21:42:55 -05:00

698 B

kojima

a small functional/monad library

Usage

Example

import { Identity, Some, None, Left, Right, Ok, Err } from 'kojima'

const i = Identity(1)
const thou = Identity(1)
i.bind(x => thou.map(y => x + y)) // Identity(2)

const maybe = Some('thing')
maybe.isSome() // true
const isnt = maybe.bind(() => None).map(_x => 'other') // None
isnt.isSome() // false

const either = Right('neutron')

const value = either.match({
    Right(value) {
        return value
    },
    Left(value) {
        console.error('oh no')
    }
}) // 'neutron'

const result = Ok('3:41am')
result.isOk() // true

result.bind(() => new Error(-Infinity))
    .map(_x => '4:10am')

result.isErr() // true