Lambda calculus-like library written in TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
jlc/test/list.spec.ts

209 lines
7.7 KiB

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';
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);
describe('toArray', () => {
it('converts []', () => {
expect(toArray(Nil)).toEqual([]);
});
it('converts [0]', () => {
expect(toArray(Cons(_(0 as unknown as Q)).then(e => e(Nil)))).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]);
});
});
describe('fromArray', () => {
it('converts []', () => {
expect(toArray(fromArray([]))).toEqual([]);
});
it('converts [0]', () => {
expect(toArray(fromArray([_(0 as unknown as Q)]))).toEqual([0]);
});
it('converts 2', () => {
expect(toArray(fromArray([_(0 as unknown as Q), _(1 as unknown as Q)]))).toEqual([0, 1]);
});
});
describe('head', () => {
it('returns nothing from empty list', () => {
expect(() => head(Nil)).not.toThrow();
expect(() => $(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);
});
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);
});
it('handles infinite lists', () => {
expect(toNumber(head(niterate(Zero)))).toBe(0);
});
});
describe('tail', () => {
it('drops nothing from empty list', () => {
expect(() => tail(Nil)).not.toThrow();
expect(() => toArray(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([]);
});
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]);
});
it('handles infinite lists', () => {
expect(toNumber(head(tail(niterate(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);
});
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);
});
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);
});
});
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();
});
it('folds single item', () => {
expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
expect(toNumber(foldlz(sum).then(e => e(fromNumberArray([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);
});
});
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);
});
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);
});
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);
});
// 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);
// });
});
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();
});
it.failing('folds single item', () => {
expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1]));
expect(toNumber(foldrz(sum).then(e => e(fromNumberArray([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);
});
// 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);
// });
});
describe('map', () => {
it('maps empty list', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([]));
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([]))))).toEqual([]);
});
it('maps only item', () => {
expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0]));
expect(toNumberArray(map(succ).then(e => e(fromNumberArray([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]);
});
it('handles infinite lists', () => {
expect(toNumber(head(map(succ).then(e => e(niterate(Zero)))))).toBe(1);
});
});
describe('filter', () => {
it('filter empty list', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([]));
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([]))))).toEqual([]);
});
it('filters only item', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0]));
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([]))))).toEqual([]);
});
it('filters several items', () => {
expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1]));
expect(toNumberArray(filter(odd).then(e => e(fromNumberArray([0, 1]))))).toEqual([1]);
});
it('handles infinite lists', () => {
expect(toNumber(head(filter(odd).then(e => e(niterate(Zero)))))).toBe(1);
});
});
});