diff --git a/src/list.ts b/src/list.ts index 8e67651..2dcf937 100644 --- a/src/list.ts +++ b/src/list.ts @@ -31,8 +31,13 @@ export const append: L<(l: List) => L<(r: List) => List>> export const concat: L<(ls: List>) => List> = _(ls => ls._(Nil)._(_(x => _(xs => append._(x)._(concat._(xs)))))); -export const repeat: L<(v: T) => List> - = _(v => cons._(v)._(repeat._(v))); +export const repeat: L<(x: T) => List> + = _(x => cons._(x)._(repeat._(x))); + +export const iterate: L<(f: L<(x: T) => T>) => L<(x: T) => List>> + = _((f: L<(x: T) => T>) => + _((x: T) => + cons._(x)._(iterate._(f)._(f._(x))))); export const head: L<(xs: List) => T> = _(xs => xs._(undef)._(_(x => _(_xs => x)))); diff --git a/src/string.ts b/src/string.ts index 3664845..a680e89 100644 --- a/src/string.ts +++ b/src/string.ts @@ -7,7 +7,7 @@ import { Cons, List, Nil, cmp as cmpl, concat, cons, eq as eql, ge as gel, gt as export type String = List; -export { Nil as Empty, nul as empty, len, Cons, cons, snoc, append, get, update, set } from './list'; +export { Nil as Empty, nul as empty, len, Cons, cons, snoc, append, repeat, iterate, get, update, set } from './list'; export const cmp: L<(l: String) => L<(r: String) => Ord>> = cmpl._(cmpc); diff --git a/test/list.spec.ts b/test/list.spec.ts index ec9a982..904f77d 100644 --- a/test/list.spec.ts +++ b/test/list.spec.ts @@ -4,12 +4,11 @@ import { toNOrd as $o, NOrd } from '../src/ord'; import { toBoolean as $b, iif } from '../src/bool'; import { toNumber as $n, toNumber as $nn, Num, Succ, Zero, fromNumber as _n, fromNumber as _nn, cmp as cmpn, eq as eqn, even, gt as ngt, show as nshow, odd, succ, sum } from '../src/num'; import { toChar as $c } from '../src/char'; -import { toArray as $a, Cons, List, Nil, fromArray as _a, all, any, append, cmp, concat, cons, eq, filter, foldl, foldlz, foldr, foldrz, ge, get, gt, head, intersperse, le, len, lt, map, nul, set, show, snoc, tail, update } from '../src/list'; +import { toArray as $a, Cons, List, Nil, fromArray as _a, all, any, append, cmp, concat, cons, eq, filter, foldl, foldlz, foldr, foldrz, ge, get, gt, head, intersperse, iterate, le, len, lt, map, nul, set, show, snoc, tail, update } from '../src/list'; import { toString as $s } from '../src/string'; describe('List', () => { - const iterate: (n: Num) => List = n => new L(() => Cons._(n)._(iterate(Succ._(n))).value); - const infinite: List = iterate(Zero); + const infinite: List = iterate._(Succ)._(Zero); const _na: (xs: number[]) => List = xs => xs.length === 0 ? Nil : Cons._(_nn(xs[0]))._(new L(() => _na(xs.slice(1)).value)); @@ -163,6 +162,15 @@ describe('List', () => { // }); // }); + // describe('iterate', () => { + // it('iterate succ 0 is [0, 1, 2, ...]', () => { + // expect($na( + // iterate._(_n(0))._>(_((x0: Num) => _((x1s: List) => + // x1s._>(_((x1: Num) => _((x2s: List) => + // x2s._>(_((x2: Num) => _((_xrs: List): List => cons._(x0)._(cons._(x1)._(cons._(x2)._(Nil)))))))))))))).toEqual([0, 1, 2]); + // }); + // }); + describe('head', () => { it('returns nothing from Nil', () => { expect(() => $$(head._(Nil))).toThrow(); diff --git a/test/string.spec.ts b/test/string.spec.ts index 887e749..c3c0985 100644 --- a/test/string.spec.ts +++ b/test/string.spec.ts @@ -37,6 +37,25 @@ describe('String', () => { }); }); + // describe('repeat', () => { + // it('repeat "a" is "aaa..."', () => { + // expect($na( + // repeat._(_s('a'))._(_((x0: Char) => _((x1s: String) => + // x1s._(_((x1: Char) => _((x2s: String) => + // x2s._(_((x2: Char) => _((_xrs: String): String => cons._(x0)._(cons._(x1)._(cons._(x2)._(Nil)))))))))))))).toEqual('aaa'); + // }); + // }); + + // describe('iterate', () => { + // it('iterate succ "a" is "abc..."', () => { + // expect($na( + // iterate._(_s('a'))._(_((x0: Char) => _((x1s: String) => + // x1s._(_((x1: Char) => _((x2s: String) => + // x2s._(_((x2: Char) => _((_xrs: String): String => cons._(x0)._(cons._(x1)._(cons._(x2)._(Nil)))))))))))))).toEqual('abc'); + // }); + // }); + + describe('empty', () => { it('empty "" is True', () => { expect($b(empty._(_s('')))).toEqual(true);