96 lines
2.4 KiB
Markdown
96 lines
2.4 KiB
Markdown
# kojima
|
|
a small functional/monad library
|
|
|
|
# Usage
|
|
## Example
|
|
```js
|
|
import { Option, Some, None, Result, Ok, Err } from 'kojima'
|
|
|
|
const maybe = Option.of('thing')
|
|
maybe.isSome() // true
|
|
const isnt = maybe.chain(None).map(_x => 'other') // None
|
|
isnt.isSome() // false
|
|
|
|
const result = Result.of('3:41am')
|
|
result.isOk() // true
|
|
|
|
result.chain(() => Err(new Error(-Infinity)))
|
|
.map(_x => '4:10am')
|
|
.chain(Ok)
|
|
|
|
result.isErr() // true
|
|
|
|
// crimes!
|
|
None == None() // true
|
|
```
|
|
|
|
## Documentation
|
|
|
|
### curry
|
|
> (* -> a) -> (* -> a)
|
|
|
|
Returns a curried equivalent of the provided function.
|
|
|
|
```js
|
|
import { curry } from './curry.js'
|
|
|
|
const add = (a, b, c) => a + b + c
|
|
const curriedAdd = curry(add)
|
|
const add1 = curriedAdd(1)
|
|
const add3 = add1(2)
|
|
const add4 = curriedAdd(2, 2)
|
|
|
|
const six = add3(3) // 6
|
|
const eight = add4(2) // 7
|
|
const twelve = curriedAdd(4, 4, 4) // 12
|
|
```
|
|
|
|
### Option
|
|
|
|
Represents a value which may not exist.
|
|
|
|
#### Methods
|
|
##### of :: Option f => a -> f a
|
|
> Option.of\<a\>(value: a) -> Option\<a\>
|
|
|
|
##### zero :: Option f => () -> f a
|
|
> Option.zero\<a\>(value: a) -> Option\<a\>
|
|
|
|
##### chain :: Option m => m a ~> (a -> m b) -> m b
|
|
> Option\<T\>.chain\<U\>(fn: (value: T) -> Option\<U\>) -> Option\<U\>
|
|
|
|
##### map :: Option f => f a ~> (a -> b) -> f b
|
|
> Option\<T\>.map\<U\>(fn: (value: T) -> U) -> Option\<U\>
|
|
|
|
##### alt :: Option f => f a ~> f a -> f a
|
|
> Option\<T\>.alt(other: Option\<T\>) -> Option\<T\>
|
|
|
|
##### fold :: Option f => f a ~> ((b, a) -> b, b) -> b
|
|
> Option\<T\>.fold\<U\>(fn: ((acc: U, value: T) -> U, initial: U) -> U) -> U
|
|
|
|
### Result
|
|
|
|
Represents a value which may fail.
|
|
|
|
#### Methods
|
|
##### of :: Result f => a -> f a
|
|
> Result.of\<a\>(value: a) -> Result\<a, ()\>
|
|
|
|
##### zero :: Result f => () -> f a
|
|
> Result.zero() -> Result\<(), ()\>
|
|
|
|
##### chain :: Result m => m a ~> (a -> m b) -> m b
|
|
> Result\<T, E\>.chain\<U\>(fn: (value: T) -> Result<U, E>) -> Result\<U, E\>
|
|
|
|
##### map :: Result f => f a ~> (a -> b) -> f b
|
|
> Result\<T, E\>.map\<U\>(fn: (value: T) -> U) -> Result\<U, E\>
|
|
|
|
##### alt :: Result f => f a ~> f a -> f a
|
|
> Result\<T, E\>.alt(other: Result\<T, E\>) -> Result\<T, E\>
|
|
|
|
##### fold :: Result f => f a ~> ((b, a) -> b, b) -> b
|
|
> Result\<T, E\>.fold\<U\>(fn: ((acc: U, value: T) -> U, initial: U) -> U) -> U
|
|
|
|
##### bimap :: Result f => f a c ~> (a -> b, c -> d) -> f b d
|
|
> Result\<T1, E1\>.bimap\<T2, E2\>(x: (value: T1) -> T2, y: (error: E1) -> E2) -> Result\<T2, E2\>
|
|
|