Curried lambdas

master
Freywar Ulvnaudgari 2 years ago
parent fbe9ba511f
commit 69c5070f9a
  1. 26
      src/bool.ts
  2. 26
      src/core.ts
  3. 71
      src/list.ts
  4. 44
      src/num.ts
  5. 47
      test/bool.spec.ts
  6. 10
      test/core.spec.ts
  7. 145
      test/list.spec.ts
  8. 111
      test/num.spec.ts
  9. 38
      test/util.ts

@ -1,6 +1,6 @@
import { Lazy, Q, V, _ } from './core'; import { L, P, _, fromNative, toNative } from './core';
export type Bool = Lazy<<T extends V>(t: T) => Lazy<(f: T) => T>>; export type Bool = L<<T extends P>(t: T) => L<(f: T) => T>>;
export const True: Bool export const True: Bool
= _(t => _(_f => t)) as Bool; = _(t => _(_f => t)) as Bool;
@ -8,23 +8,23 @@ export const True: Bool
export const False: Bool export const False: Bool
= _(_t => _(f => f)); = _(_t => _(f => f));
export const iif: <T extends V>(b: Bool) => Lazy<(t: T) => Lazy<(f: T) => T>> export const iif: L<<T extends P>(b: Bool) => L<(t: T) => L<(f: T) => T>>>
= b => _(t => _(f => b.then(b => b(t).then(e=>e(f))))); = _(b => _(t => _(f => b._(t)._(f))));
export const not: (b: Bool) => Bool export const not: L<(b: Bool) => Bool>
= b => b.then(b => b(False).then(e=>e(True))); = _(b => b._(False)._(True));
export const and: (l: Bool) => Lazy<(r: Bool) => Bool> export const and: L<(l: Bool) => L<(r: Bool) => Bool>>
= l => _(r => l.then(l => l(r).then(e=>e(False)))); = _(l => _(r => l._(r)._(False)));
export const or: (l: Bool) => Lazy<(r: Bool) => Bool> export const or: L<(l: Bool) => L<(r: Bool) => Bool>>
= l => _(r => l.then(l => l(True).then(e=>e(r)))); = _(l => _(r => l._(True)._(r)));
export const xor: (l: Bool) => Lazy<(r: Bool) => Bool> export const xor: L<(l: Bool) => L<(r: Bool) => Bool>>
= l => _(r => l.then(l => l(not(r)).then(e=>e(r)))); = _(l => _(r => l._(not._(r))._(r)));
export const fromBoolean: (b: boolean) => Bool export const fromBoolean: (b: boolean) => Bool
= b => b ? True : False; = b => b ? True : False;
export const toBoolean: (b: Bool) => boolean export const toBoolean: (b: Bool) => boolean
= b => b.then(b => b(_(true as unknown as Q)).then(e=>e(_(false as unknown as Q)))).value as unknown as boolean; = b => toNative(b._(fromNative(true))._(fromNative(false)));

@ -1,4 +1,4 @@
export class Lazy<T extends (v: unknown) => Lazy<any>> { export class L<T extends (v: unknown) => L<any>> {
protected _value?: T; protected _value?: T;
constructor(private _eval: () => T) { } constructor(private _eval: () => T) { }
@ -13,23 +13,25 @@ export class Lazy<T extends (v: unknown) => Lazy<any>> {
return this._value = this._eval(); return this._value = this._eval();
} }
public then<R extends V>(f: (v: T) => R): R { public get _(): T {
return new Lazy(() => f(this.value).value) as R; return ((p: Parameters<T>[0]) => new L(() => this.value(p).value)) as T;
} }
} }
export type Q = (v: unknown) => Lazy<any>; export type P = L<(v: unknown) => L<any>>;
export type V = Lazy<(v: unknown) => Lazy<any>>; export const _ = <T extends ((v: unknown) => L<any>)>(v: T): L<T> => new L(() => v);
export const _ = <T extends (v: unknown) => Lazy<any>>(v: T): Lazy<T> => new Lazy(() => v); export const fromNative: <T>(v: T) => L<any>
export const $ = <T extends (v: unknown) => Lazy<any>>(v: Lazy<T>): T => v.value; = v => new L(() => v as any);
export const toNative: <T>(v: L<any>) => T
= <T>(v: L<any>) => v.value as T;
export const undef: any = new Lazy(() => { export const undef: any = new L(() => {
throw new Error('undefined'); throw new Error('undefined');
}); });
export const id: <T extends V>(v: T) => T export const id: L<<T extends P>(v: T) => T>
= v => v; = _(v => v);
export const cnst: <T extends V> (v: T) => Lazy<(_: unknown) => T> export const cnst: L<<T extends P>(v: T) => L<(_: unknown) => T>>
= v => _(_ => v); = _(v => _(_ => v)) as L<<T extends P>(v: T) => L<(_: unknown) => T>>;

@ -1,50 +1,65 @@
import { Lazy, Q, V, _, undef } from './core'; import { L, P, _, fromNative, toNative, undef } from './core';
import { fromNumber, Num, toNumber } from './num'; import { Num, fromNumber, toNumber } from './num';
import { Bool, iif } from './bool'; import { Bool, iif } from './bool';
export type List<T extends V> = Lazy<<R extends V>(z: R) => Lazy<(f: (x: T) => Lazy<(xs: List<T>) => R>) => R>>; export type List<T extends P> = L<<R extends P>(z: R) => L<(f: L<(x: T) => L<(xs: List<T>) => R>>) => R>>;
export const Nil: List<any> export const Nil: List<any>
= _(z => _(_f => z)) as List<any>; = _(z => _(_f => z)) as List<any>;
export const Cons: <T extends V>(x: T) => Lazy<(xs: List<T>) => List<T>> export const Cons: L<<T extends P>(x: T) => L<(xs: List<T>) => List<T>>>
= <T extends V>(x: T) => _((xs: List<T>) => _(_z => _(f => f(x).then(e=>e(xs)))) as List<T>); = _(<T extends P>(x: T) => _((xs: List<T>) => _(_z => _(f => f._(x)._(xs))) as List<T>));
export const cons: <T extends V>(x: T) => Lazy<(xs: List<T>) => List<T>> export const cons: L<<T extends P>(x: T) => L<(xs: List<T>) => List<T>>>
= Cons; = Cons;
export const head: <T extends V>(xs: List<T>) => T export const head: L<<T extends P>(xs: List<T>) => T>
= xs => xs.then(xs => xs(undef).then(e=>e(x=>_(_xs=>x)))); = _(xs => xs._(undef)._(_(x => _(_xs => x))));
export const tail: <T extends V>(xs: List<T>) => List<T> export const tail: L<<T extends P>(xs: List<T>) => List<T>>
= xs => xs.then(xs => xs(undef).then(e=>e(_x =>_(xs=>xs)))); = _(xs => xs._(undef)._(_(_x => _(xs => xs))));
export const foldl: <T extends V, R extends V>(f: (a: R) => Lazy<(x: T) => R>) => Lazy<(z: R) => Lazy<(xs: List<T>) => R>> export const foldl: L<<T extends P, R extends P>(f: L<(a: R) => L<(x: T) => R>>) => L<(z: R) => L<(xs: List<T>) => R>>>
= f => _(z => _(xs => xs.then(xs => xs(z).then(e=>e(x => _(xs => foldl(f).then(e=>e(f(z).then(e=>e(x))).then(e=>e(xs))))))))); =
_(<T extends P, R extends P>(f: L<(a: R) => L<(x: T) => R>>) =>
_((z: R) =>
_((xs: List<T>) => xs._(z)._(_(x =>
_(xs => foldl._(f)._(f._(z)._(x))._(xs)))))));
export const foldlz: <T extends V>(f: (a: T) => Lazy<(x: T) => T>) => Lazy<(xs: List<T>) => T> export const foldlz: L<<T extends P>(f: L<(a: T) => L<(x: T) => T>>) => L<(xs: List<T>) => T>>
= f => _(xs => xs.then(xs => xs(undef).then(e=>e(x=>_(xs=>foldl(f).then(e=>e(x).then(e=>e(xs)))))))); =
_(<T extends P>(f: L<(a: T) => L<(x: T) => T>>) =>
_((xs:List<T>) =>
xs._(undef)._(_(x =>
_(xs => foldl._(f)._(x)._(xs))))));
export const foldr: <T extends V, R extends V>(f: (x: T) => Lazy<(a: R) => R>) => Lazy<(z: R) => Lazy<(xs: List<T>) => R>> export const foldr: L<<T extends P, R extends P>(f: L<(x: T) => L<(a: R) => R>>) => L<(z: R) => L<(xs: List<T>) => R>>>
= f => _(z => _(xs => xs.then(xs => xs(z).then(e=>e(x => _(xs => f(x).then(e=>e(foldr(f).then(e=>e(z).then(e=>e(xs))))))))))); =
_(<T extends P, R extends P>(f: L<(x: T) => L<(a: R) => R>>) =>
_((z: R) =>
_((xs: List<T>) => xs._(z)._(_(x => _(xs => f._(x)._(foldr._(f)._(z)._(xs))))))));
export const foldrz: <T extends V>(f: (x: T) => Lazy<(a: T) => T>) => Lazy<(xs: List<T>) => T> export const foldrz: L<<T extends P>(f: L<(x: T) => L<(a: T) => T>>) => L<(xs: List<T>) => T>>
= _f => _(_z => undef); = _(_f => _(_z => undef));
export const map: <T extends V, R extends V>(f: (x: T) => R) => Lazy<(xs: List<T>) => List<R>> export const map: L<<T extends P, R extends P>(f: L<(x: T) => R>) => L<(xs: List<T>) => List<R>>>
= f => _(xs => xs.then(xs => xs(Nil).then(e=>e(x =>_( xs => Cons(f(x)).then(e=>e(map(f).then(e=>e(xs))))))))); =
_(<T extends P, R extends P>(f: L<(x: T) => R>) =>
_((xs: List<T>) => xs._(Nil)._(_(x => _(xs => Cons._(f._(x))._(map._(f)._(xs)))))));
export const filter: <T extends V>(f: (x: T) => Bool) => Lazy<(xs: List<T>) => List<T>> export const filter: L<<T extends P>(f: L<(x: T) => Bool>) => L<(xs: List<T>) => List<T>>>
= <T extends V>(f: (x: T) => Bool) => _((xs: List<T>) => xs.then(xs => xs(Nil).then(e=>e(x => _(xs => iif<List<T>>(f(x)).then(e=>e(Cons(x).then(e=>e(filter(f).then(e=>e(xs))))).then(e=>e(filter(f).then(e=>e(xs)))))))))); =
_(<T extends P>(f: L<(x: T) => Bool>) =>
_((xs: List<T>) => xs._(Nil)._(_(x => _(xs => iif._(f._(x))._(Cons._(x)._(filter._(f)._(xs)))._(filter._(f)._(xs)))))));
export const fromArray: <T extends V>(xs: T[]) => List<T> export const fromArray: <T extends P>(xs: T[]) => List<T>
= xs => xs.length === 0 ? Nil : new Lazy(() => Cons(xs[0]).then(e=>e(fromArray(xs.slice(1)))).value); = xs => xs.length === 0 ? Nil : Cons._(xs[0])._(new L(() => fromArray(xs.slice(1)).value));
export const fromNumberArray: (xs: number[]) => List<Num> export const fromNumberArray: (xs: number[]) => List<Num>
= xs => xs.length === 0 ? Nil : new Lazy(() => Cons(fromNumber(xs[0])).then(e=>e(fromNumberArray(xs.slice(1)))).value); = xs => xs.length === 0 ? Nil : Cons._(fromNumber(xs[0]))._(new L(() => fromNumberArray(xs.slice(1)).value));
export const toArray: <R extends Q, T extends Lazy<R>>(xs: List<T>) => R[] export const toArray: <T extends P>(xs: List<T>) => P[]
= <R extends Q, T extends Lazy<R>>(xs: List<T>) => xs.then(xs => xs(_([] as R[] as unknown as Q)).then(e=>e(x => _(xs => _([x.value, ...toArray<R, T>(xs)] as unknown as Q))))).value as unknown as R[]; = xs => toNative(xs._(fromNative([]))._(_(x => _(xs => fromNative([x, ...toArray(xs)])))));
export const toNumberArray: (xs: List<Num>) => number[] export const toNumberArray: (xs: List<Num>) => number[]
= xs => xs.then(xs => xs(_([] as number[]as unknown as Q)).then(e=>e(x => _(xs => _([toNumber(x), ...toNumberArray(xs)]as unknown as Q))))).value as unknown as number[]; = xs => toNative(xs._(fromNative([]))._(_(x => _(xs => fromNative([toNumber(x), ...toNumberArray(xs)])))));

@ -1,40 +1,40 @@
import { Bool, not, True } from './bool'; import { Bool, False, True } from './bool';
import { Lazy, Q, V, _, id } from './core'; import { L, P, _, fromNative, id, toNative } from './core';
export type Num = Lazy<<T extends V>(z: T) => Lazy<(n: (p: Num) => T) => T>>; export type Num = L<<T extends P>(z: T) => L<(n: L<(p: Num) => T>) => T>>;
export const Zero: Num export const Zero: Num
= _(z => _(_n => z)) as Num; = _(z => _(_n => z)) as Num;
export const Succ: (p: Num) => Num export const Succ: L<(p: Num) => Num>
= p => _(_z => _(n => n(p))); = _(p => _(_z => _(n => n._(p))));
export const ifz: <T extends V>(n: Num) => Lazy<(t: T) => Lazy<(f: T) => T>> export const ifz: L<<T extends P>(n: Num) => L<(t: T) => L<(f: T) => T>>>
= n => _(t => _(f => n.then(n => n(t).then(e => e(_ => f))))); = _(n => _(t => _(f => n._(t)._(_(_ => f)))));
export const succ: (n: Num) => Num export const succ: L<(n: Num) => Num>
= Succ; = Succ;
export const pred: (n: Num) => Num export const pred: L<(n: Num) => Num>
= n => n.then(n => n(Zero).then(e=>e(id))); = _(n => n._(Zero)._(id));
export const even: (n: Num) => Bool export const even: L<(n: Num) => Bool>
= n => n.then(n => n(True).then(e=>e(odd))); = _(n => n._(True)._(odd));
export const odd: (n: Num) => Bool export const odd: L<(n: Num) => Bool>
= n => not(even(n)); = _(n => n._(False)._(even));
export const sum: (l: Num) => Lazy<(r: Num) => Num> export const sum: L<(l: Num) => L<(r: Num) => Num>>
= l => _(r => r.then(r => r(l).then(e=>e(t=>sum(Succ(l)).then(e=>e(t)))))); = _(l => _(r => r._(l)._(_(t => sum._(Succ._(l))._(t)))));
export const sub: (l: Num) => Lazy<(r: Num) => Num> export const sub: L<(l: Num) => L<(r: Num) => Num>>
= l => _(r => r.then(r => r(l).then(e=>e(pr => l.then(l => l(Zero).then(e=>e(pl => sub(pl).then(e=>e(pr))))))))); = _(l => _(r => r._(l)._(_(pr => l._(Zero)._(_(pl => sub._(pl)._(pr)))))));
export const mul: (l: Num) => Lazy<(r: Num) => Num> export const mul: L<(l: Num) => L<(r: Num) => Num>>
= l => _(r => l.then(l => l(Zero).then(e=>e(pl => sum(r).then(e=>e(mul(pl).then(e=>e(r)))))))); = _(l => _(r => l._(Zero)._(_(pl => sum._(r)._(mul._(pl)._(r))))));
export const fromNumber: (n: number) => Num export const fromNumber: (n: number) => Num
= n => !n ? Zero : new Lazy(() => Succ(fromNumber(n - 1)).value); = n => !n ? Zero : Succ._(new L(() => fromNumber(n - 1).value));
export const toNumber: (n: Num) => number export const toNumber: (n: Num) => number
= n => n.then(n => n(_(0 as unknown as Q)).then(e=>e(p => _((1 + toNumber(p)) as unknown as Q)))).value as unknown as number; = n => toNative(n._(fromNative(0))._(_(p => fromNative(1 + toNumber(p)))));

@ -1,116 +1,115 @@
import { describe, expect, it } from '@jest/globals'; import { describe, expect, it } from '@jest/globals';
import { _, Q } from '../src/core'; import { toBoolean as $, False, True, fromBoolean as _, and, iif, not, or, xor } from '../src/bool';
import { True, False, iif, and, not, or, xor, toBoolean, fromBoolean } from '../src/bool';
describe('Bool', () => { describe('Bool', () => {
describe('toBoolean', () => { describe('toBoolean', () => {
it('converts True', () => { it('converts True', () => {
expect(toBoolean(True)).toBe(true); expect($(True)).toBe(true);
}); });
it('converts False', () => { it('converts False', () => {
expect(toBoolean(False)).toBe(false); expect($(False)).toBe(false);
}); });
}); });
describe('fromBoolean', () => { describe('fromBoolean', () => {
it('converts true', () => { it('converts true', () => {
expect(toBoolean(fromBoolean(true))).toBe(true); expect($(_(true))).toBe(true);
}); });
it('converts false', () => { it('converts false', () => {
expect(toBoolean(fromBoolean(false))).toBe(false); expect($(_(false))).toBe(false);
}); });
}); });
describe('iif', () => { describe('iif', () => {
it('returns true argument', () => { it('returns true argument', () => {
expect(iif).toDeferEvaluationOf(False, expect.skipped(_(true as unknown as Q)), _(false as unknown as Q)); expect(iif).toDeferEvaluationOf(False, expect.skipped(True), False);
expect(iif(True).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(true); expect($(iif._(True)._(True)._(False))).toBe(true);
}); });
it('returns false argument', () => { it('returns false argument', () => {
expect(iif).toDeferEvaluationOf(True, _(true as unknown as Q), expect.skipped(_(false as unknown as Q))); expect(iif).toDeferEvaluationOf(True, True, expect.skipped(False));
expect(iif(False).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(false); expect($(iif._(False)._(True)._(False))).toBe(false);
}); });
}); });
describe('not', () => { describe('not', () => {
it('not False is True', () => { it('not False is True', () => {
expect(not).toDeferEvaluationOf(False); expect(not).toDeferEvaluationOf(False);
expect(toBoolean(not(False))).toBe(true); expect($(not._(False))).toBe(true);
}); });
it('not True is False', () => { it('not True is False', () => {
expect(not).toDeferEvaluationOf(True); expect(not).toDeferEvaluationOf(True);
expect(toBoolean(not(True))).toBe(false); expect($(not._(True))).toBe(false);
}); });
}); });
describe('and', () => { describe('and', () => {
it('False and False is False', () => { it('False and False is False', () => {
expect(and).toDeferEvaluationOf(False, expect.skipped(False)); expect(and).toDeferEvaluationOf(False, expect.skipped(False));
expect(toBoolean(and(False).then(e => e(False)))).toBe(false); expect($(and._(False)._(False))).toBe(false);
}); });
it('False and True is False', () => { it('False and True is False', () => {
expect(and).toDeferEvaluationOf(False, expect.skipped(True)); expect(and).toDeferEvaluationOf(False, expect.skipped(True));
expect(toBoolean(and(False).then(e => e(True)))).toBe(false); expect($(and._(False)._(True))).toBe(false);
}); });
it('True and False is False', () => { it('True and False is False', () => {
expect(and).toDeferEvaluationOf(True, False); expect(and).toDeferEvaluationOf(True, False);
expect(toBoolean(and(True).then(e => e(False)))).toBe(false); expect($(and._(True)._(False))).toBe(false);
}); });
it('True and True is True', () => { it('True and True is True', () => {
expect(and).toDeferEvaluationOf(True, True); expect(and).toDeferEvaluationOf(True, True);
expect(toBoolean(and(True).then(e => e(True)))).toBe(true); expect($(and._(True)._(True))).toBe(true);
}); });
}); });
describe('or', () => { describe('or', () => {
it('False or False is False', () => { it('False or False is False', () => {
expect(or).toDeferEvaluationOf(False, False); expect(or).toDeferEvaluationOf(False, False);
expect(toBoolean(or(False).then(e => e(False)))).toBe(false); expect($(or._(False)._(False))).toBe(false);
}); });
it('False or True is True', () => { it('False or True is True', () => {
expect(or).toDeferEvaluationOf(False, True); expect(or).toDeferEvaluationOf(False, True);
expect(toBoolean(or(False).then(e => e(True)))).toBe(true); expect($(or._(False)._(True))).toBe(true);
}); });
it('True or False is True', () => { it('True or False is True', () => {
expect(or).toDeferEvaluationOf(True, expect.skipped(False)); expect(or).toDeferEvaluationOf(True, expect.skipped(False));
expect(toBoolean(or(True).then(e => e(False)))).toBe(true); expect($(or._(True)._(False))).toBe(true);
}); });
it('True or True is True', () => { it('True or True is True', () => {
expect(or).toDeferEvaluationOf(True, expect.skipped(True)); expect(or).toDeferEvaluationOf(True, expect.skipped(True));
expect(toBoolean(or(True).then(e => e(True)))).toBe(true); expect($(or._(True)._(True))).toBe(true);
}); });
}); });
describe('xor', () => { describe('xor', () => {
it('False xor False is False', () => { it('False xor False is False', () => {
expect(xor).toDeferEvaluationOf(False, False); expect(xor).toDeferEvaluationOf(False, False);
expect(toBoolean(xor(False).then(e => e(False)))).toBe(false); expect($(xor._(False)._(False))).toBe(false);
}); });
it('False xor True is True', () => { it('False xor True is True', () => {
expect(xor).toDeferEvaluationOf(False, True); expect(xor).toDeferEvaluationOf(False, True);
expect(toBoolean(xor(False).then(e => e(True)))).toBe(true); expect($(xor._(False)._(True))).toBe(true);
}); });
it('True xor False is True', () => { it('True xor False is True', () => {
expect(xor).toDeferEvaluationOf(True, False); expect(xor).toDeferEvaluationOf(True, False);
expect(toBoolean(xor(True).then(e => e(False)))).toBe(true); expect($(xor._(True)._(False))).toBe(true);
}); });
it('True xor True is False', () => { it('True xor True is False', () => {
expect(xor).toDeferEvaluationOf(True, True); expect(xor).toDeferEvaluationOf(True, True);
expect(toBoolean(xor(True).then(e => e(True)))).toBe(false); expect($(xor._(True)._(True))).toBe(false);
}); });
}); });
}); });

@ -1,16 +1,16 @@
import { describe, expect, it } from '@jest/globals'; import { describe, expect, it } from '@jest/globals';
import { _, id, cnst, Q } from '../src/core'; import { _, cnst, fromNative, id, toNative } from '../src/core';
describe('id', () => { describe('id', () => {
it('returns first argument', () => { it('returns first argument', () => {
expect(id).toDeferEvaluationOf(null); expect(id).toDeferEvaluationOf('first');
expect(id(_(null as unknown as Q))).toEvaluateTo(null); expect(toNative(id._(fromNative('first')))).toBe('first');
}); });
}); });
describe('cnst', () => { describe('cnst', () => {
it('returns first argument after receiving second', () => { it('returns first argument after receiving second', () => {
// expect(cnst).toDeferEvaluationOf(null, expect.skipped(null)); expect(cnst).toDeferEvaluationOf('first', expect.skipped(fromNative('second')));
expect(cnst(_(null as unknown as Q)).then(e => e(_('whatever' as unknown as Q)))).toEvaluateTo(null); expect(toNative(cnst._(fromNative('first'))._(fromNative('second')))).toBe('first');
}); });
}); });

@ -1,209 +1,208 @@
import { describe, expect, it } from '@jest/globals'; import { describe, expect, it } from '@jest/globals';
import { $, _, Lazy, Q } from '../src/core'; import { L, toNative } from '../src/core';
import { toNumber, Zero, succ, Num, Succ, odd, sum } from '../src/num'; import { toNumber as $n, Num, Succ, Zero, fromNumber as _n, odd, succ, sum } from '../src/num';
import { Cons, filter, foldl, foldlz, foldr, foldrz, fromArray, fromNumberArray, head, List, map, Nil, tail, toArray, toNumberArray } from '../src/list'; import { toArray as $, toNumberArray as $na, Cons, List, Nil, fromArray as _, fromNumberArray as _na, filter, foldl, foldlz, foldr, foldrz, head, map, tail } from '../src/list';
describe('List', () => { describe('List', () => {
// const iterate: (n: number) => List<Lazy<number>> = n => new Lazy(() => Cons(_(n)).then(e => e(iterate(n + 1))).value); const iterate: (n: Num) => List<Num> = n => new L(() => Cons._(n)._(iterate(Succ._(n))).value);
const niterate: (n: Num) => List<Num> = n => new Lazy(() => Cons(n).then(e => e(niterate(Succ(n)))).value);
describe('toArray', () => { describe('toArray', () => {
it('converts []', () => { it('converts []', () => {
expect(toArray(Nil)).toEqual([]); expect($(Nil)).toEqual([]);
}); });
it('converts [0]', () => { it('converts [0]', () => {
expect(toArray(Cons(_(0 as unknown as Q)).then(e => e(Nil)))).toEqual([0]); expect($(Cons._(_n(0))._(Nil)).map($n)).toEqual([0]);
}); });
it('converts [0, 1]', () => { it('converts [0, 1]', () => {
expect(toArray(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil)))))).toEqual([0, 1]); expect($(Cons._(_n(0))._(Cons._(_n(1))._(Nil))).map($n)).toEqual([0, 1]);
}); });
}); });
describe('fromArray', () => { describe('fromArray', () => {
it('converts []', () => { it('converts []', () => {
expect(toArray(fromArray([]))).toEqual([]); expect($(_([]))).toEqual([]);
}); });
it('converts [0]', () => { it('converts [0]', () => {
expect(toArray(fromArray([_(0 as unknown as Q)]))).toEqual([0]); expect($(_([0].map(_n))).map($n)).toEqual([0]);
}); });
it('converts 2', () => { it('converts 2', () => {
expect(toArray(fromArray([_(0 as unknown as Q), _(1 as unknown as Q)]))).toEqual([0, 1]); expect($(_([0, 1].map(_n))).map($n)).toEqual([0, 1]);
}); });
}); });
describe('head', () => { describe('head', () => {
it('returns nothing from empty list', () => { it('returns nothing from empty list', () => {
expect(() => head(Nil)).not.toThrow(); expect(() => head._(Nil)).not.toThrow();
expect(() => $(head(Nil))).toThrow(); expect(() => toNative(head._(Nil))).toThrow();
}); });
it('returns only item', () => { it('returns only item', () => {
expect(head).toDeferEvaluationOf(Cons(_(0 as unknown as Q)).then(e => e(Nil))); expect(head).toDeferEvaluationOf(_na([0]));
expect(head(Cons(_(0 as unknown as Q)).then(e => e(Nil)))).toEvaluateTo(0); expect($n(head._(_na([0])))).toBe(0);
}); });
it('returns first item', () => { it('returns first item', () => {
expect(head).toDeferEvaluationOf(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil))))); expect(head).toDeferEvaluationOf(_na([0, 1]));
expect(head(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil)))))).toEvaluateTo(0); expect($n(head._(_na([0, 1])))).toBe(0);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(toNumber(head(niterate(Zero)))).toBe(0); expect($n(head._(iterate(Zero)))).toBe(0);
}); });
}); });
describe('tail', () => { describe('tail', () => {
it('drops nothing from empty list', () => { it('drops nothing from empty list', () => {
expect(() => tail(Nil)).not.toThrow(); expect(() => tail._(Nil)).not.toThrow();
expect(() => toArray(tail(Nil))).toThrow(); expect(() => $na(tail._(Nil))).toThrow();
}); });
it('drops only item', () => { it('drops only item', () => {
expect(tail).toDeferEvaluationOf(Cons(_(0 as unknown as Q)).then(e => e(Nil))); expect(tail).toDeferEvaluationOf(_na([0]));
expect(toArray(tail(Cons(_(0 as unknown as Q)).then(e => e(Nil))))).toEqual([]); expect($na(tail._(_na([0])))).toEqual([]);
}); });
it('drops first item', () => { it('drops first item', () => {
expect(tail).toDeferEvaluationOf(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil))))); expect(tail).toDeferEvaluationOf(_na([0, 1]));
expect(toArray(tail(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil))))))).toEqual([1]); expect($na(tail._(_na([0, 1])))).toEqual([1]);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(toNumber(head(tail(niterate(Zero))))).toBe(1); expect($n(head._(tail._(iterate(Zero))))).toBe(1);
}); });
}); });
describe('foldl', () => { describe('foldl', () => {
it('folds empty list', () => { it('folds empty list', () => {
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([])); expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([]));
expect(toNumber(foldl(sum).then(e => e(Zero).then(e => e(fromNumberArray([])))))).toBe(0); expect($n(foldl._(sum)._(Zero)._(_na([])))).toBe(0);
}); });
it('folds single item', () => { it('folds single item', () => {
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1])); expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([1]));
expect(toNumber(foldl(sum).then(e => e(Zero).then(e => e(fromNumberArray([1])))))).toBe(1); expect($n(foldl._(sum)._(Zero)._(_na([1])))).toBe(1);
}); });
it('folds several items', () => { it('folds several items', () => {
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2])); expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([1, 2]));
expect(toNumber(foldl(sum).then(e => e(Zero).then(e => e(fromNumberArray([1, 2])))))).toBe(3); expect($n(foldl._(sum)._(Zero)._(_na([1, 2])))).toBe(3);
}); });
}); });
describe('foldlz', () => { describe('foldlz', () => {
it('does not fold empty list', () => { it('does not fold empty list', () => {
expect(() => foldlz(sum).then(e => e(Nil))).not.toThrow(); expect(() => foldlz._(sum)._(Nil)).not.toThrow();
expect(() => toNumber(foldlz(sum).then(e => e(Nil)))).toThrow(); expect(() => $n(foldlz._(sum)._(Nil))).toThrow();
}); });
it('folds single item', () => { it('folds single item', () => {
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1])); expect(foldlz._(sum)).toDeferEvaluationOf(_na([1]));
expect(toNumber(foldlz(sum).then(e => e(fromNumberArray([1]))))).toBe(1); expect($n(foldlz._(sum)._(_na([1])))).toBe(1);
}); });
it('folds several items', () => { it('folds several items', () => {
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2])); expect(foldlz._(sum)).toDeferEvaluationOf(_na([1, 2]));
expect(toNumber(foldlz(sum).then(e => e(fromNumberArray([1, 2]))))).toBe(3); expect($n(foldlz._(sum)._(_na([1, 2])))).toBe(3);
}); });
}); });
describe('foldr', () => { describe('foldr', () => {
it('folds empty list', () => { it('folds empty list', () => {
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([])); expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([]));
expect(toNumber(foldr(sum).then(e => e(Zero).then(e => e(fromNumberArray([])))))).toBe(0); expect($n(foldr._(sum)._(Zero)._(_na([])))).toBe(0);
}); });
it('folds single item', () => { it('folds single item', () => {
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1])); expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([1]));
expect(toNumber(foldr(sum).then(e => e(Zero).then(e => e(fromNumberArray([1])))))).toBe(1); expect($n(foldr._(sum)._(Zero)._(_na([1])))).toBe(1);
}); });
it('folds several items', () => { it('folds several items', () => {
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2])); expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([1, 2]));
expect(toNumber(foldr(sum).then(e => e(Zero).then(e => e(fromNumberArray([1, 2])))))).toBe(3); expect($n(foldr._(sum)._(Zero)._(_na([1, 2])))).toBe(3);
}); });
// it('handles infinite lists', () => { // it('handles infinite lists', () => {
// expect(foldr<Lazy<number>, Lazy<number>>(x => _(a => // expect(foldr<Lazy<number>, Lazy<number>>(x => _(a =>
// iif<Lazy<number>> // iif<Lazy<number>>
// (x.then(x => fromBoolean(x === 0))) // (x.call(x => fromBoolean(x === 0)))
// .then(e => e // .call(
// (x).then(e => e // (x).call(
// (x.then(x => a.then(a => _(x + a))))))) // (x.call(x => a.call(a => _(x + a)))))))
// ).then(e => e(_(0 as unknown as Q)).then(e => e(iterate(-5))))).toEvaluateTo(-15); // ).call((fromNumber(0)).call((iterate(-5))))).toEvaluateTo(-15);
// }); // });
}); });
describe('foldrz', () => { describe('foldrz', () => {
it('does not fold empty list', () => { it('does not fold empty list', () => {
expect(() => foldrz(sum).then(e => e(Nil))).not.toThrow(); expect(() => foldrz._(sum)._(Nil)).not.toThrow();
expect(() => toNumber(foldrz(sum).then(e => e(Nil)))).toThrow(); expect(() => $n(foldrz._(sum)._(Nil))).toThrow();
}); });
it.failing('folds single item', () => { it.failing('folds single item', () => {
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1])); expect(foldrz._(sum)).toDeferEvaluationOf(_na([1]));
expect(toNumber(foldrz(sum).then(e => e(fromNumberArray([1]))))).toBe(1); expect($n(foldrz._(sum)._(_na([1])))).toBe(1);
}); });
it.failing('folds several items', () => { it.failing('folds several items', () => {
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2])); expect(foldrz._(sum)).toDeferEvaluationOf(_na([1, 2]));
expect(toNumber(foldrz(sum).then(e => e(fromNumberArray([1, 2]))))).toBe(3); expect($n(foldrz._(sum)._(_na([1, 2])))).toBe(3);
}); });
// it.failing('handles infinite lists', () => { // it.failing('handles infinite lists', () => {
// expect(foldrz<Lazy<number>>(x => _(a => // expect(foldrz<Lazy<number>>(x => _(a =>
// iif<Lazy<number>> // iif<Lazy<number>>
// (x.then(x => fromBoolean(x === 0))) // (x.call(x => fromBoolean(x === 0)))
// .then(e => e // .call(
// (x).then(e => e // (x).call(
// (x.then(x => a.then(a => _(x + a))))))) // (x.call(x => a.call(a => _(x + a)))))))
// ).then(e => e(iterate(-5)))).toEvaluateTo(-10); // ).call((iterate(-5)))).toEvaluateTo(-10);
// }); // });
}); });
describe('map', () => { describe('map', () => {
it('maps empty list', () => { it('maps empty list', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([])); expect(map._(succ)).toDeferEvaluationOf(_na([]));
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([]))))).toEqual([]); expect($na(map._(succ)._(_na([])))).toEqual([]);
}); });
it('maps only item', () => { it('maps only item', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0])); expect(map._(succ)).toDeferEvaluationOf(_na([0]));
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([0]))))).toEqual([1]); expect($na(map._(succ)._(_na([0])))).toEqual([1]);
}); });
it('maps several items', () => { it('maps several items', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0, 1])); expect(map._(succ)).toDeferEvaluationOf(_na([0, 1]));
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([0, 1]))))).toEqual([1, 2]); expect($na(map._(succ)._(_na([0, 1])))).toEqual([1, 2]);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(toNumber(head(map(succ).then(e => e(niterate(Zero)))))).toBe(1); expect($n(head._(map._(succ)._(iterate(Zero))))).toBe(1);
}); });
}); });
describe('filter', () => { describe('filter', () => {
it('filter empty list', () => { it('filter empty list', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([])); expect(filter._(odd)).toDeferEvaluationOf(_na([]));
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([]))))).toEqual([]); expect($na(filter._(odd)._(_na([])))).toEqual([]);
}); });
it('filters only item', () => { it('filters only item', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0])); expect(filter._(odd)).toDeferEvaluationOf(_na([0]));
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([]))))).toEqual([]); expect($na(filter._(odd)._(_na([])))).toEqual([]);
}); });
it('filters several items', () => { it('filters several items', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1])); expect(filter._(odd)).toDeferEvaluationOf(_na([0, 1]));
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([0, 1]))))).toEqual([1]); expect($na(filter._(odd)._(_na([0, 1])))).toEqual([1]);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(toNumber(head(filter(odd).then(e => e(niterate(Zero)))))).toBe(1); expect($n(head._(filter._(odd)._(iterate(Zero))))).toBe(1);
}); });
}); });
}); });

@ -1,166 +1,165 @@
import { describe, expect, it } from '@jest/globals'; import { describe, expect, it } from '@jest/globals';
import { _, Q } from '../src/core'; import { toNumber as $, Num, Succ, Zero, fromNumber as _, ifz, mul, pred, sub, succ, sum } from '../src/num';
import { Num, Zero, Succ, sum, succ, pred, ifz, fromNumber, toNumber, sub, mul } from '../src/num';
describe('Num', () => { describe('Num', () => {
describe('toNumber', () => { describe('toNumber', () => {
it('converts 0', () => { it('converts 0', () => {
expect(toNumber(Zero)).toBe(0); expect($(Zero)).toBe(0);
}); });
it('converts 1', () => { it('converts 1', () => {
expect(toNumber(Succ(Zero))).toBe(1); expect($(Succ._(Zero))).toBe(1);
}); });
it('converts 2', () => { it('converts 2', () => {
expect(toNumber(Succ(Succ(Zero)))).toBe(2); expect($(Succ._(Succ._(Zero)))).toBe(2);
}); });
}); });
describe('fromNumber', () => { describe('fromNumber', () => {
it('converts 0', () => { it('converts 0', () => {
expect(toNumber(fromNumber(0))).toBe(0); expect($(_(0))).toBe(0);
}); });
it('converts 1', () => { it('converts 1', () => {
expect(toNumber(fromNumber(1))).toBe(1); expect($(_(1))).toBe(1);
}); });
it('converts 2', () => { it('converts 2', () => {
expect(toNumber(fromNumber(2))).toBe(2); expect($(_(2))).toBe(2);
}); });
}); });
describe('ifz', () => { describe('ifz', () => {
it('returns zero branch', () => { it('returns zero branch', () => {
expect(ifz).toDeferEvaluationOf(fromNumber(0), true, false); expect(ifz).toDeferEvaluationOf(_(0), true, false);
expect(ifz(fromNumber(0)).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(true); expect($(ifz._(_(0))._(_(0))._(_(1)))).toBe(0);
}); });
it('returns non-zero branch', () => { it('returns non-zero branch', () => {
expect(ifz).toDeferEvaluationOf(fromNumber(1), true, false); expect(ifz).toDeferEvaluationOf(_(1), true, false);
expect(ifz(fromNumber(1)).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(false); expect($(ifz._(_(1))._(_(0))._(_(1)))).toBe(1);
}); });
it('returns non-zero branch', () => { it('returns non-zero branch', () => {
expect(ifz).toDeferEvaluationOf(fromNumber(100), true, false); expect(ifz).toDeferEvaluationOf(_(100), true, false);
expect(ifz(fromNumber(10)).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(false); expect($(ifz._(_(10))._(_(0))._(_(1)))).toBe(1);
}); });
it('deeply lazy', () => { it('deeply lazy', () => {
const alot: Num = fromNumber(1000000); const alot: Num = _(1000000);
expect(ifz(Succ(alot)).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(false); expect($(ifz._(Succ._(alot))._(_(0))._(_(1)))).toBe(1);
expect(alot.evaluated).toBe(false); expect(alot.evaluated).toBe(false);
}); });
}); });
describe('succ', () => { describe('succ', () => {
it('succ 0 is 1', () => { it('succ 0 is 1', () => {
expect(succ).toDeferEvaluationOf(fromNumber(0)); expect(succ).toDeferEvaluationOf(_(0));
expect(toNumber(succ(fromNumber(0)))).toBe(1); expect($(succ._(_(0)))).toBe(1);
}); });
it('succ 1 is 2', () => { it('succ 1 is 2', () => {
expect(succ).toDeferEvaluationOf(fromNumber(0)); expect(succ).toDeferEvaluationOf(_(0));
expect(toNumber(succ(fromNumber(1)))).toBe(2); expect($(succ._(_(1)))).toBe(2);
}); });
it('succ 10 is 11', () => { it('succ 10 is 11', () => {
expect(succ).toDeferEvaluationOf(fromNumber(10)); expect(succ).toDeferEvaluationOf(_(10));
expect(toNumber(succ(fromNumber(10)))).toBe(11); expect($(succ._(_(10)))).toBe(11);
}); });
}); });
describe('pred', () => { describe('pred', () => {
it('pred 0 is 0', () => { it('pred 0 is 0', () => {
expect(pred).toDeferEvaluationOf(fromNumber(0)); expect(pred).toDeferEvaluationOf(_(0));
expect(toNumber(pred(fromNumber(0)))).toBe(0); expect($(pred._(_(0)))).toBe(0);
}); });
it('pred 1 is 0', () => { it('pred 1 is 0', () => {
expect(pred).toDeferEvaluationOf(fromNumber(0)); expect(pred).toDeferEvaluationOf(_(0));
expect(toNumber(pred(fromNumber(1)))).toBe(0); expect($(pred._(_(1)))).toBe(0);
}); });
it('pred 10 is 9', () => { it('pred 10 is 9', () => {
expect(pred).toDeferEvaluationOf(fromNumber(10)); expect(pred).toDeferEvaluationOf(_(10));
expect(toNumber(pred(fromNumber(10)))).toBe(9); expect($(pred._(_(10)))).toBe(9);
}); });
}); });
describe('sum', () => { describe('sum', () => {
it('0 sum 0 is 0', () => { it('0 sum 0 is 0', () => {
expect(sum).toDeferEvaluationOf(fromNumber(0), fromNumber(0)); expect(sum).toDeferEvaluationOf(_(0), _(0));
expect(toNumber(sum(fromNumber(0)).then(e => e(fromNumber(0))))).toBe(0); expect($(sum._(_(0))._(_(0)))).toBe(0);
}); });
it('0 sum 1 is 1', () => { it('0 sum 1 is 1', () => {
expect(sum).toDeferEvaluationOf(fromNumber(0), fromNumber(1)); expect(sum).toDeferEvaluationOf(_(0), _(1));
expect(toNumber(sum(fromNumber(0)).then(e => e(fromNumber(1))))).toBe(1); expect($(sum._(_(0))._(_(1)))).toBe(1);
}); });
it('1 sum 0 is 1', () => { it('1 sum 0 is 1', () => {
expect(sum).toDeferEvaluationOf(fromNumber(1), fromNumber(0)); expect(sum).toDeferEvaluationOf(_(1), _(0));
expect(toNumber(sum(fromNumber(1)).then(e => e(fromNumber(0))))).toBe(1); expect($(sum._(_(1))._(_(0)))).toBe(1);
}); });
it('3 sum 4 is 7', () => { it('3 sum 4 is 7', () => {
expect(sum).toDeferEvaluationOf(fromNumber(3), fromNumber(4)); expect(sum).toDeferEvaluationOf(_(3), _(4));
expect(toNumber(sum(fromNumber(3)).then(e => e(fromNumber(4))))).toBe(7); expect($(sum._(_(3))._(_(4)))).toBe(7);
}); });
}); });
describe('sub', () => { describe('sub', () => {
it('0 sub 0 is 0', () => { it('0 sub 0 is 0', () => {
expect(sub).toDeferEvaluationOf(fromNumber(0), fromNumber(0)); expect(sub).toDeferEvaluationOf(_(0), _(0));
expect(toNumber(sub(fromNumber(0)).then(e => e(fromNumber(0))))).toBe(0); expect($(sub._(_(0))._(_(0)))).toBe(0);
}); });
it('0 sub 1 is 0', () => { it('0 sub 1 is 0', () => {
expect(sub).toDeferEvaluationOf(fromNumber(0), fromNumber(1)); expect(sub).toDeferEvaluationOf(_(0), _(1));
expect(toNumber(sub(fromNumber(0)).then(e => e(fromNumber(1))))).toBe(0); expect($(sub._(_(0))._(_(1)))).toBe(0);
}); });
it('1 sub 0 is 1', () => { it('1 sub 0 is 1', () => {
expect(sub).toDeferEvaluationOf(fromNumber(1), fromNumber(0)); expect(sub).toDeferEvaluationOf(_(1), _(0));
expect(toNumber(sub(fromNumber(1)).then(e => e(fromNumber(0))))).toBe(1); expect($(sub._(_(1))._(_(0)))).toBe(1);
}); });
it('7 sub 4 is 3', () => { it('7 sub 4 is 3', () => {
expect(sub).toDeferEvaluationOf(fromNumber(7), fromNumber(4)); expect(sub).toDeferEvaluationOf(_(7), _(4));
expect(toNumber(sub(fromNumber(7)).then(e => e(fromNumber(4))))).toBe(3); expect($(sub._(_(7))._(_(4)))).toBe(3);
}); });
}); });
describe('mul', () => { describe('mul', () => {
it('0 mul 0 is 0', () => { it('0 mul 0 is 0', () => {
expect(mul).toDeferEvaluationOf(fromNumber(0), fromNumber(0)); expect(mul).toDeferEvaluationOf(_(0), _(0));
expect(toNumber(mul(fromNumber(0)).then(e => e(fromNumber(0))))).toBe(0); expect($(mul._(_(0))._(_(0)))).toBe(0);
}); });
it('0 mul 10 is 0', () => { it('0 mul 10 is 0', () => {
expect(mul).toDeferEvaluationOf(fromNumber(0), expect.skipped(fromNumber(10))); expect(mul).toDeferEvaluationOf(_(0), expect.skipped(_(10)));
expect(toNumber(mul(fromNumber(0)).then(e => e(fromNumber(10))))).toBe(0); expect($(mul._(_(0))._(_(10)))).toBe(0);
}); });
it('10 mul 0 is 0', () => { it('10 mul 0 is 0', () => {
expect(mul).toDeferEvaluationOf(fromNumber(10), fromNumber(0)); expect(mul).toDeferEvaluationOf(_(10), _(0));
expect(toNumber(mul(fromNumber(1)).then(e => e(fromNumber(0))))).toBe(0); expect($(mul._(_(1))._(_(0)))).toBe(0);
}); });
it('1 mul 10 is 10', () => { it('1 mul 10 is 10', () => {
expect(mul).toDeferEvaluationOf(fromNumber(1), fromNumber(10)); expect(mul).toDeferEvaluationOf(_(1), _(10));
expect(toNumber(mul(fromNumber(1)).then(e => e(fromNumber(10))))).toBe(10); expect($(mul._(_(1))._(_(10)))).toBe(10);
}); });
it('10 mul 1 is 10', () => { it('10 mul 1 is 10', () => {
expect(mul).toDeferEvaluationOf(fromNumber(10), fromNumber(1)); expect(mul).toDeferEvaluationOf(_(10), _(1));
expect(toNumber(mul(fromNumber(10)).then(e => e(fromNumber(1))))).toBe(10); expect($(mul._(_(10))._(_(1)))).toBe(10);
}); });
it('3 mul 4 is 12', () => { it('3 mul 4 is 12', () => {
expect(mul).toDeferEvaluationOf(fromNumber(3), fromNumber(4)); expect(mul).toDeferEvaluationOf(_(3), _(4));
expect(toNumber(mul(fromNumber(3)).then(e => e(fromNumber(4))))).toBe(12); expect($(mul._(_(3))._(_(4)))).toBe(12);
}); });
}); });
}); });

@ -1,8 +1,8 @@
import { expect } from '@jest/globals'; import { expect } from '@jest/globals';
import type { MatcherFunction } from 'expect'; import type { MatcherFunction } from 'expect';
import { $, _, Lazy, V } from '../src/core'; import { L, P, _, fromNative } from '../src/core';
class Deferrable<T extends V> { class Deferrable<T extends P> {
constructor( constructor(
public readonly when: 'always' | 'deferred' | 'never', public readonly when: 'always' | 'deferred' | 'never',
public readonly what: T) { } public readonly what: T) { }
@ -10,25 +10,25 @@ class Deferrable<T extends V> {
const toEvaluateTo: MatcherFunction<[expected: unknown]> = const toEvaluateTo: MatcherFunction<[expected: unknown]> =
function (actual, expected) { function (actual, expected) {
if (!(actual instanceof Lazy)) { if (!(actual instanceof L)) {
throw new TypeError('Actual must be of type Lazy!'); throw new TypeError('Actual must be of type Lazy!');
} }
if (this.equals($(actual), expected)) { if (this.equals(fromNative(actual), expected)) {
return { return {
message: () => `expected ${this.utils.printReceived($(actual))} not to evaluate to ${this.utils.printExpected(expected)}`, message: () => `expected ${this.utils.printReceived(fromNative(actual))} not to evaluate to ${this.utils.printExpected(expected)}`,
pass: true, pass: true,
}; };
} else { } else {
return { return {
message: () => `expected ${this.utils.printReceived($(actual))} to evaluate to ${this.utils.printExpected(expected)}`, message: () => `expected ${this.utils.printReceived(fromNative(actual))} to evaluate to ${this.utils.printExpected(expected)}`,
pass: false, pass: false,
}; };
} }
}; };
const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<V>)[]> = const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<P>)[]> =
function (actual, ...args) { function (actual, ...args) {
while (actual instanceof Lazy) { while (actual instanceof L) {
actual = actual.value; actual = actual.value;
} }
if (typeof actual !== 'function') { if (typeof actual !== 'function') {
@ -37,16 +37,16 @@ const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<V>)[]> =
if (this.isNot) { if (this.isNot) {
throw new TypeError('`toDeferEvaluationOf` can not be `not`ted, use `always`/`deferred`/`never` on arguments!'); throw new TypeError('`toDeferEvaluationOf` can not be `not`ted, use `always`/`deferred`/`never` on arguments!');
} }
const largs: Deferrable<V>[] = args.map(v => const largs: Deferrable<P>[] = args.map(v =>
v instanceof Deferrable ? v v instanceof Deferrable ? v
: v instanceof Lazy ? new Deferrable('deferred', _(v.value)) : v instanceof L ? new Deferrable('deferred', _(v.value))
: new Deferrable('deferred', _(v as any))); : new Deferrable('deferred', _(v as any)));
const cargs: Deferrable<V>[] = []; const cargs: Deferrable<P>[] = [];
let result = actual; let result = actual;
for (const la of largs) { for (const la of largs) {
result = result instanceof Lazy ? result.then(e => e(la.what)) : result(la.what); result = result instanceof L ? result._(la.what) : result(la.what);
cargs.push(la); cargs.push(la);
const mismatchingArg: Deferrable<V> | undefined = cargs.find(v => v.when === 'always' ? !v.what.evaluated : v.what.evaluated); const mismatchingArg: Deferrable<P> | undefined = cargs.find(v => v.when === 'always' ? !v.what.evaluated : v.what.evaluated);
if (mismatchingArg) { if (mismatchingArg) {
return { return {
message: () => message: () =>
@ -57,11 +57,11 @@ const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<V>)[]> =
}; };
} }
} }
if (!(result instanceof Lazy)) { if (!(result instanceof L)) {
throw new TypeError('Actual did not resolve to value, more arguments expected.'); throw new TypeError('Actual did not resolve to value, more arguments expected.');
} }
result.value; (() => result.value)();
const mismatchingArg: Deferrable<V> | undefined = cargs.find(v => v.when === 'never' && v.what.evaluated); const mismatchingArg: Deferrable<P> | undefined = cargs.find(v => v.when === 'never' && v.what.evaluated);
if (mismatchingArg) { if (mismatchingArg) {
return { return {
message: () => `expected ${this.utils.printExpected(actual.name)} not to evaluate ${this.utils.printReceived(`${largs.indexOf(mismatchingArg) + 1}th argument`)}`, message: () => `expected ${this.utils.printExpected(actual.name)} not to evaluate ${this.utils.printReceived(`${largs.indexOf(mismatchingArg) + 1}th argument`)}`,
@ -86,9 +86,9 @@ expect.skipped = (v) => new Deferrable('never', _(v.value) as any);
declare module 'expect' { declare module 'expect' {
interface AsymmetricMatchers { interface AsymmetricMatchers {
evaluated<T extends V>(value: T): Deferrable<T>; evaluated<T extends P>(value: T): Deferrable<T>;
deferred<T extends V>(value: T): Deferrable<T>; deferred<T extends P>(value: T): Deferrable<T>;
skipped<T extends V>(value: T): Deferrable<T>; skipped<T extends P>(value: T): Deferrable<T>;
toEvaluateTo(value: any): void; toEvaluateTo(value: any): void;
toDeferEvaluationOf(...args: any[]): void; toDeferEvaluationOf(...args: any[]): void;
} }

Loading…
Cancel
Save