Skip to content

Commit

Permalink
feat: Add monoid and monad for Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina committed Aug 14, 2022
1 parent cb35d66 commit e1cde54
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Monad1 } from "./type-class/monad";
import type { Monoid } from "./type-class/monoid";

declare const promiseNominal: unique symbol;
export type PromiseHktKey = typeof promiseNominal;

declare module "./hkt" {
interface HktDictA1<A1> {
[promiseNominal]: Promise<A1>;
}
}

export const monoid = <T>(identity: T): Monoid<Promise<T>> => ({
combine: (l, r) => l.then(() => r),
identity: Promise.resolve(identity),
});

export const monad: Monad1<PromiseHktKey> = {
pure: Promise.resolve,
map: (f) => (t) => t.then(f),
flatMap: (f) => (t) => t.then(f),
apply: (f) => (t) => t.then((value) => f.then((func) => func(value))),
};

0 comments on commit e1cde54

Please sign in to comment.