import { describe, expect, it } from '@jest/globals'; import { L, _, toNative, undef } from '../src/core'; import { toBoolean as $b, iif } from '../src/bool'; import { toNumber as $n, Num, Succ, Zero, fromNumber as _n, eq as eqn, even, odd, succ, sum } from '../src/num'; import { toArray as $a, toNumberArray as $na, Cons, List, Nil, fromArray as _a, fromNumberArray as _na, all, any, concat, cons, eq, filter, foldl, foldlz, foldr, foldrz, head, map, tail } from '../src/list'; describe('List', () => { const iterate: (n: Num) => List = n => new L(() => Cons._(n)._(iterate(Succ._(n))).value); const infinite: List = iterate(Zero); describe('toArray', () => { it('converts []', () => { expect($a(Nil)).toEqual([]); }); it('converts [0]', () => { expect($a(Cons._(_n(0))._(Nil)).map($n)).toEqual([0]); }); it('converts [0, 1]', () => { expect($a(Cons._(_n(0))._(Cons._(_n(1))._(Nil))).map($n)).toEqual([0, 1]); }); }); describe('fromArray', () => { it('converts []', () => { expect($a(_a([]))).toEqual([]); }); it('converts [0]', () => { expect($a(_a([0].map(_n))).map($n)).toEqual([0]); }); it('converts 2', () => { expect($a(_a([0, 1].map(_n))).map($n)).toEqual([0, 1]); }); }); describe('cons', () => { it('0 cons Nil is [0]', () => { expect(cons).toDeferEvaluationOf(_n(0), Nil); expect($na(cons._(_n(0))._(Nil))).toEqual([0]); }); it('0 cons [1] is [0, 1]', () => { expect(cons).toDeferEvaluationOf(_n(0), _na([1])); expect($na(cons._(_n(0))._(_na([1])))).toEqual([0, 1]); }); it('0 cons [1, 2] is [0, 1, 2]', () => { expect(cons).toDeferEvaluationOf(_n(0), _na([1, 2])); expect($na(cons._(_n(0))._(_na([1, 2])))).toEqual([0, 1, 2]); }); it('handles infinite lists', () => { expect($n(cons._(_n(0))._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe(0); }); }); describe('concat', () => { it('Nil concat Nil is Nil', () => { expect(concat).toDeferEvaluationOf(Nil, Nil); expect($na(concat._(Nil)._(Nil))).toEqual([]); }); it('[0] concat [1] is [0, 1]', () => { expect(concat).toDeferEvaluationOf(_na([0]), _na([1])); expect($na(concat._(_na([0]))._(_na([1])))).toEqual([0, 1]); }); it('[0, 1] concat [2, 3] is [0, 1, 2, 3]', () => { expect(concat).toDeferEvaluationOf(_na([0, 1]), _na([2, 3])); expect($na(concat._(_na([0, 1]))._(_na([2, 3])))).toEqual([0, 1, 2, 3]); }); it('handles infinite lists', () => { expect($n(concat._(infinite)._(infinite)._(undef)._(_(x => _(_xs => x))))).toBe(0); }); }); describe('head', () => { it('returns nothing from Nil', () => { expect(() => head._(Nil)).not.toThrow(); expect(() => toNative(head._(Nil))).toThrow(); }); it('returns only item', () => { expect(head).toDeferEvaluationOf(_na([0])); expect($n(head._(_na([0])))).toBe(0); }); it('returns first item', () => { expect(head).toDeferEvaluationOf(_na([0, 1])); expect($n(head._(_na([0, 1])))).toBe(0); }); it('handles infinite lists', () => { expect($n(head._(infinite))).toBe(0); }); }); describe('tail', () => { it('drops nothing from Nil', () => { expect(() => tail._(Nil)).not.toThrow(); expect(() => $na(tail._(Nil))).toThrow(); }); it('drops only item', () => { expect(tail).toDeferEvaluationOf(_na([0])); expect($na(tail._(_na([0])))).toEqual([]); }); it('drops first item', () => { expect(tail).toDeferEvaluationOf(_na([0, 1])); expect($na(tail._(_na([0, 1])))).toEqual([1]); }); it('handles infinite lists', () => { expect($n(head._(tail._(infinite)))).toBe(1); }); }); describe('foldl', () => { it('folds Nil', () => { expect(foldl._(sum)).toDeferEvaluationOf(Zero, _na([])); expect($n(foldl._(sum)._(Zero)._(_na([])))).toBe(0); }); it('folds single item', () => { 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, _na([1, 2])); expect($n(foldl._(sum)._(Zero)._(_na([1, 2])))).toBe(3); }); }); describe('foldlz', () => { it('does not fold Nil', () => { expect(() => foldlz._(sum)._(Nil)).not.toThrow(); expect(() => $n(foldlz._(sum)._(Nil))).toThrow(); }); it('folds single item', () => { expect(foldlz._(sum)).toDeferEvaluationOf(_na([1])); expect($n(foldlz._(sum)._(_na([1])))).toBe(1); }); it('folds several items', () => { expect(foldlz._(sum)).toDeferEvaluationOf(_na([1, 2])); expect($n(foldlz._(sum)._(_na([1, 2])))).toBe(3); }); }); describe('foldr', () => { it('folds Nil', () => { expect(foldr._(sum)).toDeferEvaluationOf(Zero, _na([])); expect($n(foldr._(sum)._(Zero)._(_na([])))).toBe(0); }); it('folds single item', () => { 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, _na([1, 2])); expect($n(foldr._(sum)._(Zero)._(_na([1, 2])))).toBe(3); }); it('handles infinite lists', () => { expect($n( foldr ._(_(x => _(a => iif._(eqn._(x)._(_n(5)))._(x)._(sum._(x)._(a))))) ._(Zero) ._(infinite))) .toBe(15); }); }); describe('foldrz', () => { it('does not fold Nil', () => { expect(() => foldrz._(sum)._(Nil)).not.toThrow(); expect(() => $n(foldrz._(sum)._(Nil))).toThrow(); }); it.failing('folds single item', () => { expect(foldrz._(sum)).toDeferEvaluationOf(_na([1])); expect($n(foldrz._(sum)._(_na([1])))).toBe(1); }); it.failing('folds several items', () => { expect(foldrz._(sum)).toDeferEvaluationOf(_na([1, 2])); expect($n(foldrz._(sum)._(_na([1, 2])))).toBe(3); }); it.failing('handles infinite lists', () => { expect($n( foldrz ._(_(x => _(a => iif._(eqn._(x)._(_n(5)))._(x)._(sum._(x)._(a))))) ._(infinite))) .toBe(15); }); }); describe('eq', () => { it('Nil eq Nil is True', () => { expect(eq).toDeferEvaluationOf(eqn, _na([]), _na([])); expect($b(eq._(eqn)._(_na([]))._(_na([])))).toEqual(true); }); it('Nil eq [0, 1, 2] is False', () => { expect(eq).toDeferEvaluationOf(eqn, _na([]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([]))._(_na([0, 1, 2])))).toEqual(false); }); it('[0, 1, 2] eq Nil is False', () => { expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([])))).toEqual(false); }); it('[0] eq [0, 1, 2] is False', () => { expect(eq).toDeferEvaluationOf(eqn, _na([0]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([0]))._(_na([0, 1, 2])))).toEqual(false); }); it('[0, 1, 2] eq [0] is False', () => { expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([0])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([0])))).toEqual(false); }); it('[1, 2, 0] eq [0, 1, 2] is False', () => { expect(eq).toDeferEvaluationOf(eqn, _na([1, 2, 0]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([1, 2, 0]))._(_na([0, 1, 2])))).toEqual(false); }); it('[0, 1, 2] eq [1, 2, 0] is False', () => { expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([1, 2, 0])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([1, 2, 0])))).toEqual(false); }); it('[0, 1, 2] eq [0, 1, 2] is True', () => { expect(eq).toDeferEvaluationOf(eqn, _na([0, 1, 2]), _na([0, 1, 2])); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(_na([0, 1, 2])))).toEqual(true); }); it('handles infinite lists', () => { expect($b(eq._(eqn)._(infinite)._(_na([0, 1, 2])))).toEqual(false); expect($b(eq._(eqn)._(_na([0, 1, 2]))._(infinite))).toEqual(false); }); }); describe('map', () => { it('maps Nil', () => { expect(map._(succ)).toDeferEvaluationOf(_na([])); expect($na(map._(succ)._(_na([])))).toEqual([]); }); it('maps only item', () => { expect(map._(succ)).toDeferEvaluationOf(_na([0])); expect($na(map._(succ)._(_na([0])))).toEqual([1]); }); it('maps several items', () => { expect(map._(succ)).toDeferEvaluationOf(_na([0, 1])); expect($na(map._(succ)._(_na([0, 1])))).toEqual([1, 2]); }); it('handles infinite lists', () => { expect($n(head._(map._(succ)._(infinite)))).toBe(1); }); }); describe('filter', () => { it('filter Nil', () => { expect(filter._(odd)).toDeferEvaluationOf(_na([])); expect($na(filter._(odd)._(_na([])))).toEqual([]); }); it('filters only item', () => { expect(filter._(odd)).toDeferEvaluationOf(_na([0])); expect($na(filter._(odd)._(_na([])))).toEqual([]); }); it('filters several items', () => { expect(filter._(odd)).toDeferEvaluationOf(_na([0, 1])); expect($na(filter._(odd)._(_na([0, 1])))).toEqual([1]); }); it('handles infinite lists', () => { expect($n(head._(filter._(odd)._(infinite)))).toBe(1); }); }); describe('any', () => { it('any Nil is False', () => { expect(any._(odd)).toDeferEvaluationOf(_na([])); expect($b(any._(odd)._(_na([])))).toEqual(false); }); it('any for no matching item is False', () => { expect(any._(odd)).toDeferEvaluationOf(_na([0, 2])); expect($b(any._(odd)._(_na([0, 2])))).toEqual(false); }); it('any for matching item is True', () => { expect(any._(odd)).toDeferEvaluationOf(_na([0, 1, 2])); expect($b(any._(odd)._(_na([0, 1, 2])))).toEqual(true); }); it('handles infinite lists', () => { expect($b(any._(odd)._(infinite))).toEqual(true); }); }); describe('all', () => { it('all Nil is True', () => { expect(all._(even)).toDeferEvaluationOf(_na([])); expect($b(all._(even)._(_na([])))).toEqual(true); }); it('all for non-matching item is False', () => { expect(all._(even)).toDeferEvaluationOf(_na([0, 1, 2])); expect($b(all._(even)._(_na([0, 1, 2])))).toEqual(false); }); it('all for no non-matching item is True', () => { expect(all._(even)).toDeferEvaluationOf(_na([0, 2])); expect($b(all._(even)._(_na([0, 2])))).toEqual(true); }); it('handles infinite lists', () => { expect($b(all._(even)._(infinite))).toEqual(false); }); }); });