No explicit Lazy

master
Freywar Ulvnaudgari 2 years ago
parent 1c70da5f3b
commit 531c52212d
  1. 26
      src/bool.ts
  2. 23
      src/core.ts
  3. 46
      src/list.ts
  4. 32
      src/num.ts
  5. 38
      test/bool.spec.ts
  6. 73
      test/list.spec.ts
  7. 59
      test/num.spec.ts

@ -1,30 +1,30 @@
import { Lazy, _ } from './core'; import { Lazy, V, _ } from './core';
export type Bool = <T>(t: Lazy<T>) => (f: Lazy<T>) => Lazy<T>; export type Bool = Lazy<<T extends V>(t: T) => (f: T) => T>;
export const True: Lazy<Bool> export const True: Bool
= _(t => _ => t) as Lazy<Bool>; = _(t => _ => t) as Bool;
export const False: Lazy<Bool> export const False: Bool
= _(_ => f => f); = _(_ => f => f);
export const iif: <T>(b: Lazy<Bool>) => (t: Lazy<T>) => (f: Lazy<T>) => Lazy<T> export const iif: <T extends V>(b: Bool) => (t: T) => (f: T) => T
= b => t => f => b.then(b => b(t)(f)); = b => t => f => b.then(b => b(t)(f));
export const not: (b: Lazy<Bool>) => Lazy<Bool> export const not: (b: Bool) => Bool
= b => b.then(b => b(False)(True)); = b => b.then(b => b(False)(True));
export const and: (l: Lazy<Bool>) => (r: Lazy<Bool>) => Lazy<Bool> export const and: (l: Bool) => (r: Bool) => Bool
= l => r => l.then(l => l(r)(False)); = l => r => l.then(l => l(r)(False));
export const or: (l: Lazy<Bool>) => (r: Lazy<Bool>) => Lazy<Bool> export const or: (l: Bool) => (r: Bool) => Bool
= l => r => l.then(l => l(True)(r)); = l => r => l.then(l => l(True)(r));
export const xor: (l: Lazy<Bool>) => (r: Lazy<Bool>) => Lazy<Bool> export const xor: (l: Bool) => (r: Bool) => Bool
= l => r => l.then(l => l(not(r))(r)); = l => r => l.then(l => l(not(r))(r));
export const fromBoolean: (b: boolean) => Lazy<Bool> export const fromBoolean: (b: boolean) => Bool
= b => b ? True : False; = b => b ? True : False;
export const toBoolean: (b: Lazy<Bool>) => Lazy<boolean> export const toBoolean: (b: Bool) => boolean
= b => b.then(b => b(_(true))(_(false))); = b => b.then(b => b(_(true))(_(false))).value;

@ -13,32 +13,21 @@ export class Lazy<T> {
return this._value = this._eval(); return this._value = this._eval();
} }
public then<R>(f: (v: T) => Lazy<R>): Lazy<R> { public then<R extends V>(f: (v: T) => R): R {
return new Lazy(() => f(this.value).value); return new Lazy(() => f(this.value).value) as R;
}
}
type Choice = <T>(t: Lazy<T>) => (f: Lazy<T>) => Lazy<T>;
export class LazyChoice extends Lazy<Choice> {
public call<T>(l: LazyChoice): (r: LazyChoice) => LazyChoice;
public call<T>(l: Lazy<T>): (r: Lazy<T>) => Lazy<T>;
public call<T>(l: Lazy<T> | LazyChoice): (r: Lazy<T> | LazyChoice) => Lazy<T> | LazyChoice {
return l instanceof LazyChoice
? (r: LazyChoice) => new LazyChoice(() => this.value(l)(r).value)
: (r: Lazy<T>) => this.then(c => c(l)(r));
} }
} }
export type V = Lazy<unknown>;
export const _ = <T>(v: T): Lazy<T> => new Lazy(() => v); export const _ = <T>(v: T): Lazy<T> => new Lazy(() => v);
export const $ = <T>(v: Lazy<T>): T => v.value; export const $ = <T>(v: Lazy<T>): T => v.value;
export const undef: Lazy<any> = new Lazy(() => { export const undef: any = new Lazy(() => {
throw new Error('undefined'); throw new Error('undefined');
}); });
export const id: <T>(v: Lazy<T>) => Lazy<T> export const id: <T extends V>(v: T) => T
= v => v; = v => v;
export const cnst: <T> (v: Lazy<T>) => (_: Lazy<unknown>) => Lazy<T> export const cnst: <T extends V> (v: T) => (_: unknown) => T
= v => _ => v; = v => _ => v;

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

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

@ -5,23 +5,21 @@ import { True, False, iif, and, not, or, xor, toBoolean, fromBoolean } from '../
describe('Bool', () => { describe('Bool', () => {
describe('toBoolean', () => { describe('toBoolean', () => {
it('converts True', () => { it('converts True', () => {
expect(toBoolean).toDeferEvaluationOf(True); expect(toBoolean(True)).toBe(true);
expect(toBoolean(True)).toEvaluateTo(true);
}); });
it('converts False', () => { it('converts False', () => {
expect(toBoolean).toDeferEvaluationOf(False); expect(toBoolean(False)).toBe(false);
expect(toBoolean(False)).toEvaluateTo(false);
}); });
}); });
describe('fromBoolean', () => { describe('fromBoolean', () => {
it('converts true', () => { it('converts true', () => {
expect(toBoolean(fromBoolean(true))).toEvaluateTo(true); expect(toBoolean(fromBoolean(true))).toBe(true);
}); });
it('converts false', () => { it('converts false', () => {
expect(toBoolean(fromBoolean(false))).toEvaluateTo(false); expect(toBoolean(fromBoolean(false))).toBe(false);
}); });
}); });
@ -40,79 +38,79 @@ describe('Bool', () => {
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))).toEvaluateTo(true); expect(toBoolean(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))).toEvaluateTo(false); expect(toBoolean(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)(False))).toEvaluateTo(false); expect(toBoolean(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)(True))).toEvaluateTo(false); expect(toBoolean(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)(False))).toEvaluateTo(false); expect(toBoolean(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)(True))).toEvaluateTo(true); expect(toBoolean(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)(False))).toEvaluateTo(false); expect(toBoolean(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)(True))).toEvaluateTo(true); expect(toBoolean(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)(False))).toEvaluateTo(true); expect(toBoolean(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)(True))).toEvaluateTo(true); expect(toBoolean(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)(False))).toEvaluateTo(false); expect(toBoolean(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)(True))).toEvaluateTo(true); expect(toBoolean(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)(False))).toEvaluateTo(true); expect(toBoolean(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)(True))).toEvaluateTo(false); expect(toBoolean(xor(True)(True))).toBe(false);
}); });
}); });
}); });

@ -5,37 +5,34 @@ import { toNumber, Zero, sum, succ, Num, Succ, odd } from '../src/num';
import { Cons, filter, foldl, foldlz, foldr, foldrz, fromArray, fromNumberArray, head, List, map, Nil, tail, toArray, toNumberArray } from '../src/list'; import { Cons, filter, foldl, foldlz, foldr, foldrz, fromArray, fromNumberArray, head, List, map, Nil, tail, toArray, toNumberArray } from '../src/list';
describe('List', () => { describe('List', () => {
const iterate: (n: number) => Lazy<List<number>> = n => new Lazy(() => Cons(_(n))(iterate(n + 1)).value); const iterate: (n: number) => List<Lazy<number>> = n => new Lazy(() => Cons(_(n))(iterate(n + 1)).value);
const niterate: (n: Lazy<Num>) => Lazy<List<Num>> = n => new Lazy(() => Cons(n)(niterate(Succ(n))).value); const niterate: (n: Num) => List<Num> = n => new Lazy(() => Cons(n)(niterate(Succ(n))).value);
describe('toArray', () => { describe('toArray', () => {
it('converts []', () => { it('converts []', () => {
expect(toArray).toDeferEvaluationOf(Nil); expect(toArray(Nil)).toEqual([]);
expect(toArray(Nil)).toEvaluateTo([]);
}); });
it('converts [0]', () => { it('converts [0]', () => {
expect(toArray).toDeferEvaluationOf(Cons(_(0))(Nil)); expect(toArray(Cons(_(0))(Nil))).toEqual([0]);
expect(toArray(Cons(_(0))(Nil))).toEvaluateTo([0]);
}); });
it('converts [0, 1]', () => { it('converts [0, 1]', () => {
expect(toArray).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil))); expect(toArray(Cons(_(0))(Cons(_(1))(Nil)))).toEqual([0, 1]);
expect(toArray(Cons(_(0))(Cons(_(1))(Nil)))).toEvaluateTo([0, 1]);
}); });
}); });
describe('fromArray', () => { describe('fromArray', () => {
it('converts []', () => { it('converts []', () => {
expect(toArray(fromArray([]))).toEvaluateTo([]); expect(toArray(fromArray([]))).toEqual([]);
}); });
it('converts [0]', () => { it('converts [0]', () => {
expect(toArray(fromArray([0]))).toEvaluateTo([0]); expect(toArray(fromArray([0].map(_)))).toEqual([0]);
}); });
it('converts 2', () => { it('converts 2', () => {
expect(toArray(fromArray([0, 1]))).toEvaluateTo([0, 1]); expect(toArray(fromArray([0, 1].map(_)))).toEqual([0, 1]);
}); });
}); });
@ -63,17 +60,17 @@ describe('List', () => {
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(() => toArray(tail(Nil))).toThrow();
}); });
it('drops only item', () => { it('drops only item', () => {
expect(tail).toDeferEvaluationOf(Cons(_(0))(Nil)); expect(tail).toDeferEvaluationOf(Cons(_(0))(Nil));
expect(toArray(tail(Cons(_(0))(Nil)))).toEvaluateTo([]); expect(toArray(tail(Cons(_(0))(Nil)))).toEqual([]);
}); });
it('drops first item', () => { it('drops first item', () => {
expect(tail).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil))); expect(tail).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil)));
expect(toArray(tail(Cons(_(0))(Cons(_(1))(Nil))))).toEvaluateTo([1]); expect(toArray(tail(Cons(_(0))(Cons(_(1))(Nil))))).toEqual([1]);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
@ -84,56 +81,56 @@ describe('List', () => {
describe('foldl', () => { describe('foldl', () => {
it('folds empty list', () => { it('folds empty list', () => {
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([])); expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([]));
expect(toNumber(foldl(sum)(Zero)(fromNumberArray([])))).toEvaluateTo(0); expect(toNumber(foldl(sum)(Zero)(fromNumberArray([])))).toBe(0);
}); });
it('folds single item', () => { it('folds single item', () => {
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1])); expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1]));
expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1])))).toEvaluateTo(1); expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1])))).toBe(1);
}); });
it('folds several items', () => { it('folds several items', () => {
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2])); expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2]));
expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1, 2])))).toEvaluateTo(3); expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1, 2])))).toBe(3);
}); });
}); });
describe('foldlz', () => { describe('foldlz', () => {
it('does not fold empty list', () => { it('does not fold empty list', () => {
expect(() => foldlz(sum)(Nil)).not.toThrow(); expect(() => foldlz(sum)(Nil)).not.toThrow();
expect(() => $(toNumber(foldlz(sum)(Nil)))).toThrow(); expect(() => toNumber(foldlz(sum)(Nil))).toThrow();
}); });
it('folds single item', () => { it('folds single item', () => {
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1])); expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
expect(toNumber(foldlz(sum)(fromNumberArray([1])))).toEvaluateTo(1); expect(toNumber(foldlz(sum)(fromNumberArray([1])))).toBe(1);
}); });
it('folds several items', () => { it('folds several items', () => {
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2])); expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2]));
expect(toNumber(foldlz(sum)(fromNumberArray([1, 2])))).toEvaluateTo(3); expect(toNumber(foldlz(sum)(fromNumberArray([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, fromNumberArray([]));
expect(toNumber(foldr(sum)(Zero)(fromNumberArray([])))).toEvaluateTo(0); expect(toNumber(foldr(sum)(Zero)(fromNumberArray([])))).toBe(0);
}); });
it('folds single item', () => { it('folds single item', () => {
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1])); expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1]));
expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1])))).toEvaluateTo(1); expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1])))).toBe(1);
}); });
it('folds several items', () => { it('folds several items', () => {
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2])); expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2]));
expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1, 2])))).toEvaluateTo(3); expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1, 2])))).toBe(3);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(foldr<number, number>(x => a => expect(foldr<Lazy<number>, Lazy<number>>(x => a =>
iif<number> iif<Lazy<number>>
(x.then(x => fromBoolean(x === 0))) (x.then(x => fromBoolean(x === 0)))
(x) (x)
(x.then(x => a.then(a => _(x + a)))) (x.then(x => a.then(a => _(x + a))))
@ -144,22 +141,22 @@ describe('List', () => {
describe('foldrz', () => { describe('foldrz', () => {
it('does not fold empty list', () => { it('does not fold empty list', () => {
expect(() => foldrz(sum)(Nil)).not.toThrow(); expect(() => foldrz(sum)(Nil)).not.toThrow();
expect(() => $(toNumber(foldrz(sum)(Nil)))).toThrow(); expect(() => toNumber(foldrz(sum)(Nil))).toThrow();
}); });
it.failing('folds single item', () => { it.failing('folds single item', () => {
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1])); expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
expect(toNumber(foldrz(sum)(fromNumberArray([1])))).toEvaluateTo(1); expect(toNumber(foldrz(sum)(fromNumberArray([1])))).toBe(1);
}); });
it.failing('folds several items', () => { it.failing('folds several items', () => {
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2])); expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2]));
expect(toNumber(foldrz(sum)(fromNumberArray([1, 2])))).toEvaluateTo(3); expect(toNumber(foldrz(sum)(fromNumberArray([1, 2])))).toBe(3);
}); });
it.failing('handles infinite lists', () => { it.failing('handles infinite lists', () => {
expect(foldrz<number>(x => a => expect(foldrz<Lazy<number>>(x => a =>
iif<number> iif<Lazy<number>>
(x.then(x => fromBoolean(x === 0))) (x.then(x => fromBoolean(x === 0)))
(x) (x)
(x.then(x => a.then(a => _(x + a)))) (x.then(x => a.then(a => _(x + a))))
@ -170,42 +167,42 @@ describe('List', () => {
describe('map', () => { describe('map', () => {
it('maps empty list', () => { it('maps empty list', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([])); expect(map(succ)).toDeferEvaluationOf(fromNumberArray([]));
expect(toNumberArray(map(succ)(fromNumberArray([])))).toEvaluateTo([]); expect(toNumberArray(map(succ)(fromNumberArray([])))).toEqual([]);
}); });
it('maps only item', () => { it('maps only item', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0])); expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0]));
expect(toNumberArray(map(succ)(fromNumberArray([0])))).toEvaluateTo([1]); expect(toNumberArray(map(succ)(fromNumberArray([0])))).toEqual([1]);
}); });
it('maps several items', () => { it('maps several items', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0, 1])); expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0, 1]));
expect(toNumberArray(map(succ)(fromNumberArray([0, 1])))).toEvaluateTo([1, 2]); expect(toNumberArray(map(succ)(fromNumberArray([0, 1])))).toEqual([1, 2]);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(toNumber(head(map(succ)(niterate(Zero))))).toEvaluateTo(1); expect(toNumber(head(map(succ)(niterate(Zero))))).toBe(1);
}); });
}); });
describe('filter', () => { describe('filter', () => {
it('filter empty list', () => { it('filter empty list', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([])); expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([]));
expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEvaluateTo([]); expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEqual([]);
}); });
it('filters only item', () => { it('filters only item', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0])); expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0]));
expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEvaluateTo([]); expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEqual([]);
}); });
it('filters several items', () => { it('filters several items', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1])); expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1]));
expect(toNumberArray(filter(odd)(fromNumberArray([0, 1])))).toEvaluateTo([1]); expect(toNumberArray(filter(odd)(fromNumberArray([0, 1])))).toEqual([1]);
}); });
it('handles infinite lists', () => { it('handles infinite lists', () => {
expect(toNumber(head(filter(odd)(niterate(Zero))))).toEvaluateTo(1); expect(toNumber(head(filter(odd)(niterate(Zero))))).toBe(1);
}); });
}); });
}); });

@ -1,36 +1,33 @@
import { describe, expect, it } from '@jest/globals'; import { describe, expect, it } from '@jest/globals';
import { _, Lazy } from '../src/core'; import { _ } from '../src/core';
import { Num, Zero, Succ, sum, succ, pred, ifz, fromNumber, toNumber, sub, mul } 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).toDeferEvaluationOf(Zero); expect(toNumber(Zero)).toBe(0);
expect(toNumber(Zero)).toEvaluateTo(0);
}); });
it('converts 1', () => { it('converts 1', () => {
expect(toNumber).toDeferEvaluationOf(Succ(Zero)); expect(toNumber(Succ(Zero))).toBe(1);
expect(toNumber(Succ(Zero))).toEvaluateTo(1);
}); });
it('converts 2', () => { it('converts 2', () => {
expect(toNumber).toDeferEvaluationOf(Succ(Succ(Zero))); expect(toNumber(Succ(Succ(Zero)))).toBe(2);
expect(toNumber(Succ(Succ(Zero)))).toEvaluateTo(2);
}); });
}); });
describe('fromNumber', () => { describe('fromNumber', () => {
it('converts 0', () => { it('converts 0', () => {
expect(toNumber(fromNumber(0))).toEvaluateTo(0); expect(toNumber(fromNumber(0))).toBe(0);
}); });
it('converts 1', () => { it('converts 1', () => {
expect(toNumber(fromNumber(1))).toEvaluateTo(1); expect(toNumber(fromNumber(1))).toBe(1);
}); });
it('converts 2', () => { it('converts 2', () => {
expect(toNumber(fromNumber(2))).toEvaluateTo(2); expect(toNumber(fromNumber(2))).toBe(2);
}); });
}); });
@ -51,7 +48,7 @@ describe('Num', () => {
}); });
it('deeply lazy', () => { it('deeply lazy', () => {
const alot: Lazy<Num> = fromNumber(1000000); const alot: Num = fromNumber(1000000);
expect(ifz(Succ(alot))(_(true))(_(false))).toEvaluateTo(false); expect(ifz(Succ(alot))(_(true))(_(false))).toEvaluateTo(false);
expect(alot.evaluated).toBe(false); expect(alot.evaluated).toBe(false);
}); });
@ -60,110 +57,110 @@ describe('Num', () => {
describe('succ', () => { describe('succ', () => {
it('succ 0 is 1', () => { it('succ 0 is 1', () => {
expect(succ).toDeferEvaluationOf(fromNumber(0)); expect(succ).toDeferEvaluationOf(fromNumber(0));
expect(toNumber(succ(fromNumber(0)))).toEvaluateTo(1); expect(toNumber(succ(fromNumber(0)))).toBe(1);
}); });
it('succ 1 is 2', () => { it('succ 1 is 2', () => {
expect(succ).toDeferEvaluationOf(fromNumber(0)); expect(succ).toDeferEvaluationOf(fromNumber(0));
expect(toNumber(succ(fromNumber(1)))).toEvaluateTo(2); expect(toNumber(succ(fromNumber(1)))).toBe(2);
}); });
it('succ 10 is 11', () => { it('succ 10 is 11', () => {
expect(succ).toDeferEvaluationOf(fromNumber(10)); expect(succ).toDeferEvaluationOf(fromNumber(10));
expect(toNumber(succ(fromNumber(10)))).toEvaluateTo(11); expect(toNumber(succ(fromNumber(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(fromNumber(0));
expect(toNumber(pred(fromNumber(0)))).toEvaluateTo(0); expect(toNumber(pred(fromNumber(0)))).toBe(0);
}); });
it('pred 1 is 0', () => { it('pred 1 is 0', () => {
expect(pred).toDeferEvaluationOf(fromNumber(0)); expect(pred).toDeferEvaluationOf(fromNumber(0));
expect(toNumber(pred(fromNumber(1)))).toEvaluateTo(0); expect(toNumber(pred(fromNumber(1)))).toBe(0);
}); });
it('pred 10 is 9', () => { it('pred 10 is 9', () => {
expect(pred).toDeferEvaluationOf(fromNumber(10)); expect(pred).toDeferEvaluationOf(fromNumber(10));
expect(toNumber(pred(fromNumber(10)))).toEvaluateTo(9); expect(toNumber(pred(fromNumber(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(fromNumber(0), fromNumber(0));
expect(toNumber(sum(fromNumber(0))(fromNumber(0)))).toEvaluateTo(0); expect(toNumber(sum(fromNumber(0))(fromNumber(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(fromNumber(0), fromNumber(1));
expect(toNumber(sum(fromNumber(0))(fromNumber(1)))).toEvaluateTo(1); expect(toNumber(sum(fromNumber(0))(fromNumber(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(fromNumber(1), fromNumber(0));
expect(toNumber(sum(fromNumber(1))(fromNumber(0)))).toEvaluateTo(1); expect(toNumber(sum(fromNumber(1))(fromNumber(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(fromNumber(3), fromNumber(4));
expect(toNumber(sum(fromNumber(3))(fromNumber(4)))).toEvaluateTo(7); expect(toNumber(sum(fromNumber(3))(fromNumber(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(fromNumber(0), fromNumber(0));
expect(toNumber(sub(fromNumber(0))(fromNumber(0)))).toEvaluateTo(0); expect(toNumber(sub(fromNumber(0))(fromNumber(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(fromNumber(0), fromNumber(1));
expect(toNumber(sub(fromNumber(0))(fromNumber(1)))).toEvaluateTo(0); expect(toNumber(sub(fromNumber(0))(fromNumber(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(fromNumber(1), fromNumber(0));
expect(toNumber(sub(fromNumber(1))(fromNumber(0)))).toEvaluateTo(1); expect(toNumber(sub(fromNumber(1))(fromNumber(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(fromNumber(7), fromNumber(4));
expect(toNumber(sub(fromNumber(7))(fromNumber(4)))).toEvaluateTo(3); expect(toNumber(sub(fromNumber(7))(fromNumber(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(fromNumber(0), fromNumber(0));
expect(toNumber(mul(fromNumber(0))(fromNumber(0)))).toEvaluateTo(0); expect(toNumber(mul(fromNumber(0))(fromNumber(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(fromNumber(0), expect.skipped(fromNumber(10)));
expect(toNumber(mul(fromNumber(0))(fromNumber(10)))).toEvaluateTo(0); expect(toNumber(mul(fromNumber(0))(fromNumber(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(fromNumber(10), fromNumber(0));
expect(toNumber(mul(fromNumber(1))(fromNumber(0)))).toEvaluateTo(0); expect(toNumber(mul(fromNumber(1))(fromNumber(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(fromNumber(1), fromNumber(10));
expect(toNumber(mul(fromNumber(1))(fromNumber(10)))).toEvaluateTo(10); expect(toNumber(mul(fromNumber(1))(fromNumber(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(fromNumber(10), fromNumber(1));
expect(toNumber(mul(fromNumber(10))(fromNumber(1)))).toEvaluateTo(10); expect(toNumber(mul(fromNumber(10))(fromNumber(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(fromNumber(3), fromNumber(4));
expect(toNumber(mul(fromNumber(3))(fromNumber(4)))).toEvaluateTo(12); expect(toNumber(mul(fromNumber(3))(fromNumber(4)))).toBe(12);
}); });
}); });
}); });

Loading…
Cancel
Save