0.3.8 • Published 4 years ago
@typed-f/maybe v0.3.8
@typed-f/maybe
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
isJust(this: Maybe<T>): this is Just<T>
Checks whetherthis
isJust
or not. You can use this withif
statement like following code.declare const m: Maybe<number>; if (m.isJust()) { // Now `m` is `Just<number>` console.log(m.value.toFixed(4)); }
isNothing(this: Maybe<T>): this is Nothing<any>
Counterpart ofisJust
. You can use this withif
statement.valueOr(this: Maybe<T>, def: T): T
Ifthis
isJust
, this function returns the inner value ofthis
. If not, this returnsdef
(default value) you passed.valueOrThrow(this: Maybe<T>, err: Error): T
Ifthis
isJust
, this function returns the inner value ofthis
. If not, this throwserr
you passed.valueOrCompute(this: Maybe<T>, f: Fun<[], T>): T
Ifthis
isJust
, this function returns the inner value ofthis
. If not, this invokef
with inner value of currentthis
, and return the result.
Implemented Typeclasses
- Monad
bind<U>(this: Maybe<T>, f: Fun<[T], Maybe<U>>): Maybe<U>
Ifthis
isJust
, appliesf
to its inner value and return the result. Ifthis
isNothing
, returnsthis
.
- Applicative
unit<U>(v: U): Maybe<any, U>
ReturnJust
ofv
.ap<U>(this: Maybe<T>, f: Maybe<Fun<[T], U>>): Maybe<U>
Ifthis
isJust
andf
isJust
, applies the inner value off
to the inner value ofthis
and returnsJust
of the result. Ifthis
isJust
butf
isNothing
, returnsf
. Ifthis
isNothing
, returnsthis
.
- Functor
map<U>(this: Maybe<T>, f: Fun<[R], U>): Maybe<U>
Ifthis
isJust
, appliesf
to the inner value ofthis
and returnsJust
of the result. Ifthis
isNothing
, returnsthis
.
- Matchable
matchWith<U>(this: Maybe<T>, cases: MaybePatterns<T, U>): U
Ifthis
isJust
, appliescases.just
to the inner value ofthis
and returns the result. Ifthis
isNothing
, invokecases.nothing
and returns the result.
- Setoid
equals(other: Maybe<any>): boolean
Ifthis
equals toother
, returntrue
. Ifthis
does not equal toother
, returnfalse
. When boththis
andother
areJust
s and have complex inner values (object), this function triesequals
method of inner value ofthis
. In other words,this.equals(other) === this.value.equals(other.value)
notEquals(other: Maybe<any>): boolean
Returnstrue
if and only ifthis.equals(other)
returnsfalse
.
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>
ReturnsJust
ofvalue
.of
Alias ofunit
from<T>(value?: null): Nothing<T>
from<T>(value: T): Just<T>
ReturnsNothing
forvalue
that isnull
orundefined
, and returnsJust
ofvalue
for other values.maybe
Alias offrom
sequenceObject<O extends object>(obj: { [K in keyof O]: Maybe<O[K]> }): Maybe<O>
Takes anobj
ofMaybe
s and returns anMaybe
of object. If for all keys ofobj
, correspoding values areJust
, then this will return aJust
of object whose keys are original keys and correspoding values are inner value of original values (Just
s). If for one or more keys ofobj
, values areNothing
, then this will returnNothing
.sequenceArray<T>(array: Maybe<T>[]): Maybe<T[]>
Takes anarray
ofMaybe
s and returns anMaybe
of array. If all entries ofarray
areJust
, this will return aJust
of array whose entries are inner values of original entries. Corresponding entries will have same indices. If one or more entries ofarray
areNothing
, this will return aNothing
.map<T, U>(f: Fun<[T], U>): Fun<[Maybe<T>], Maybe<U>>
Returns a function that takesMaybe<T>
and maps its inner value usingf
.map(f)(a)
is same witha.map(f)
.