More library functions
This commit is contained in:
+7
-2
@@ -31,8 +31,13 @@ export const append: L<<T extends P>(l: List<T>) => L<(r: List<T>) => List<T>>>
|
||||
export const concat: L<<T extends P>(ls: List<List<T>>) => List<T>>
|
||||
= _(ls => ls._(Nil)._(_(x => _(xs => append._(x)._(concat._(xs))))));
|
||||
|
||||
export const repeat: L<<T extends P>(v: T) => List<T>>
|
||||
= _(v => cons._(v)._(repeat._(v)));
|
||||
export const repeat: L<<T extends P>(x: T) => List<T>>
|
||||
= _(x => cons._(x)._(repeat._(x)));
|
||||
|
||||
export const iterate: L<<T extends P>(f: L<(x: T) => T>) => L<(x: T) => List<T>>>
|
||||
= _(<T extends P>(f: L<(x: T) => T>) =>
|
||||
_((x: T) =>
|
||||
cons._(x)._(iterate._(f)._(f._(x)))));
|
||||
|
||||
export const head: L<<T extends P>(xs: List<T>) => T>
|
||||
= _(xs => xs._(undef)._(_(x => _(_xs => x))));
|
||||
|
||||
+1
-1
@@ -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<Char>;
|
||||
|
||||
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);
|
||||
|
||||
+11
-3
@@ -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<Num> = n => new L(() => Cons._(n)._(iterate(Succ._(n))).value);
|
||||
const infinite: List<Num> = iterate(Zero);
|
||||
const infinite: List<Num> = iterate._(Succ)._(Zero);
|
||||
|
||||
const _na: (xs: number[]) => List<Num>
|
||||
= 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))._<List<Num>>(_((x0: Num) => _((x1s: List<Num>) =>
|
||||
// x1s._<List<Num>>(_((x1: Num) => _((x2s: List<Num>) =>
|
||||
// x2s._<List<Num>>(_((x2: Num) => _((_xrs: List<Num>): List<Num> => cons._(x0)._(cons._(x1)._(cons._(x2)._(Nil)))))))))))))).toEqual([0, 1, 2]);
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('head', () => {
|
||||
it('returns nothing from Nil', () => {
|
||||
expect(() => $$(head._(Nil))).toThrow();
|
||||
|
||||
@@ -37,6 +37,25 @@ describe('String', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// describe('repeat', () => {
|
||||
// it('repeat "a" is "aaa..."', () => {
|
||||
// expect($na(
|
||||
// repeat._(_s('a'))._<String>(_((x0: Char) => _((x1s: String) =>
|
||||
// x1s._<String>(_((x1: Char) => _((x2s: String) =>
|
||||
// x2s._<String>(_((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'))._<String>(_((x0: Char) => _((x1s: String) =>
|
||||
// x1s._<String>(_((x1: Char) => _((x2s: String) =>
|
||||
// x2s._<String>(_((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);
|
||||
|
||||
Reference in New Issue
Block a user