Curried lambdas
This commit is contained in:
+23
-24
@@ -1,116 +1,115 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { _, Q } from '../src/core';
|
||||
import { True, False, iif, and, not, or, xor, toBoolean, fromBoolean } from '../src/bool';
|
||||
import { toBoolean as $, False, True, fromBoolean as _, and, iif, not, or, xor } from '../src/bool';
|
||||
|
||||
describe('Bool', () => {
|
||||
describe('toBoolean', () => {
|
||||
it('converts True', () => {
|
||||
expect(toBoolean(True)).toBe(true);
|
||||
expect($(True)).toBe(true);
|
||||
});
|
||||
|
||||
it('converts False', () => {
|
||||
expect(toBoolean(False)).toBe(false);
|
||||
expect($(False)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromBoolean', () => {
|
||||
it('converts true', () => {
|
||||
expect(toBoolean(fromBoolean(true))).toBe(true);
|
||||
expect($(_(true))).toBe(true);
|
||||
});
|
||||
|
||||
it('converts false', () => {
|
||||
expect(toBoolean(fromBoolean(false))).toBe(false);
|
||||
expect($(_(false))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('iif', () => {
|
||||
it('returns true argument', () => {
|
||||
expect(iif).toDeferEvaluationOf(False, expect.skipped(_(true as unknown as Q)), _(false as unknown as Q));
|
||||
expect(iif(True).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(true);
|
||||
expect(iif).toDeferEvaluationOf(False, expect.skipped(True), False);
|
||||
expect($(iif._(True)._(True)._(False))).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false argument', () => {
|
||||
expect(iif).toDeferEvaluationOf(True, _(true as unknown as Q), expect.skipped(_(false as unknown as Q)));
|
||||
expect(iif(False).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(false);
|
||||
expect(iif).toDeferEvaluationOf(True, True, expect.skipped(False));
|
||||
expect($(iif._(False)._(True)._(False))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('not', () => {
|
||||
it('not False is True', () => {
|
||||
expect(not).toDeferEvaluationOf(False);
|
||||
expect(toBoolean(not(False))).toBe(true);
|
||||
expect($(not._(False))).toBe(true);
|
||||
|
||||
});
|
||||
|
||||
it('not True is False', () => {
|
||||
expect(not).toDeferEvaluationOf(True);
|
||||
expect(toBoolean(not(True))).toBe(false);
|
||||
expect($(not._(True))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('and', () => {
|
||||
it('False and False is 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', () => {
|
||||
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', () => {
|
||||
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', () => {
|
||||
expect(and).toDeferEvaluationOf(True, True);
|
||||
expect(toBoolean(and(True).then(e => e(True)))).toBe(true);
|
||||
expect($(and._(True)._(True))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('or', () => {
|
||||
it('False or False is 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', () => {
|
||||
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', () => {
|
||||
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', () => {
|
||||
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', () => {
|
||||
it('False xor False is 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', () => {
|
||||
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', () => {
|
||||
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', () => {
|
||||
expect(xor).toDeferEvaluationOf(True, True);
|
||||
expect(toBoolean(xor(True).then(e => e(True)))).toBe(false);
|
||||
expect($(xor._(True)._(True))).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+5
-5
@@ -1,16 +1,16 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { _, id, cnst, Q } from '../src/core';
|
||||
import { _, cnst, fromNative, id, toNative } from '../src/core';
|
||||
|
||||
describe('id', () => {
|
||||
it('returns first argument', () => {
|
||||
expect(id).toDeferEvaluationOf(null);
|
||||
expect(id(_(null as unknown as Q))).toEvaluateTo(null);
|
||||
expect(id).toDeferEvaluationOf('first');
|
||||
expect(toNative(id._(fromNative('first')))).toBe('first');
|
||||
});
|
||||
});
|
||||
|
||||
describe('cnst', () => {
|
||||
it('returns first argument after receiving second', () => {
|
||||
// expect(cnst).toDeferEvaluationOf(null, expect.skipped(null));
|
||||
expect(cnst(_(null as unknown as Q)).then(e => e(_('whatever' as unknown as Q)))).toEvaluateTo(null);
|
||||
expect(cnst).toDeferEvaluationOf('first', expect.skipped(fromNative('second')));
|
||||
expect(toNative(cnst._(fromNative('first'))._(fromNative('second')))).toBe('first');
|
||||
});
|
||||
});
|
||||
|
||||
+72
-73
@@ -1,209 +1,208 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { $, _, Lazy, Q } from '../src/core';
|
||||
import { toNumber, Zero, succ, Num, Succ, odd, sum } from '../src/num';
|
||||
import { Cons, filter, foldl, foldlz, foldr, foldrz, fromArray, fromNumberArray, head, List, map, Nil, tail, toArray, toNumberArray } from '../src/list';
|
||||
import { L, toNative } from '../src/core';
|
||||
import { toNumber as $n, Num, Succ, Zero, fromNumber as _n, odd, succ, sum } from '../src/num';
|
||||
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', () => {
|
||||
// const iterate: (n: number) => List<Lazy<number>> = n => new Lazy(() => Cons(_(n)).then(e => e(iterate(n + 1))).value);
|
||||
const niterate: (n: Num) => List<Num> = n => new Lazy(() => Cons(n).then(e => e(niterate(Succ(n)))).value);
|
||||
const iterate: (n: Num) => List<Num> = n => new L(() => Cons._(n)._(iterate(Succ._(n))).value);
|
||||
|
||||
describe('toArray', () => {
|
||||
it('converts []', () => {
|
||||
expect(toArray(Nil)).toEqual([]);
|
||||
expect($(Nil)).toEqual([]);
|
||||
});
|
||||
|
||||
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]', () => {
|
||||
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', () => {
|
||||
it('converts []', () => {
|
||||
expect(toArray(fromArray([]))).toEqual([]);
|
||||
expect($(_([]))).toEqual([]);
|
||||
});
|
||||
|
||||
it('converts [0]', () => {
|
||||
expect(toArray(fromArray([_(0 as unknown as Q)]))).toEqual([0]);
|
||||
expect($(_([0].map(_n))).map($n)).toEqual([0]);
|
||||
});
|
||||
|
||||
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', () => {
|
||||
it('returns nothing from empty list', () => {
|
||||
expect(() => head(Nil)).not.toThrow();
|
||||
expect(() => $(head(Nil))).toThrow();
|
||||
expect(() => head._(Nil)).not.toThrow();
|
||||
expect(() => toNative(head._(Nil))).toThrow();
|
||||
});
|
||||
|
||||
it('returns only item', () => {
|
||||
expect(head).toDeferEvaluationOf(Cons(_(0 as unknown as Q)).then(e => e(Nil)));
|
||||
expect(head(Cons(_(0 as unknown as Q)).then(e => e(Nil)))).toEvaluateTo(0);
|
||||
expect(head).toDeferEvaluationOf(_na([0]));
|
||||
expect($n(head._(_na([0])))).toBe(0);
|
||||
});
|
||||
|
||||
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(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil)))))).toEvaluateTo(0);
|
||||
expect(head).toDeferEvaluationOf(_na([0, 1]));
|
||||
expect($n(head._(_na([0, 1])))).toBe(0);
|
||||
});
|
||||
|
||||
it('handles infinite lists', () => {
|
||||
expect(toNumber(head(niterate(Zero)))).toBe(0);
|
||||
expect($n(head._(iterate(Zero)))).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tail', () => {
|
||||
it('drops nothing from empty list', () => {
|
||||
expect(() => tail(Nil)).not.toThrow();
|
||||
expect(() => toArray(tail(Nil))).toThrow();
|
||||
expect(() => tail._(Nil)).not.toThrow();
|
||||
expect(() => $na(tail._(Nil))).toThrow();
|
||||
});
|
||||
|
||||
it('drops only item', () => {
|
||||
expect(tail).toDeferEvaluationOf(Cons(_(0 as unknown as Q)).then(e => e(Nil)));
|
||||
expect(toArray(tail(Cons(_(0 as unknown as Q)).then(e => e(Nil))))).toEqual([]);
|
||||
expect(tail).toDeferEvaluationOf(_na([0]));
|
||||
expect($na(tail._(_na([0])))).toEqual([]);
|
||||
});
|
||||
|
||||
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(toArray(tail(Cons(_(0 as unknown as Q)).then(e => e(Cons(_(1 as unknown as Q)).then(e => e(Nil))))))).toEqual([1]);
|
||||
expect(tail).toDeferEvaluationOf(_na([0, 1]));
|
||||
expect($na(tail._(_na([0, 1])))).toEqual([1]);
|
||||
});
|
||||
|
||||
it('handles infinite lists', () => {
|
||||
expect(toNumber(head(tail(niterate(Zero))))).toBe(1);
|
||||
expect($n(head._(tail._(iterate(Zero))))).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('foldl', () => {
|
||||
it('folds empty list', () => {
|
||||
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([]));
|
||||
expect(toNumber(foldl(sum).then(e => e(Zero).then(e => e(fromNumberArray([])))))).toBe(0);
|
||||
expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([]));
|
||||
expect($n(foldl._(sum)._(Zero)._(_na([])))).toBe(0);
|
||||
});
|
||||
|
||||
it('folds single item', () => {
|
||||
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1]));
|
||||
expect(toNumber(foldl(sum).then(e => e(Zero).then(e => e(fromNumberArray([1])))))).toBe(1);
|
||||
expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([1]));
|
||||
expect($n(foldl._(sum)._(Zero)._(_na([1])))).toBe(1);
|
||||
});
|
||||
|
||||
it('folds several items', () => {
|
||||
expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2]));
|
||||
expect(toNumber(foldl(sum).then(e => e(Zero).then(e => e(fromNumberArray([1, 2])))))).toBe(3);
|
||||
expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([1, 2]));
|
||||
expect($n(foldl._(sum)._(Zero)._(_na([1, 2])))).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('foldlz', () => {
|
||||
it('does not fold empty list', () => {
|
||||
expect(() => foldlz(sum).then(e => e(Nil))).not.toThrow();
|
||||
expect(() => toNumber(foldlz(sum).then(e => e(Nil)))).toThrow();
|
||||
expect(() => foldlz._(sum)._(Nil)).not.toThrow();
|
||||
expect(() => $n(foldlz._(sum)._(Nil))).toThrow();
|
||||
});
|
||||
|
||||
it('folds single item', () => {
|
||||
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
|
||||
expect(toNumber(foldlz(sum).then(e => e(fromNumberArray([1]))))).toBe(1);
|
||||
expect(foldlz._(sum)).toDeferEvaluationOf(_na([1]));
|
||||
expect($n(foldlz._(sum)._(_na([1])))).toBe(1);
|
||||
});
|
||||
|
||||
it('folds several items', () => {
|
||||
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2]));
|
||||
expect(toNumber(foldlz(sum).then(e => e(fromNumberArray([1, 2]))))).toBe(3);
|
||||
expect(foldlz._(sum)).toDeferEvaluationOf(_na([1, 2]));
|
||||
expect($n(foldlz._(sum)._(_na([1, 2])))).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('foldr', () => {
|
||||
it('folds empty list', () => {
|
||||
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([]));
|
||||
expect(toNumber(foldr(sum).then(e => e(Zero).then(e => e(fromNumberArray([])))))).toBe(0);
|
||||
expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([]));
|
||||
expect($n(foldr._(sum)._(Zero)._(_na([])))).toBe(0);
|
||||
});
|
||||
|
||||
it('folds single item', () => {
|
||||
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1]));
|
||||
expect(toNumber(foldr(sum).then(e => e(Zero).then(e => e(fromNumberArray([1])))))).toBe(1);
|
||||
expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([1]));
|
||||
expect($n(foldr._(sum)._(Zero)._(_na([1])))).toBe(1);
|
||||
});
|
||||
|
||||
it('folds several items', () => {
|
||||
expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2]));
|
||||
expect(toNumber(foldr(sum).then(e => e(Zero).then(e => e(fromNumberArray([1, 2])))))).toBe(3);
|
||||
expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([1, 2]));
|
||||
expect($n(foldr._(sum)._(Zero)._(_na([1, 2])))).toBe(3);
|
||||
});
|
||||
|
||||
// it('handles infinite lists', () => {
|
||||
// expect(foldr<Lazy<number>, Lazy<number>>(x => _(a =>
|
||||
// iif<Lazy<number>>
|
||||
// (x.then(x => fromBoolean(x === 0)))
|
||||
// .then(e => e
|
||||
// (x).then(e => e
|
||||
// (x.then(x => a.then(a => _(x + a)))))))
|
||||
// ).then(e => e(_(0 as unknown as Q)).then(e => e(iterate(-5))))).toEvaluateTo(-15);
|
||||
// (x.call(x => fromBoolean(x === 0)))
|
||||
// .call(
|
||||
// (x).call(
|
||||
// (x.call(x => a.call(a => _(x + a)))))))
|
||||
// ).call((fromNumber(0)).call((iterate(-5))))).toEvaluateTo(-15);
|
||||
// });
|
||||
});
|
||||
|
||||
describe('foldrz', () => {
|
||||
it('does not fold empty list', () => {
|
||||
expect(() => foldrz(sum).then(e => e(Nil))).not.toThrow();
|
||||
expect(() => toNumber(foldrz(sum).then(e => e(Nil)))).toThrow();
|
||||
expect(() => foldrz._(sum)._(Nil)).not.toThrow();
|
||||
expect(() => $n(foldrz._(sum)._(Nil))).toThrow();
|
||||
});
|
||||
|
||||
it.failing('folds single item', () => {
|
||||
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
|
||||
expect(toNumber(foldrz(sum).then(e => e(fromNumberArray([1]))))).toBe(1);
|
||||
expect(foldrz._(sum)).toDeferEvaluationOf(_na([1]));
|
||||
expect($n(foldrz._(sum)._(_na([1])))).toBe(1);
|
||||
});
|
||||
|
||||
it.failing('folds several items', () => {
|
||||
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2]));
|
||||
expect(toNumber(foldrz(sum).then(e => e(fromNumberArray([1, 2]))))).toBe(3);
|
||||
expect(foldrz._(sum)).toDeferEvaluationOf(_na([1, 2]));
|
||||
expect($n(foldrz._(sum)._(_na([1, 2])))).toBe(3);
|
||||
});
|
||||
|
||||
// it.failing('handles infinite lists', () => {
|
||||
// expect(foldrz<Lazy<number>>(x => _(a =>
|
||||
// iif<Lazy<number>>
|
||||
// (x.then(x => fromBoolean(x === 0)))
|
||||
// .then(e => e
|
||||
// (x).then(e => e
|
||||
// (x.then(x => a.then(a => _(x + a)))))))
|
||||
// ).then(e => e(iterate(-5)))).toEvaluateTo(-10);
|
||||
// (x.call(x => fromBoolean(x === 0)))
|
||||
// .call(
|
||||
// (x).call(
|
||||
// (x.call(x => a.call(a => _(x + a)))))))
|
||||
// ).call((iterate(-5)))).toEvaluateTo(-10);
|
||||
// });
|
||||
});
|
||||
|
||||
describe('map', () => {
|
||||
it('maps empty list', () => {
|
||||
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([]));
|
||||
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([]))))).toEqual([]);
|
||||
expect(map._(succ)).toDeferEvaluationOf(_na([]));
|
||||
expect($na(map._(succ)._(_na([])))).toEqual([]);
|
||||
});
|
||||
|
||||
it('maps only item', () => {
|
||||
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0]));
|
||||
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([0]))))).toEqual([1]);
|
||||
expect(map._(succ)).toDeferEvaluationOf(_na([0]));
|
||||
expect($na(map._(succ)._(_na([0])))).toEqual([1]);
|
||||
});
|
||||
|
||||
it('maps several items', () => {
|
||||
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0, 1]));
|
||||
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([0, 1]))))).toEqual([1, 2]);
|
||||
expect(map._(succ)).toDeferEvaluationOf(_na([0, 1]));
|
||||
expect($na(map._(succ)._(_na([0, 1])))).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
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', () => {
|
||||
it('filter empty list', () => {
|
||||
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([]));
|
||||
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([]))))).toEqual([]);
|
||||
expect(filter._(odd)).toDeferEvaluationOf(_na([]));
|
||||
expect($na(filter._(odd)._(_na([])))).toEqual([]);
|
||||
});
|
||||
|
||||
it('filters only item', () => {
|
||||
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0]));
|
||||
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([]))))).toEqual([]);
|
||||
expect(filter._(odd)).toDeferEvaluationOf(_na([0]));
|
||||
expect($na(filter._(odd)._(_na([])))).toEqual([]);
|
||||
});
|
||||
|
||||
it('filters several items', () => {
|
||||
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1]));
|
||||
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([0, 1]))))).toEqual([1]);
|
||||
expect(filter._(odd)).toDeferEvaluationOf(_na([0, 1]));
|
||||
expect($na(filter._(odd)._(_na([0, 1])))).toEqual([1]);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+55
-56
@@ -1,166 +1,165 @@
|
||||
import { describe, expect, it } from '@jest/globals';
|
||||
import { _, Q } from '../src/core';
|
||||
import { Num, Zero, Succ, sum, succ, pred, ifz, fromNumber, toNumber, sub, mul } from '../src/num';
|
||||
import { toNumber as $, Num, Succ, Zero, fromNumber as _, ifz, mul, pred, sub, succ, sum } from '../src/num';
|
||||
|
||||
describe('Num', () => {
|
||||
describe('toNumber', () => {
|
||||
it('converts 0', () => {
|
||||
expect(toNumber(Zero)).toBe(0);
|
||||
expect($(Zero)).toBe(0);
|
||||
});
|
||||
|
||||
it('converts 1', () => {
|
||||
expect(toNumber(Succ(Zero))).toBe(1);
|
||||
expect($(Succ._(Zero))).toBe(1);
|
||||
});
|
||||
|
||||
it('converts 2', () => {
|
||||
expect(toNumber(Succ(Succ(Zero)))).toBe(2);
|
||||
expect($(Succ._(Succ._(Zero)))).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fromNumber', () => {
|
||||
it('converts 0', () => {
|
||||
expect(toNumber(fromNumber(0))).toBe(0);
|
||||
expect($(_(0))).toBe(0);
|
||||
});
|
||||
|
||||
it('converts 1', () => {
|
||||
expect(toNumber(fromNumber(1))).toBe(1);
|
||||
expect($(_(1))).toBe(1);
|
||||
});
|
||||
|
||||
it('converts 2', () => {
|
||||
expect(toNumber(fromNumber(2))).toBe(2);
|
||||
expect($(_(2))).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ifz', () => {
|
||||
it('returns zero branch', () => {
|
||||
expect(ifz).toDeferEvaluationOf(fromNumber(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).toDeferEvaluationOf(_(0), true, false);
|
||||
expect($(ifz._(_(0))._(_(0))._(_(1)))).toBe(0);
|
||||
});
|
||||
|
||||
it('returns non-zero branch', () => {
|
||||
expect(ifz).toDeferEvaluationOf(fromNumber(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).toDeferEvaluationOf(_(1), true, false);
|
||||
expect($(ifz._(_(1))._(_(0))._(_(1)))).toBe(1);
|
||||
});
|
||||
|
||||
it('returns non-zero branch', () => {
|
||||
expect(ifz).toDeferEvaluationOf(fromNumber(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).toDeferEvaluationOf(_(100), true, false);
|
||||
expect($(ifz._(_(10))._(_(0))._(_(1)))).toBe(1);
|
||||
});
|
||||
|
||||
it('deeply lazy', () => {
|
||||
const alot: Num = fromNumber(1000000);
|
||||
expect(ifz(Succ(alot)).then(e => e(_(true as unknown as Q)).then(e => e(_(false as unknown as Q))))).toEvaluateTo(false);
|
||||
const alot: Num = _(1000000);
|
||||
expect($(ifz._(Succ._(alot))._(_(0))._(_(1)))).toBe(1);
|
||||
expect(alot.evaluated).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('succ', () => {
|
||||
it('succ 0 is 1', () => {
|
||||
expect(succ).toDeferEvaluationOf(fromNumber(0));
|
||||
expect(toNumber(succ(fromNumber(0)))).toBe(1);
|
||||
expect(succ).toDeferEvaluationOf(_(0));
|
||||
expect($(succ._(_(0)))).toBe(1);
|
||||
});
|
||||
|
||||
it('succ 1 is 2', () => {
|
||||
expect(succ).toDeferEvaluationOf(fromNumber(0));
|
||||
expect(toNumber(succ(fromNumber(1)))).toBe(2);
|
||||
expect(succ).toDeferEvaluationOf(_(0));
|
||||
expect($(succ._(_(1)))).toBe(2);
|
||||
});
|
||||
|
||||
it('succ 10 is 11', () => {
|
||||
expect(succ).toDeferEvaluationOf(fromNumber(10));
|
||||
expect(toNumber(succ(fromNumber(10)))).toBe(11);
|
||||
expect(succ).toDeferEvaluationOf(_(10));
|
||||
expect($(succ._(_(10)))).toBe(11);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pred', () => {
|
||||
it('pred 0 is 0', () => {
|
||||
expect(pred).toDeferEvaluationOf(fromNumber(0));
|
||||
expect(toNumber(pred(fromNumber(0)))).toBe(0);
|
||||
expect(pred).toDeferEvaluationOf(_(0));
|
||||
expect($(pred._(_(0)))).toBe(0);
|
||||
});
|
||||
|
||||
it('pred 1 is 0', () => {
|
||||
expect(pred).toDeferEvaluationOf(fromNumber(0));
|
||||
expect(toNumber(pred(fromNumber(1)))).toBe(0);
|
||||
expect(pred).toDeferEvaluationOf(_(0));
|
||||
expect($(pred._(_(1)))).toBe(0);
|
||||
});
|
||||
|
||||
it('pred 10 is 9', () => {
|
||||
expect(pred).toDeferEvaluationOf(fromNumber(10));
|
||||
expect(toNumber(pred(fromNumber(10)))).toBe(9);
|
||||
expect(pred).toDeferEvaluationOf(_(10));
|
||||
expect($(pred._(_(10)))).toBe(9);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sum', () => {
|
||||
it('0 sum 0 is 0', () => {
|
||||
expect(sum).toDeferEvaluationOf(fromNumber(0), fromNumber(0));
|
||||
expect(toNumber(sum(fromNumber(0)).then(e => e(fromNumber(0))))).toBe(0);
|
||||
expect(sum).toDeferEvaluationOf(_(0), _(0));
|
||||
expect($(sum._(_(0))._(_(0)))).toBe(0);
|
||||
});
|
||||
|
||||
it('0 sum 1 is 1', () => {
|
||||
expect(sum).toDeferEvaluationOf(fromNumber(0), fromNumber(1));
|
||||
expect(toNumber(sum(fromNumber(0)).then(e => e(fromNumber(1))))).toBe(1);
|
||||
expect(sum).toDeferEvaluationOf(_(0), _(1));
|
||||
expect($(sum._(_(0))._(_(1)))).toBe(1);
|
||||
});
|
||||
|
||||
it('1 sum 0 is 1', () => {
|
||||
expect(sum).toDeferEvaluationOf(fromNumber(1), fromNumber(0));
|
||||
expect(toNumber(sum(fromNumber(1)).then(e => e(fromNumber(0))))).toBe(1);
|
||||
expect(sum).toDeferEvaluationOf(_(1), _(0));
|
||||
expect($(sum._(_(1))._(_(0)))).toBe(1);
|
||||
});
|
||||
|
||||
it('3 sum 4 is 7', () => {
|
||||
expect(sum).toDeferEvaluationOf(fromNumber(3), fromNumber(4));
|
||||
expect(toNumber(sum(fromNumber(3)).then(e => e(fromNumber(4))))).toBe(7);
|
||||
expect(sum).toDeferEvaluationOf(_(3), _(4));
|
||||
expect($(sum._(_(3))._(_(4)))).toBe(7);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sub', () => {
|
||||
it('0 sub 0 is 0', () => {
|
||||
expect(sub).toDeferEvaluationOf(fromNumber(0), fromNumber(0));
|
||||
expect(toNumber(sub(fromNumber(0)).then(e => e(fromNumber(0))))).toBe(0);
|
||||
expect(sub).toDeferEvaluationOf(_(0), _(0));
|
||||
expect($(sub._(_(0))._(_(0)))).toBe(0);
|
||||
});
|
||||
|
||||
it('0 sub 1 is 0', () => {
|
||||
expect(sub).toDeferEvaluationOf(fromNumber(0), fromNumber(1));
|
||||
expect(toNumber(sub(fromNumber(0)).then(e => e(fromNumber(1))))).toBe(0);
|
||||
expect(sub).toDeferEvaluationOf(_(0), _(1));
|
||||
expect($(sub._(_(0))._(_(1)))).toBe(0);
|
||||
});
|
||||
|
||||
it('1 sub 0 is 1', () => {
|
||||
expect(sub).toDeferEvaluationOf(fromNumber(1), fromNumber(0));
|
||||
expect(toNumber(sub(fromNumber(1)).then(e => e(fromNumber(0))))).toBe(1);
|
||||
expect(sub).toDeferEvaluationOf(_(1), _(0));
|
||||
expect($(sub._(_(1))._(_(0)))).toBe(1);
|
||||
});
|
||||
|
||||
it('7 sub 4 is 3', () => {
|
||||
expect(sub).toDeferEvaluationOf(fromNumber(7), fromNumber(4));
|
||||
expect(toNumber(sub(fromNumber(7)).then(e => e(fromNumber(4))))).toBe(3);
|
||||
expect(sub).toDeferEvaluationOf(_(7), _(4));
|
||||
expect($(sub._(_(7))._(_(4)))).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mul', () => {
|
||||
it('0 mul 0 is 0', () => {
|
||||
expect(mul).toDeferEvaluationOf(fromNumber(0), fromNumber(0));
|
||||
expect(toNumber(mul(fromNumber(0)).then(e => e(fromNumber(0))))).toBe(0);
|
||||
expect(mul).toDeferEvaluationOf(_(0), _(0));
|
||||
expect($(mul._(_(0))._(_(0)))).toBe(0);
|
||||
});
|
||||
|
||||
it('0 mul 10 is 0', () => {
|
||||
expect(mul).toDeferEvaluationOf(fromNumber(0), expect.skipped(fromNumber(10)));
|
||||
expect(toNumber(mul(fromNumber(0)).then(e => e(fromNumber(10))))).toBe(0);
|
||||
expect(mul).toDeferEvaluationOf(_(0), expect.skipped(_(10)));
|
||||
expect($(mul._(_(0))._(_(10)))).toBe(0);
|
||||
});
|
||||
|
||||
it('10 mul 0 is 0', () => {
|
||||
expect(mul).toDeferEvaluationOf(fromNumber(10), fromNumber(0));
|
||||
expect(toNumber(mul(fromNumber(1)).then(e => e(fromNumber(0))))).toBe(0);
|
||||
expect(mul).toDeferEvaluationOf(_(10), _(0));
|
||||
expect($(mul._(_(1))._(_(0)))).toBe(0);
|
||||
});
|
||||
|
||||
it('1 mul 10 is 10', () => {
|
||||
expect(mul).toDeferEvaluationOf(fromNumber(1), fromNumber(10));
|
||||
expect(toNumber(mul(fromNumber(1)).then(e => e(fromNumber(10))))).toBe(10);
|
||||
expect(mul).toDeferEvaluationOf(_(1), _(10));
|
||||
expect($(mul._(_(1))._(_(10)))).toBe(10);
|
||||
});
|
||||
|
||||
it('10 mul 1 is 10', () => {
|
||||
expect(mul).toDeferEvaluationOf(fromNumber(10), fromNumber(1));
|
||||
expect(toNumber(mul(fromNumber(10)).then(e => e(fromNumber(1))))).toBe(10);
|
||||
expect(mul).toDeferEvaluationOf(_(10), _(1));
|
||||
expect($(mul._(_(10))._(_(1)))).toBe(10);
|
||||
});
|
||||
|
||||
it('3 mul 4 is 12', () => {
|
||||
expect(mul).toDeferEvaluationOf(fromNumber(3), fromNumber(4));
|
||||
expect(toNumber(mul(fromNumber(3)).then(e => e(fromNumber(4))))).toBe(12);
|
||||
expect(mul).toDeferEvaluationOf(_(3), _(4));
|
||||
expect($(mul._(_(3))._(_(4)))).toBe(12);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+19
-19
@@ -1,8 +1,8 @@
|
||||
import { expect } from '@jest/globals';
|
||||
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(
|
||||
public readonly when: 'always' | 'deferred' | 'never',
|
||||
public readonly what: T) { }
|
||||
@@ -10,25 +10,25 @@ class Deferrable<T extends V> {
|
||||
|
||||
const toEvaluateTo: MatcherFunction<[expected: unknown]> =
|
||||
function (actual, expected) {
|
||||
if (!(actual instanceof Lazy)) {
|
||||
if (!(actual instanceof L)) {
|
||||
throw new TypeError('Actual must be of type Lazy!');
|
||||
}
|
||||
if (this.equals($(actual), expected)) {
|
||||
if (this.equals(fromNative(actual), expected)) {
|
||||
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,
|
||||
};
|
||||
} else {
|
||||
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,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<V>)[]> =
|
||||
const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<P>)[]> =
|
||||
function (actual, ...args) {
|
||||
while (actual instanceof Lazy) {
|
||||
while (actual instanceof L) {
|
||||
actual = actual.value;
|
||||
}
|
||||
if (typeof actual !== 'function') {
|
||||
@@ -37,16 +37,16 @@ const toDeferEvaluationOf: MatcherFunction<(unknown | Deferrable<V>)[]> =
|
||||
if (this.isNot) {
|
||||
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 Lazy ? new Deferrable('deferred', _(v.value))
|
||||
: v instanceof L ? new Deferrable('deferred', _(v.value))
|
||||
: new Deferrable('deferred', _(v as any)));
|
||||
const cargs: Deferrable<V>[] = [];
|
||||
const cargs: Deferrable<P>[] = [];
|
||||
let result = actual;
|
||||
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);
|
||||
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) {
|
||||
return {
|
||||
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.');
|
||||
}
|
||||
result.value;
|
||||
const mismatchingArg: Deferrable<V> | undefined = cargs.find(v => v.when === 'never' && v.what.evaluated);
|
||||
(() => result.value)();
|
||||
const mismatchingArg: Deferrable<P> | undefined = cargs.find(v => v.when === 'never' && v.what.evaluated);
|
||||
if (mismatchingArg) {
|
||||
return {
|
||||
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' {
|
||||
interface AsymmetricMatchers {
|
||||
evaluated<T extends V>(value: T): Deferrable<T>;
|
||||
deferred<T extends V>(value: T): Deferrable<T>;
|
||||
skipped<T extends V>(value: T): Deferrable<T>;
|
||||
evaluated<T extends P>(value: T): Deferrable<T>;
|
||||
deferred<T extends P>(value: T): Deferrable<T>;
|
||||
skipped<T extends P>(value: T): Deferrable<T>;
|
||||
toEvaluateTo(value: any): void;
|
||||
toDeferEvaluationOf(...args: any[]): void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user