@typed-f/maybe
repo-circleci-badge: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci
Partial type for Typed-F
Installation
# for NPM>=5
npm install @typed-f/maybe
# or
npm i @typed-f/maybe
# for NPM<5
npm install --save @typed-f/maybe
# or
npm i -S @typed-f/maybe
APIs
This package includes type definition for Maybe
type, which is an union type of Just
and Nothing
.
This package also includes their methods and some utility functions for them.
Types
Maybe<T>
Just<T>
Nothing<T>
Methods only for Maybe
Implemented Typeclasses
- Monad
bind<U>(this: Maybe<T>, f: Fun<[T], Maybe<U>>): Maybe<U>
If this
is Just
, applies f
to its inner value and return the result.
If this
is Nothing
, returns this
.
- Applicative
unit<U>(v: U): Maybe<any, U>
Return Just
of v
.ap<U>(this: Maybe<T>, f: Maybe<Fun<[T], U>>): Maybe<U>
If this
is Just
and f
is Just
, applies the inner value of f
to the inner value of this
and returns Just
of the result.
If this
is Just
but f
is Nothing
, returns f
.
If this
is Nothing
, returns this
.
- Functor
map<U>(this: Maybe<T>, f: Fun<[R], U>): Maybe<U>
If this
is Just
, applies f
to the inner value of this
and returns Just
of the result.
If this
is Nothing
, returns this
.
- Matchable
matchWith<U>(this: Maybe<T>, cases: MaybePatterns<T, U>): U
If this
is Just
, applies cases.just
to the inner value of this
and returns the result.
If this
is Nothing
, invoke cases.nothing
and returns the result.
- Setoid
equals(other: Maybe<any>): boolean
If this
equals to other
, return true
.
If this
does not equal to other
, return false
.
When both this
and other
are Just
s and have complex inner values (object), this function tries equals
method of inner value of this
. In other words, this.equals(other) === this.value.equals(other.value)
notEquals(other: Maybe<any>): boolean
Returns true
if and only if this.equals(other)
returns false
.
Utility Functions
You can use these functions like Maybe.<function name>
, for example, in case of map
, you can access it with Maybe.map
.
unit<T>(value: T): Maybe<T>
Returns Just
of value
.of
Alias of unit
from<T>(value?: null): Nothing<T>
from<T>(value: T): Just<T>
Returns Nothing
for value
that is null
or undefined
, and returns Just
of value
for other values.maybe
Alias of from
sequenceObject<O extends object>(obj: { [K in keyof O]: Maybe<O[K]> }): Maybe<O>
Takes an obj
of Maybe
s and returns an Maybe
of object.
If for all keys of obj
, correspoding values are Just
, then this will return a Just
of object whose keys are original keys and correspoding values are inner value of original values (Just
s).
If for one or more keys of obj
, values are Nothing
, then this will return Nothing
.sequenceArray<T>(array: Maybe<T>[]): Maybe<T[]>
Takes an array
of Maybe
s and returns an Maybe
of array.
If all entries of array
are Just
, this will return a Just
of array whose entries are inner values of original entries. Corresponding entries will have same indices.
If one or more entries of array
are Nothing
, this will return a Nothing
.map<T, U>(f: Fun<[T], U>): Fun<[Maybe<T>], Maybe<U>>
Returns a function that takes Maybe<T>
and maps its inner value using f
.
map(f)(a)
is same with a.map(f)
.