-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add monoid and monad for Promise
- Loading branch information
1 parent
cb35d66
commit e1cde54
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))), | ||
}; |