Comparing version 0.0.740 to 0.0.741
@@ -11,4 +11,6 @@ import { Narrow, Intersect } from './type'; | ||
type Returns<FS extends readonly Function[]> = { [P in keyof FS]: FS[P] extends FS[number] ? ReturnType<FS[P]> : FS[P]; }; | ||
type Result<T> = { value: T; done: true; } | { value: unknown; done: false; }; | ||
type ReturnResults<FS extends readonly Function[]> = { [P in keyof FS]: FS[P] extends FS[number] ? Result<ReturnType<FS[P]>> : FS[P]; }; | ||
type ReturnResults<FS extends readonly Function[]> = { readonly [P in keyof FS]: FS[P] extends FS[number] ? Result<ReturnType<FS[P]>> : FS[P]; }; | ||
type Result<T> = | ||
| { readonly value: T; readonly done: true; } | ||
| { readonly value: unknown; readonly done: false; }; | ||
@@ -53,3 +55,3 @@ export function bundle<fs extends ((this: undefined, a?: unknown) => unknown)[]>(...fs: fs): (...bs: ParamNs<fs, 0>) => Returns<fs>; | ||
function cancel<T>(cancellers: readonly ((error?: Error) => T)[], error?: Error): Result<T>[] { | ||
function cancel<T>(cancellers: readonly ((error?: Error) => T)[], error?: Error): readonly Result<T>[] { | ||
const results: Result<T>[] = Array(cancellers.length); | ||
@@ -56,0 +58,0 @@ for (let i = 0, g: typeof cancellers[number]; g = cancellers[i]; ++i) { |
@@ -26,3 +26,3 @@ import { AtomicFuture } from './future'; | ||
})); | ||
for (; ;) { | ||
while (true) { | ||
await notifier; | ||
@@ -29,0 +29,0 @@ notifier = new AtomicFuture(); |
@@ -307,3 +307,3 @@ import { Structural, DeepImmutable, DeepRequired } from './type'; | ||
let reply: R | T | undefined; | ||
for (; ;) { | ||
while (true) { | ||
const result = await iter.next(reply!); | ||
@@ -310,0 +310,0 @@ if (result.done) return await result.value; |
135
either.ts
@@ -1,1 +0,134 @@ | ||
export * from './monad/either'; | ||
import { AtomicPromise } from './promise'; | ||
import { curry } from './curry'; | ||
export interface Either<a, b> { | ||
fmap<c>(f: (b: b) => c): Either<a, c>; | ||
ap<b, z>(this: Either<a, (b: b) => z>, b: Either<a, b>): Either<a, z>; | ||
ap<b, c, z>(this: Either<a, (b: b, c: c) => z>, b: Either<a, b>): Either<a, (c: c) => z>; | ||
ap<b, c, d, z>(this: Either<a, (b: b, c: c, d: d) => z>, b: Either<a, b>): Either<a, (c: c, d: d) => z>; | ||
ap<b, c, d, e, z>(this: Either<a, (b: b, c: c, d: d, e: e) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e) => z>; | ||
ap<b, c, d, e, f, z>(this: Either<a, (b: b, c: c, d: d, e: e, f: f) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e, f: f) => z>; | ||
bind<c>(f: (b: b) => Either<a, c>): Either<a, c>; | ||
join<c>(this: Either<a, Either<a, c>>): Either<a, c>; | ||
extract(): b; | ||
extract<c>(left: (a: a) => c): b | c; | ||
extract<c>(left: (a: a) => c, right: (b: b) => c): c; | ||
} | ||
class Right<b> implements Either<never, b> { | ||
constructor( | ||
private readonly value: b, | ||
) { | ||
} | ||
public fmap<c>(f: (b: b) => c): Right<c> { | ||
return new Right(f(this.value)); | ||
} | ||
public ap<b, z>(this: Either<never, (b: b) => z>, b: Either<never, b>): Either<never, z>; | ||
public ap<b, c, z>(this: Either<never, (b: b, c: c) => z>, b: Either<never, b>): Either<never, (c: c) => z>; | ||
public ap<b, c, d, z>(this: Either<never, (b: b, c: c, d: d) => z>, b: Either<never, b>): Either<never, (c: c, d: d) => z>; | ||
public ap<b, c, d, e, z>(this: Either<never, (b: b, c: c, d: d, e: e) => z>, b: Either<never, b>): Either<never, (c: c, d: d, e: e) => z>; | ||
public ap<b, c, d, e, f, z>(this: Either<never, (b: b, c: c, d: d, e: e, f: f) => z>, b: Either<never, b>): Either<never, (c: c, d: d, e: e, f: f) => z>; | ||
public ap<b, z>(this: Either<never, (b: b) => z>, b: Either<never, b>): Either<never, z> { | ||
return Either.ap(this, b); | ||
} | ||
public bind<c, a = never>(f: (b: b) => Right<c>): Right<c>; | ||
public bind<c, a>(f: (b: b) => Either<a, c>): Either<a, c>; | ||
public bind<c, a>(f: (b: b) => Either<a, c>): Either<a, c> { | ||
return f(this.extract()); | ||
} | ||
public join<c>(this: Either<never, Either<never, c>>): Either<never, c> { | ||
return this.bind(m => m); | ||
} | ||
public extract(): b; | ||
public extract<c>(transform: (a: never) => c): b; | ||
public extract<c>(left: (a: never) => c, right: (b: b) => c): c; | ||
public extract<c>(left?: (a: never) => c, right?: (b: b) => c): b | c { | ||
if (right !== undefined) return right(this.value); | ||
return this.value; | ||
assert([left]); | ||
} | ||
} | ||
class Left<a> implements Either<a, never> { | ||
constructor( | ||
private readonly value: a, | ||
) { | ||
} | ||
public fmap<c>(f: (b: never) => c): Left<a> { | ||
return this; | ||
assert(f); | ||
} | ||
public ap<b, z>(this: Either<a, (b: b) => z>, b: Either<a, b>): Either<a, z>; | ||
public ap<b, c, z>(this: Either<a, (b: b, c: c) => z>, b: Either<a, b>): Either<a, (c: c) => z>; | ||
public ap<b, c, d, z>(this: Either<a, (b: b, c: c, d: d) => z>, b: Either<a, b>): Either<a, (c: c, d: d) => z>; | ||
public ap<b, c, d, e, z>(this: Either<a, (b: b, c: c, d: d, e: e) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e) => z>; | ||
public ap<b, c, d, e, f, z>(this: Either<a, (b: b, c: c, d: d, e: e, f: f) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e, f: f) => z>; | ||
public ap<b, z>(this: Either<a, (b: b) => z>, _: Either<a, b>): Either<a, z> { | ||
return this as Left<a>; | ||
} | ||
public bind<b>(f: (b: never) => Either<a, b>): Left<a> { | ||
return this; | ||
assert(f); | ||
} | ||
public join<c>(this: Either<a, Either<a, c>>): Left<a> { | ||
return this as Left<a>; | ||
} | ||
public extract(): never; | ||
public extract<c>(transform: (a: a) => c): c; | ||
public extract<c>(left: (a: a) => c, right: (b: never) => c): c; | ||
public extract<c>(left?: (a: a) => c): c { | ||
if (left !== undefined) return left(this.value); | ||
throw this.value; | ||
} | ||
} | ||
function right<b>(b: b): Right<b>; | ||
function right<a, b>(b: b): Either<a, b>; | ||
function right<a, b>(b: b): Either<a, b> { | ||
return new Right(b); | ||
} | ||
function left<a>(value: a): Left<a> { | ||
return new Left(value); | ||
} | ||
export { | ||
right as Right, | ||
left as Left, | ||
}; | ||
export namespace Either { | ||
export function fmap<a, b, c>(m: Either<a, b>, f: (b: b) => c): Either<a, c>; | ||
export function fmap<a, b>(m: Either<a, b>): <c>(f: (b: b) => c) => Either<a, c>; | ||
export function fmap<a, b, c>(m: Either<a, b>, f?: (b: b) => c): Either<a, c> | (<c>(f: (b: b) => c) => Either<a, c>) { | ||
return f | ||
? m.fmap(f) | ||
: <c>(f: (b: b) => c) => m.fmap(f); | ||
} | ||
export const pure = right; | ||
export function ap<a, b, c>(mf: Either<a, (b: b) => c>, ma: Either<a, b>): Either<a, c>; | ||
export function ap<a, b, c>(mf: Either<a, (b: b) => c>): (ma: Either<a, b>) => Either<a, c>; | ||
export function ap<a, b, c>(af: Either<a, (b: b) => c>, aa?: Either<a, b>): Either<a, c> | ((aa: Either<a, b>) => Either<a, c>) { | ||
return aa | ||
? af.bind(f => aa.fmap(curry(f))) | ||
: (aa: Either<a, b>) => ap(af, aa); | ||
} | ||
export const Return = pure; | ||
export function bind<a, b, c>(m: Either<a, b>, f: (b: b) => Either<a, c>): Either<a, c>; | ||
export function bind<a, b>(m: Either<a, b>): <c>(f: (b: b) => Either<a, c>) => Either<a, c>; | ||
export function bind<a, b, c>(m: Either<a, b>, f?: (b: b) => Either<a, c>): Either<a, c> | (<c>(f: (b: b) => Either<a, c>) => Either<a, c>) { | ||
return f | ||
? m.bind(f) | ||
: <c>(f: (b: b) => Either<a, c>) => bind(m, f); | ||
} | ||
export function sequence<a, b>(fm: Either<a, b>[]): Either<a, b[]>; | ||
export function sequence<a, b>(fm: Either<a, PromiseLike<b>>): AtomicPromise<Either<a, b>>; | ||
export function sequence<a, b>(fm: Either<a, b>[] | Either<a, PromiseLike<b>>): Either<a, b[]> | AtomicPromise<Either<a, b>> { | ||
return Array.isArray(fm) | ||
? fm.reduce((acc, m) => | ||
acc.bind(as => | ||
m.fmap(a => | ||
[...as, a])) | ||
, Return([])) | ||
: fm.extract(b => AtomicPromise.resolve(new Left(b)), a => AtomicPromise.resolve(a).then<Either<a, b>>(Return)); | ||
} | ||
} |
144
maybe.ts
@@ -1,1 +0,143 @@ | ||
export * from './monad/maybe'; | ||
import { AtomicPromise } from './promise'; | ||
import { curry } from './curry'; | ||
export interface Maybe<a> { | ||
fmap<b>(f: (a: a) => b): Maybe<b>; | ||
ap<a, z>(this: Maybe<(a: a) => z>, a: Maybe<a>): Maybe<z> | ||
ap<a, b, z>(this: Maybe<(a: a, b: b) => z>, a: Maybe<a>): Maybe<(b: b) => z> | ||
ap<a, b, c, z>(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe<a>): Maybe<(b: b, c: c) => z> | ||
ap<a, b, c, d, z>(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d) => z> | ||
ap<a, b, c, d, e, z>(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d, e: e) => z> | ||
bind<b>(f: (a: a) => Maybe<b>): Maybe<b>; | ||
join<b>(this: Maybe<Maybe<b>>): Maybe<b>; | ||
guard(cond: boolean): Maybe<a>; | ||
extract(): a; | ||
extract(nothing: () => a): a; | ||
extract<b>(nothing: () => b): a | b; | ||
extract<b>(nothing: () => b, just: (a: a) => b): b; | ||
} | ||
class Just<a> implements Maybe<a> { | ||
constructor( | ||
private readonly value: a, | ||
) { | ||
} | ||
public fmap<b>(f: (a: a) => b): Just<b> { | ||
return new Just(f(this.value)); | ||
} | ||
public ap<a, z>(this: Maybe<(a: a) => z>, a: Maybe<a>): Maybe<z> | ||
public ap<a, b, z>(this: Maybe<(a: a, b: b) => z>, a: Maybe<a>): Maybe<(b: b) => z> | ||
public ap<a, b, c, z>(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe<a>): Maybe<(b: b, c: c) => z> | ||
public ap<a, b, c, d, z>(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d) => z> | ||
public ap<a, b, c, d, e, z>(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d, e: e) => z> | ||
public ap<a, z>(this: Maybe<(...as: any[]) => z>, a: Maybe<a>): Maybe<z> { | ||
return Maybe.ap(this, a); | ||
} | ||
public bind<b>(f: (a: a) => Just<b>): Just<b>; | ||
public bind<b>(f: (a: a) => Maybe<b>): Maybe<b>; | ||
public bind<b>(f: (a: a) => Maybe<b>): Maybe<b> { | ||
return f(this.value); | ||
} | ||
public join<b>(this: Maybe<Maybe<b>>): Maybe<b> { | ||
return this.bind(m => m); | ||
} | ||
public guard(cond: boolean): Maybe<a> { | ||
return cond | ||
? this | ||
: nothing; | ||
} | ||
public extract(): a; | ||
public extract(nothing: () => a): a; | ||
public extract<b>(nothing: () => b): a | b; | ||
public extract<b>(nothing: () => b, just: (a: a) => b): b; | ||
public extract<b>(nothing?: () => b, just?: (a: a) => b): a | b { | ||
if (just !== undefined) return just(this.value); | ||
return this.value; | ||
assert(nothing); | ||
} | ||
} | ||
class Nothing implements Maybe<never> { | ||
public fmap<b>(f: (a: never) => b): Nothing { | ||
return this; | ||
assert(f); | ||
} | ||
public ap<a, z>(this: Maybe<(a: a) => z>, a: Maybe<a>): Nothing; | ||
public ap<a, b, z>(this: Maybe<(a: a, b: b) => z>, a: Maybe<a>): Nothing; | ||
public ap<a, b, c, z>(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe<a>): Nothing; | ||
public ap<a, b, c, d, z>(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe<a>): Nothing; | ||
public ap<a, b, c, d, e, z>(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe<a>): Nothing; | ||
public ap<a, z>(this: Maybe<(...as: any[]) => z>, _: Maybe<a>): Nothing { | ||
return this as Nothing; | ||
} | ||
public bind<b>(f: (a: never) => Maybe<b>): Nothing { | ||
return this; | ||
assert(f); | ||
} | ||
public join<b>(this: Maybe<Maybe<b>>): Nothing { | ||
return this as Nothing; | ||
} | ||
public guard(cond: boolean): Nothing { | ||
return this; | ||
assert(cond); | ||
} | ||
public extract(): never; | ||
public extract<b>(transform: () => b): b; | ||
public extract<b>(nothing: () => b, just: (a: never) => b): b; | ||
public extract<b>(nothing?: () => b): b { | ||
if (nothing !== undefined) return nothing(); | ||
throw new Error(`Spica: Maybe: Nothig value is extracted.`); | ||
assert(just); | ||
} | ||
} | ||
function just<a>(value: a): Just<a> { | ||
return new Just(value); | ||
} | ||
const nothing = new Nothing(); | ||
export { | ||
just as Just, | ||
nothing as Nothing, | ||
}; | ||
export namespace Maybe { | ||
export function fmap<a, b>(m: Maybe<a>, f: (a: a) => b): Maybe<b>; | ||
export function fmap<a>(m: Maybe<a>): <b>(f: (a: a) => b) => Maybe<b>; | ||
export function fmap<a, b>(m: Maybe<a>, f?: (a: a) => b): Maybe<b> | (<b>(f: (a: a) => b) => Maybe<b>) { | ||
return f | ||
? m.fmap(f) | ||
: <b>(f: (a: a) => b) => m.fmap(f); | ||
} | ||
export const pure = just; | ||
export function ap<a, b>(mf: Maybe<(a: a) => b>, ma: Maybe<a>): Maybe<b>; | ||
export function ap<a, b>(mf: Maybe<(a: a) => b>): (ma: Maybe<a>) => Maybe<b>; | ||
export function ap<a, b>(af: Maybe<(a: a) => b>, aa?: Maybe<a>): Maybe<b> | ((aa: Maybe<a>) => Maybe<b>) { | ||
return aa | ||
? af.bind(f => aa.fmap(curry(f))) | ||
: (aa: Maybe<a>) => ap(af, aa); | ||
} | ||
export const Return = pure; | ||
export function bind<a, b>(m: Maybe<a>, f: (a: a) => Maybe<b>): Maybe<b>; | ||
export function bind<a>(m: Maybe<a>): <b>(f: (a: a) => Maybe<b>) => Maybe<b>; | ||
export function bind<a, b>(m: Maybe<a>, f?: (a: a) => Maybe<b>): Maybe<b> | (<b>(f: (a: a) => Maybe<b>) => Maybe<b>) { | ||
return f | ||
? m.bind(f) | ||
: <b>(f: (a: a) => Maybe<b>) => bind(m, f); | ||
} | ||
export function sequence<a>(fm: Maybe<a>[]): Maybe<a[]>; | ||
export function sequence<a>(fm: Maybe<PromiseLike<a>>): AtomicPromise<Maybe<a>>; | ||
export function sequence<a>(fm: Maybe<a>[] | Maybe<PromiseLike<a>>): Maybe<a[]> | AtomicPromise<Maybe<a>> { | ||
return Array.isArray(fm) | ||
? fm.reduce((acc, m) => | ||
acc.bind(as => | ||
m.fmap(a => | ||
[...as, a])) | ||
, Return([])) | ||
: fm.extract(() => AtomicPromise.resolve(Maybe.mzero), a => AtomicPromise.resolve(a).then(Return)) | ||
} | ||
export const mzero: Maybe<never> = nothing; | ||
export function mplus<a>(ml: Maybe<a>, mr: Maybe<a>): Maybe<a> { | ||
return ml.extract(() => mr, () => ml); | ||
} | ||
} |
@@ -12,7 +12,7 @@ import { Monad } from './monad'; | ||
} | ||
public ap<b, z>(this: Either<a, (b: b) => z>, b: Either<a, b>): Either<a, z> | ||
public ap<b, c, z>(this: Either<a, (b: b, c: c) => z>, b: Either<a, b>): Either<a, (c: c) => z> | ||
public ap<b, c, d, z>(this: Either<a, (b: b, c: c, d: d) => z>, b: Either<a, b>): Either<a, (c: c, d: d) => z> | ||
public ap<b, c, d, e, z>(this: Either<a, (b: b, c: c, d: d, e: e) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e) => z> | ||
public ap<b, c, d, e, f, z>(this: Either<a, (b: b, c: c, d: d, e: e, f: f) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e, f: f) => z> | ||
public ap<b, z>(this: Either<a, (b: b) => z>, b: Either<a, b>): Either<a, z>; | ||
public ap<b, c, z>(this: Either<a, (b: b, c: c) => z>, b: Either<a, b>): Either<a, (c: c) => z>; | ||
public ap<b, c, d, z>(this: Either<a, (b: b, c: c, d: d) => z>, b: Either<a, b>): Either<a, (c: c, d: d) => z>; | ||
public ap<b, c, d, e, z>(this: Either<a, (b: b, c: c, d: d, e: e) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e) => z>; | ||
public ap<b, c, d, e, f, z>(this: Either<a, (b: b, c: c, d: d, e: e, f: f) => z>, b: Either<a, b>): Either<a, (c: c, d: d, e: e, f: f) => z>; | ||
public ap<b, z>(this: Either<a, (b: b) => z>, b: Either<a, b>): Either<a, z> { | ||
@@ -24,12 +24,10 @@ return Either.ap(this, b); | ||
const m: Either<a, b> = this.evaluate(); | ||
if (m instanceof Left) { | ||
return m; | ||
switch (m.constructor) { | ||
case Right: | ||
return f(m.extract()); | ||
case Left: | ||
return m as Left<a>; | ||
default: | ||
return m.bind(f); | ||
} | ||
if (m instanceof Right) { | ||
return f(m.extract()); | ||
} | ||
if (m instanceof Either) { | ||
return m.bind(f); | ||
} | ||
throw new TypeError(`Spica: Either: Invalid monad value: ${m}`); | ||
}); | ||
@@ -40,6 +38,5 @@ } | ||
} | ||
public extract(): b | ||
public extract(left: (a: a) => b): b | ||
public extract<c>(left: (a: a) => c): b | c | ||
public extract<c>(left: (a: a) => c, right: (b: b) => c): c | ||
public extract(): b; | ||
public extract<c>(left: (a: a) => c): b | c; | ||
public extract<c>(left: (a: a) => c, right: (b: b) => c): c; | ||
public extract<c>(left?: (a: a) => c, right?: (b: b) => c): b | c { | ||
@@ -50,8 +47,8 @@ return right === undefined | ||
} | ||
public static do<b>(block: () => Iterator<Either<never, b>, Either<never, b>, b>): Either<never, b> | ||
public static do<a, b>(block: () => Iterator<Either<a, b>, Either<a, b>, b>): Either<a, b> | ||
public static do<b>(block: () => Iterator<Either<never, b>, Either<never, b>, b>): Either<never, b>; | ||
public static do<a, b>(block: () => Iterator<Either<a, b>, Either<a, b>, b>): Either<a, b>; | ||
public static do<a, b>(block: () => Iterator<Either<a, b>, Either<a, b>, b>): Either<a, b> { | ||
const iter = block(); | ||
let value: b | undefined; | ||
for (; ;) { | ||
while (true) { | ||
const { value: m, done } = iter.next(value!); | ||
@@ -64,14 +61,14 @@ if (done) return m; | ||
export namespace Either { | ||
export declare function fmap<e, a, b>(m: Either<e, a>, f: (a: a) => b): Either<e, b> | ||
export declare function fmap<e, a>(m: Either<e, a>): <b>(f: (a: a) => b) => Either<e, b> | ||
export function pure<b>(b: b): Right<b> | ||
export function pure<a, b>(b: b): Either<a, b> | ||
export declare function fmap<a, b, c>(m: Either<a, b>, f: (b: b) => c): Either<a, c>; | ||
export declare function fmap<a, b>(m: Either<a, b>): <c>(f: (b: b) => c) => Either<a, c>; | ||
export function pure<b>(b: b): Right<b>; | ||
export function pure<a, b>(b: b): Either<a, b>; | ||
export function pure<a, b>(b: b): Either<a, b> { | ||
return new Right(b); | ||
} | ||
export declare function ap<e, a, b>(mf: Either<e, (a: a) => b>, ma: Either<e, a>): Either<e, b> | ||
export declare function ap<e, a, b>(mf: Either<e, (a: a) => b>): (ma: Either<e, a>) => Either<e, b> | ||
export declare function ap<a, b, c>(mf: Either<a, (b: b) => c>, ma: Either<a, b>): Either<a, c>; | ||
export declare function ap<a, b, c>(mf: Either<a, (b: b) => c>): (ma: Either<a, b>) => Either<a, c>; | ||
export const Return = pure; | ||
export declare function bind<e, a, b>(m: Either<e, a>, f: (a: a) => Either<e, b>): Either<e, b> | ||
export declare function bind<e, a>(m: Either<e, a>): <b>(f: (a: a) => Either<e, b>) => Either<e, b> | ||
export declare function bind<a, b, c>(m: Either<a, b>, f: (b: b) => Either<a, c>): Either<a, c>; | ||
export declare function bind<a, b>(m: Either<a, b>): <c>(f: (b: b) => Either<a, c>) => Either<a, c>; | ||
export function sequence<a, b>(fm: Either<a, b>[]): Either<a, b[]>; | ||
@@ -86,3 +83,3 @@ export function sequence<a, b>(fm: Either<a, PromiseLike<b>>): AtomicPromise<Either<a, b>>; | ||
[...as, a])) | ||
, Return<a, b[]>([])); | ||
, Return([])); | ||
} | ||
@@ -95,13 +92,12 @@ } | ||
} | ||
public override bind<_>(_: (_: never) => Left<a>): Left<a> | ||
public override bind<b>(_: (_: never) => Either<a, b>): Either<a, b> | ||
public override bind<b>(_: (_: never) => Either<a, b>): Either<a, b> { | ||
public override bind<b>(f: (b: never) => Either<a, b>): Left<a> { | ||
return this; | ||
assert(f); | ||
} | ||
public override extract(): never | ||
public override extract<c>(transform: (a: a) => c): c | ||
public override extract<c>(left: (a: a) => c, right: (b: never) => c): c | ||
public override extract(): never; | ||
public override extract<c>(transform: (a: a) => c): c; | ||
public override extract<c>(left: (a: a) => c, right: (b: never) => c): c; | ||
public override extract<c>(left?: (a: a) => c): c { | ||
if (left === undefined) throw this.value; | ||
return left(this.value); | ||
if (left !== undefined) return left(this.value); | ||
throw this.value; | ||
} | ||
@@ -114,15 +110,14 @@ } | ||
} | ||
public override bind<c, _ = never>(f: (b: b) => Right<c>): Right<c> | ||
public override bind<c, a>(f: (b: b) => Either<a, c>): Either<a, c> | ||
public override bind<c, a = never>(f: (b: b) => Right<c>): Right<c>; | ||
public override bind<c, a>(f: (b: b) => Either<a, c>): Either<a, c>; | ||
public override bind<c, a>(f: (b: b) => Either<a, c>): Either<a, c> { | ||
return new Either(() => f(this.extract())); | ||
} | ||
public override extract(): b | ||
public override extract(transform: (a: never) => b): b | ||
public override extract<c>(transform: (a: never) => c): b | ||
public override extract<c>(left: (a: never) => c, right: (b: b) => c): c | ||
public override extract<c>(_?: (a: never) => c, right?: (b: b) => c): b | c { | ||
return right === undefined | ||
? this.value | ||
: right(this.value); | ||
public override extract(): b; | ||
public override extract<c>(transform: (a: never) => c): b; | ||
public override extract<c>(left: (a: never) => c, right: (b: b) => c): c; | ||
public override extract<c>(left?: (a: never) => c, right?: (b: b) => c): b | c { | ||
if (right !== undefined) return right(this.value); | ||
return this.value; | ||
assert([left]); | ||
} | ||
@@ -129,0 +124,0 @@ } |
import { Either, Left, Right } from './either'; | ||
describe('Unit: lib/either', () => { | ||
describe('Unit: lib/monad/either', () => { | ||
const Return = Either.Return; | ||
@@ -5,0 +5,0 @@ |
@@ -12,7 +12,7 @@ import { MonadPlus } from './monadplus'; | ||
} | ||
public ap<a, z>(this: Maybe<(a: a) => z>, a: Maybe<a>): Maybe<z> | ||
public ap<a, b, z>(this: Maybe<(a: a, b: b) => z>, a: Maybe<a>): Maybe<(b: b) => z> | ||
public ap<a, b, c, z>(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe<a>): Maybe<(b: b, c: c) => z> | ||
public ap<a, b, c, d, z>(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d) => z> | ||
public ap<a, b, c, d, e, z>(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d, e: e) => z> | ||
public ap<a, z>(this: Maybe<(a: a) => z>, a: Maybe<a>): Maybe<z>; | ||
public ap<a, b, z>(this: Maybe<(a: a, b: b) => z>, a: Maybe<a>): Maybe<(b: b) => z>; | ||
public ap<a, b, c, z>(this: Maybe<(a: a, b: b, c: c) => z>, a: Maybe<a>): Maybe<(b: b, c: c) => z>; | ||
public ap<a, b, c, d, z>(this: Maybe<(a: a, b: b, c: c, d: d) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d) => z>; | ||
public ap<a, b, c, d, e, z>(this: Maybe<(a: a, b: b, c: c, d: d, e: e) => z>, a: Maybe<a>): Maybe<(b: b, c: c, d: d, e: e) => z>; | ||
public ap<a, z>(this: Maybe<(...as: any[]) => z>, a: Maybe<a>): Maybe<z> { | ||
@@ -24,14 +24,15 @@ return Maybe.ap(this, a); | ||
const m: Maybe<a> = this.evaluate(); | ||
if (m instanceof Just) { | ||
return f(m.extract()); | ||
switch (m.constructor) { | ||
case Just: | ||
return f(m.extract()); | ||
case Nothing: | ||
return m as Nothing; | ||
default: | ||
return m.bind(f); | ||
} | ||
if (m instanceof Nothing) { | ||
return m; | ||
} | ||
if (m instanceof Maybe) { | ||
return m.bind(f); | ||
} | ||
throw new TypeError(`Spica: Maybe: Invalid monad value: ${m}`); | ||
}); | ||
} | ||
public join<b>(this: Maybe<Maybe<b>>): Maybe<b> { | ||
return this.bind(m => m); | ||
} | ||
public guard(cond: boolean): Maybe<a> { | ||
@@ -42,9 +43,5 @@ return cond | ||
} | ||
public join<b>(this: Maybe<Maybe<b>>): Maybe<b> { | ||
return this.bind(m => m); | ||
} | ||
public extract(): a | ||
public extract(nothing: () => a): a | ||
public extract<b>(nothing: () => b): a | b | ||
public extract<b>(nothing: () => b, just: (a: a) => b): b | ||
public extract(): a; | ||
public extract<b>(nothing: () => b): a | b; | ||
public extract<b>(nothing: () => b, just: (a: a) => b): b; | ||
public extract<b>(nothing?: () => b, just?: (a: a) => b): a | b { | ||
@@ -58,3 +55,3 @@ return just === undefined | ||
let value: a | undefined; | ||
for (; ;) { | ||
while (true) { | ||
const { value: m, done } = iter.next(value!); | ||
@@ -67,16 +64,12 @@ if (done) return m; | ||
export namespace Maybe { | ||
export declare function fmap<a, b>(m: Maybe<a>, f: (a: a) => b): Maybe<b> | ||
export declare function fmap<a>(m: Maybe<a>): <b>(f: (a: a) => b) => Maybe<b> | ||
export declare function fmap<a, b>(m: Maybe<a>, f: (a: a) => b): Maybe<b>; | ||
export declare function fmap<a>(m: Maybe<a>): <b>(f: (a: a) => b) => Maybe<b>; | ||
export function pure<a>(a: a): Maybe<a> { | ||
return new Just(a); | ||
} | ||
export declare function ap<a, b>(mf: Maybe<(a: a) => b>, ma: Maybe<a>): Maybe<b> | ||
export declare function ap<a, b>(mf: Maybe<(a: a) => b>): (ma: Maybe<a>) => Maybe<b> | ||
export declare function ap<a, b>(mf: Maybe<(a: a) => b>, ma: Maybe<a>): Maybe<b>; | ||
export declare function ap<a, b>(mf: Maybe<(a: a) => b>): (ma: Maybe<a>) => Maybe<b>; | ||
export const Return = pure; | ||
export declare function bind<a>(m: Maybe<a>, f: (a: a) => Maybe<a>): Maybe<a> | ||
export declare function bind<a, b>(m: Maybe<a>, f: (a: a) => Maybe<b>): Maybe<b> | ||
export declare function bind<a>(m: Maybe<a>): { | ||
(f: (a: a) => Nothing): Maybe<a>; | ||
<b>(f: (a: a) => Maybe<b>): Maybe<b>; | ||
}; | ||
export declare function bind<a, b>(m: Maybe<a>, f: (a: a) => Maybe<b>): Maybe<b>; | ||
export declare function bind<a>(m: Maybe<a>): <b>(f: (a: a) => Maybe<b>) => Maybe<b>; | ||
export function sequence<a>(fm: Maybe<a>[]): Maybe<a[]>; | ||
@@ -91,3 +84,3 @@ export function sequence<a>(fm: Maybe<PromiseLike<a>>): AtomicPromise<Maybe<a>>; | ||
[...as, a])) | ||
, Return<a[]>([])); | ||
, Return([])); | ||
} | ||
@@ -100,13 +93,14 @@ } | ||
} | ||
public override bind<b>(f: (a: a) => Just<b>): Just<b>; | ||
public override bind<b>(f: (a: a) => Maybe<b>): Maybe<b>; | ||
public override bind<b>(f: (a: a) => Maybe<b>): Maybe<b> { | ||
return new Maybe(() => f(this.extract())); | ||
} | ||
public override extract(): a | ||
public override extract(transform: () => a): a | ||
public override extract<b>(transform: () => b): a | ||
public override extract<b>(nothing: () => b, just: (a: a) => b): b | ||
public override extract<b>(_?: () => b, just?: (a: a) => b): a | b { | ||
return just === undefined | ||
? this.value | ||
: just(this.value); | ||
public override extract(): a; | ||
public override extract<b>(transform: () => b): a; | ||
public override extract<b>(nothing: () => b, just: (a: a) => b): b; | ||
public override extract<b>(nothing?: () => b, just?: (a: a) => b): a | b { | ||
if (just !== undefined) return just(this.value); | ||
return this.value; | ||
assert([nothing]); | ||
} | ||
@@ -119,13 +113,12 @@ } | ||
} | ||
public override bind<_>(_: (_: never) => Nothing): Nothing | ||
public override bind<a>(_: (_: never) => Maybe<a>): Maybe<a> | ||
public override bind<a>(_: (_: never) => Maybe<a>): Maybe<a> { | ||
public override bind<b>(f: (a: never) => Maybe<b>): Nothing { | ||
return this; | ||
assert(f); | ||
} | ||
public override extract(): never | ||
public override extract<b>(transform: () => b): b | ||
public override extract<b>(nothing: () => b, just: (a: never) => b): b | ||
public override extract(): never; | ||
public override extract<b>(transform: () => b): b; | ||
public override extract<b>(nothing: () => b, just: (a: never) => b): b; | ||
public override extract<b>(nothing?: () => b): b { | ||
if (nothing === undefined) throw new Error(`Spica: Maybe: Nothig value is extracted.`); | ||
return nothing(); | ||
if (nothing !== undefined) return nothing(); | ||
throw new Error(`Spica: Maybe: Nothig value is extracted.`); | ||
} | ||
@@ -136,10 +129,5 @@ } | ||
export const mzero: Maybe<never> = new Nothing(); | ||
export function mplus<a>(ml: Maybe<a>, mr: Nothing): Maybe<a> | ||
export function mplus<a>(ml: Nothing, mr: Maybe<a>): Maybe<a> | ||
export function mplus<a>(ml: Maybe<a>, mr: Maybe<a>): Maybe<a> | ||
export function mplus<a>(ml: Maybe<a>, mr: Maybe<a>): Maybe<a> { | ||
return new Maybe<a>(() => | ||
ml | ||
.fmap(() => ml) | ||
.extract(() => mr)); | ||
ml.extract(() => mr, () => ml)); | ||
} | ||
@@ -146,0 +134,0 @@ } |
import { Maybe, Just, Nothing } from './maybe'; | ||
import { Sequence } from './sequence'; | ||
describe('Unit: lib/maybe', () => { | ||
describe('Unit: lib/monad/maybe', () => { | ||
const Return = Maybe.Return; | ||
@@ -145,2 +145,6 @@ | ||
it('join', () => { | ||
assert(Return(Return(0)).join().extract() === 0); | ||
}); | ||
it('guard', () => { | ||
@@ -153,6 +157,2 @@ assert(Just(0).guard(true).extract(() => 1) === 0); | ||
it('join', () => { | ||
assert(Return(Return(0)).join().extract() === 0); | ||
}); | ||
it('sequence', async () => { | ||
@@ -159,0 +159,0 @@ assert.deepStrictEqual(Maybe.sequence([Just(0), Just(1)]).extract(), [0, 1]); |
@@ -8,3 +8,3 @@ import { Sequence } from '../../core'; | ||
let iter = () => this.iterate(); | ||
for (; ;) { | ||
while (true) { | ||
const thunk = iter(); | ||
@@ -11,0 +11,0 @@ if (!Sequence.isIterable(thunk)) return acc; |
{ | ||
"name": "spica", | ||
"version": "0.0.740", | ||
"version": "0.0.741", | ||
"description": "Supervisor, Coroutine, Channel, select, AtomicPromise, Cancellation, Cache, List, Queue, Stack, and some utils.", | ||
@@ -5,0 +5,0 @@ "private": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
620028
223
16896