@typed/maybe -- 2.0.0
Well-typed Maybe data structure
Get it
yarn add @typed/maybe
npm install --save @typed/maybe
API Documentation
All functions are curried!
Just
A JSON-serializable Just data-structure
export interface Just<A> {
readonly '@typed/Just': A
}
Just.of<A>(value: A): Just<A>
Creates a Just given a value.
See the code
export function of<A>(value: A): Just<A> {
return { '@typed/Just': value }
}
}
Maybe.of<A>(value: A): Maybe<A>
Creates a Maybe containg a value
See the code
export const of: <A>(value: A) => Maybe<A> = Just.of
}
Maybe<A>
Maybe type. Very useful when errors can occur.
export type Maybe<A> = Just<A> | Nothing
Nothing
The Nothing type, used in place of nulls or undefined.
export interface Nothing {
readonly '@typed/Nothing': true
}
export const Nothing: Nothing = { '@typed/Nothing': true }
ap<A, B>(fn: Maybe<(value: A) => B>, value: Maybe<A>): Maybe<B>
Applies the function contained in a Maybe
to the value contained in a
second Maybe
.
See the code
export const ap: MaybeAp = function ap<A, B>(fn: Maybe<(value: A) => B>, maybe?: Maybe<A>): any {
return maybe ? __ap(fn, maybe) : (maybe: Maybe<A>) => __ap(fn, maybe)
}
function __ap<A, B>(fn: Maybe<(value: A) => B>, maybe: Maybe<A>): Maybe<B> {
return chain(f => map(f, maybe), fn)
}
export interface MaybeAp {
<A, B>(fn: Maybe<(value: A) => B>, value: Maybe<A>): Maybe<B>
<A, B>(fn: Maybe<(value: A) => B>): (value: Maybe<A>) => Maybe<B>
}
fromJust<A>(just: Just<A>): A
Extract the value contained in a Just
See an example
import { fromJust, Just } from '@typed/maybe'
const value = fromJust(Just.of(1))
console.log(value)
See the code
export function fromJust<A>(just: Just<A>): A {
return just['@typed/Just']
}
fromMaybe<A>(defaultValue: A, maybe: Maybe<A>): A
Given a default value and a Maybe returns the default value if the Maybe is a
Nothing or the value contained in a Just.
See the code
export const fromMaybe: FromMaybe = function fromMaybe<A>(defaultValue: A, maybe?: Maybe<A>) {
if (!maybe) return (maybe: Maybe<A>) => __fromMaybe(defaultValue, maybe)
return __fromMaybe(defaultValue, maybe)
}
function __fromMaybe<A>(defaultValue: A, maybe: Maybe<A>): A {
return isJust(maybe) ? fromJust(maybe) : defaultValue
}
export interface FromMaybe {
<A>(defaultValue: A, maybe: Maybe<A>): A
<A>(defaultValue: A): (maybe: Maybe<A>) => A
}
isJust<A>(maybe: Maybe<A>): maybe is Just<A>
Given a Maybe<A> it returns true if the Maybe<A> is Just<A> or
false if it is a Nothing.
See an example
import { isJust, Nothing, Maybe } from '@typed/maybe'
console.log(isJust(Nothing))
console.log(isJust(Maybe.of(1)))
See the code
export function isJust<A>(maybe: Maybe<A>): maybe is Just<A> {
return maybe.hasOwnProperty('@typed/Just')
}
isNothing<A>(maybe: Maybe<A>): maybe is Nothing
Given a Maybe<A> it returns false if the Maybe<A> is Just<A> or
true if it is a Nothing.
See an example
import { isNothing, Maybe, Nothing } from '@typed/maybe'
console.log(isNothing(Nothing))
console.log(isNothing(Maybe.of(1)))
See the code
export function isNothing<A>(maybe: Maybe<A>): maybe is Nothing {
return (maybe as Nothing)['@typed/Nothing'] === true
}