import { describe, expect, it } from '@jest/globals'; import { $, _, Lazy } from '../src/core'; import { fromBoolean, iif } from '../src/bool'; 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'; describe('List', () => { const iterate: (n: number) => Lazy> = n => new Lazy(() => Cons(_(n))(iterate(n + 1)).value); const niterate: (n: Lazy) => Lazy> = n => new Lazy(() => Cons(n)(niterate(Succ(n))).value); describe('toArray', () => { it('converts []', () => { expect(toArray).toDeferEvaluationOf(Nil); expect(toArray(Nil)).toEvaluateTo([]); }); it('converts [0]', () => { expect(toArray).toDeferEvaluationOf(Cons(_(0))(Nil)); expect(toArray(Cons(_(0))(Nil))).toEvaluateTo([0]); }); it('converts [0, 1]', () => { expect(toArray).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil))); expect(toArray(Cons(_(0))(Cons(_(1))(Nil)))).toEvaluateTo([0, 1]); }); }); describe('fromArray', () => { it('converts []', () => { expect(toArray(fromArray([]))).toEvaluateTo([]); }); it('converts [0]', () => { expect(toArray(fromArray([0]))).toEvaluateTo([0]); }); it('converts 2', () => { expect(toArray(fromArray([0, 1]))).toEvaluateTo([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))(Nil)); expect(head(Cons(_(0))(Nil))).toEvaluateTo(0); }); it('returns first item', () => { expect(head).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil))); expect(head(Cons(_(0))(Cons(_(1))(Nil)))).toEvaluateTo(0); }); it('handles infinite lists', () => { expect(head(iterate(0))).toEvaluateTo(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))(Nil)); expect(toArray(tail(Cons(_(0))(Nil)))).toEvaluateTo([]); }); it('drops first item', () => { expect(tail).toDeferEvaluationOf(Cons(_(0))(Cons(_(1))(Nil))); expect(toArray(tail(Cons(_(0))(Cons(_(1))(Nil))))).toEvaluateTo([1]); }); it('handles infinite lists', () => { expect(head(tail(iterate(0)))).toEvaluateTo(1); }); }); describe('foldl', () => { it('folds empty list', () => { expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([])); expect(toNumber(foldl(sum)(Zero)(fromNumberArray([])))).toEvaluateTo(0); }); it('folds single item', () => { expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1])); expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1])))).toEvaluateTo(1); }); it('folds several items', () => { expect(foldl(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2])); expect(toNumber(foldl(sum)(Zero)(fromNumberArray([1, 2])))).toEvaluateTo(3); }); }); describe('foldlz', () => { it('does not fold empty list', () => { expect(() => foldlz(sum)(Nil)).not.toThrow(); expect(() => $(toNumber(foldlz(sum)(Nil)))).toThrow(); }); it('folds single item', () => { expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1])); expect(toNumber(foldlz(sum)(fromNumberArray([1])))).toEvaluateTo(1); }); it('folds several items', () => { expect(foldlz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2])); expect(toNumber(foldlz(sum)(fromNumberArray([1, 2])))).toEvaluateTo(3); }); }); describe('foldr', () => { it('folds empty list', () => { expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([])); expect(toNumber(foldr(sum)(Zero)(fromNumberArray([])))).toEvaluateTo(0); }); it('folds single item', () => { expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1])); expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1])))).toEvaluateTo(1); }); it('folds several items', () => { expect(foldr(sum)).toDeferEvaluationOf(Zero, fromNumberArray([1, 2])); expect(toNumber(foldr(sum)(Zero)(fromNumberArray([1, 2])))).toEvaluateTo(3); }); it('handles infinite lists', () => { expect(foldr(x => a => iif (x.then(x => fromBoolean(x === 0))) (x) (x.then(x => a.then(a => _(x + a)))) )(_(0))(iterate(-5))).toEvaluateTo(-15); }); }); describe('foldrz', () => { it('does not fold empty list', () => { expect(() => foldrz(sum)(Nil)).not.toThrow(); expect(() => $(toNumber(foldrz(sum)(Nil)))).toThrow(); }); it.failing('folds single item', () => { expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1])); expect(toNumber(foldrz(sum)(fromNumberArray([1])))).toEvaluateTo(1); }); it.failing('folds several items', () => { expect(foldrz(sum)).toDeferEvaluationOf(fromNumberArray([1, 2])); expect(toNumber(foldrz(sum)(fromNumberArray([1, 2])))).toEvaluateTo(3); }); it.failing('handles infinite lists', () => { expect(foldrz(x => a => iif (x.then(x => fromBoolean(x === 0))) (x) (x.then(x => a.then(a => _(x + a)))) )(iterate(-5))).toEvaluateTo(-10); }); }); describe('map', () => { it('maps empty list', () => { expect(map(succ)).toDeferEvaluationOf(fromNumberArray([])); expect(toNumberArray(map(succ)(fromNumberArray([])))).toEvaluateTo([]); }); it('maps only item', () => { expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0])); expect(toNumberArray(map(succ)(fromNumberArray([0])))).toEvaluateTo([1]); }); it('maps several items', () => { expect(map(succ)).toDeferEvaluationOf(fromNumberArray([0, 1])); expect(toNumberArray(map(succ)(fromNumberArray([0, 1])))).toEvaluateTo([1, 2]); }); it('handles infinite lists', () => { expect(toNumber(head(map(succ)(niterate(Zero))))).toEvaluateTo(1); }); }); describe('filter', () => { it('filter empty list', () => { expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([])); expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEvaluateTo([]); }); it('filters only item', () => { expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0])); expect(toNumberArray(filter(odd)(fromNumberArray([])))).toEvaluateTo([]); }); it('filters several items', () => { expect(filter(odd)).toDeferEvaluationOf(fromNumberArray([0, 1])); expect(toNumberArray(filter(odd)(fromNumberArray([0, 1])))).toEvaluateTo([1]); }); it('handles infinite lists', () => { expect(toNumber(head(filter(odd)(niterate(Zero))))).toEvaluateTo(1); }); }); });