0.3.8 • Published 6 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/maybeAPIs
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 whetherthisisJustor not. You can use this withifstatement 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 withifstatement.valueOr(this: Maybe<T>, def: T): TIfthisisJust, this function returns the inner value ofthis. If not, this returnsdef(default value) you passed.valueOrThrow(this: Maybe<T>, err: Error): TIfthisisJust, this function returns the inner value ofthis. If not, this throwserryou passed.valueOrCompute(this: Maybe<T>, f: Fun<[], T>): TIfthisisJust, this function returns the inner value ofthis. If not, this invokefwith inner value of currentthis, and return the result.
Implemented Typeclasses
- Monad
bind<U>(this: Maybe<T>, f: Fun<[T], Maybe<U>>): Maybe<U>IfthisisJust, appliesfto its inner value and return the result. IfthisisNothing, returnsthis.
- Applicative
unit<U>(v: U): Maybe<any, U>ReturnJustofv.ap<U>(this: Maybe<T>, f: Maybe<Fun<[T], U>>): Maybe<U>IfthisisJustandfisJust, applies the inner value offto the inner value ofthisand returnsJustof the result. IfthisisJustbutfisNothing, returnsf. IfthisisNothing, returnsthis.
- Functor
map<U>(this: Maybe<T>, f: Fun<[R], U>): Maybe<U>IfthisisJust, appliesfto the inner value ofthisand returnsJustof the result. IfthisisNothing, returnsthis.
- Matchable
matchWith<U>(this: Maybe<T>, cases: MaybePatterns<T, U>): UIfthisisJust, appliescases.justto the inner value ofthisand returns the result. IfthisisNothing, invokecases.nothingand returns the result.
- Setoid
equals(other: Maybe<any>): booleanIfthisequals toother, returntrue. Ifthisdoes not equal toother, returnfalse. When boththisandotherareJusts and have complex inner values (object), this function triesequalsmethod of inner value ofthis. In other words,this.equals(other) === this.value.equals(other.value)notEquals(other: Maybe<any>): booleanReturnstrueif 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>ReturnsJustofvalue.ofAlias ofunitfrom<T>(value?: null): Nothing<T>from<T>(value: T): Just<T>ReturnsNothingforvaluethat isnullorundefined, and returnsJustofvaluefor other values.maybeAlias offromsequenceObject<O extends object>(obj: { [K in keyof O]: Maybe<O[K]> }): Maybe<O>Takes anobjofMaybes and returns anMaybeof object. If for all keys ofobj, correspoding values areJust, then this will return aJustof object whose keys are original keys and correspoding values are inner value of original values (Justs). If for one or more keys ofobj, values areNothing, then this will returnNothing.sequenceArray<T>(array: Maybe<T>[]): Maybe<T[]>Takes anarrayofMaybes and returns anMaybeof array. If all entries ofarrayareJust, this will return aJustof array whose entries are inner values of original entries. Corresponding entries will have same indices. If one or more entries ofarrayareNothing, 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).