@sweet-monads/identity
Identity Monad, The Identity monad is a monad that does not embody any computational strategy. It simply applies the bound function to its input without any modification.
This library belongs to sweet-monads project
sweet-monads — easy-to-use monads implementation with static types definition and separated packages.
Usage
npm install @sweet-monads/identity
app.use(
express.static(
path.resolve(getDirname(import.meta.url), "../public")
)
)
Identity.from(import.meta.url)
.map(getDirname)
.map(dir => path.resolve(dir, "../public"))
.map(express.static)
.map(app.use);
API
chain
function chain<A, B>(fn: (v: A) => Promise<Identity<B>>): (m: Identity<A>) => Promise<Identity<B>>;
fn: (v: A) => Promise<Identity<B>>
- function which should be applied asynchronously to Identity<A>
value- Returns function with
Identity<A>
argument and mapped by fn
value (could be used inside Promise#then
function).
Example:
const getValue = async () => from(1);
const result = await getValue()
.then(Identity.chain(async v => from(v * 2)))
.then(Identity.chain(async () => from(null)));
from
function from<T>(value: T): Identity<T>;
- Returns
Identity
which contains value with T
type.
Example:
const v1 = from(2);
const v2 = from<2>(2);
isIdentity
function isIdentity<T>(value: unknown | Identity<T>): value is Identity<T>;
- Returns
boolean
if given value
is instance of Identity constructor.
Example:
const value: unknown = 2;
if (isIdentity(value)) {
}
Identity#join
function join<V>(this: Identity<Identity<V>>): Identity<V>;
this: Identity<Identity<V>>
- Identity
instance which contains another Identity
instance.- Returns unwrapped (inner)
Identity
.
Example:
const v = from(from(2));
v1.join();
Identity#map
function map<Val, NewVal>(fn: (val: Val) => NewVal): Identity<NewVal>;
- Returns mapped by
fn
function value wrapped with Identity
.
Example:
const v = just(2);
const newVal = v.map(a => a.toString());
Identity#asyncMap
function asyncMap<Val, NewVal>(fn: (val: Val) => Promise<NewVal>): Promise<Identity<NewVal>>;
- Returns
Promise
with mapped by fn
function value wrapped with Identity
Example:
const v = from(2);
const newVal = v.asyncMap(a => Promise.resolve(a.toString()));
Identity#apply
function apply<A, B>(this: Identity<(a: A) => B>, arg: Identity<A>): Identity<B>;
function apply<A, B>(this: Identity<A>, fn: Identity<(a: A) => B>): Identity<B>;
this | fn
- function wrapped by Identity, which should be applied to value arg
arg | this
- value which should be applied to fn
- Returns mapped by
fn
function value wrapped by Identity
.
Example:
const v = from(2);
const fn = from((a: number) => a * 2);
const newVal1 = fn.apply(v);
const newVal2 = v.apply(fn);
Identity#asyncApply
Async variant of Identity#apply
function asyncApply<A, B>(
this: Identity<(a: Promise<A> | A) => Promise<B>>,
arg: Identity<Promise<A> | A>
): Promise<Identity<B>>;
function asyncApply<A, B>(this: Identity<Promise<A> | A>, fn: Identity<(a: Promise<A> | A) => Promise<B>>): Promise<Identity<B>>;
this | fn
- function wrapped by Identity, which should be applied to value arg
arg | this
- value which should be applied to fn
- Returns
Promise
with mapped by fn
function value wrapped by Identity
.
Example:
const v = from(2);
const fn = from((a: number) => Promise, resolve(a * 2));
const newVal1 = fn.apply(v);
const newVal2 = v.apply(fn);
Identity#chain
function chain<Val, NewVal>(fn: (val: Val) => Identity<NewVal>): Identity<NewVal>;
- Returns mapped by
fn
function value wrapped by Identity
Example:
const v = from(2);
const newVal = v1.chain(a => from(a.toString()));
Identity#asyncChain
function asyncChain<Val, NewVal>(fn: (val: Val) => Promise<Identity<NewVal>>): Promise<Identity<NewVal>>;
- Returns
Promise
with mapped by fn
function value wrapped with Identity
.
Example:
const v = from(2);
const newVal = v.asyncChain(a => Promise.resolve(from(a.toString())));
Helpers
const { value } = from(2);
const value = from(2).unwrap();
License
MIT (c) Artem Kobzar see LICENSE file.