More library functions

This commit is contained in:
Freywar Ulvnaudgari
2024-08-05 14:57:46 +03:00
parent 9d2bb159e6
commit a1663cbde0
11 changed files with 359 additions and 12 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
import { L, P, _, fromNative, toNative } from './core';
import { toNative as $n, L, P, _, fromNative as _n } from './core';
export type Bool = L<<T extends P>(t: T) => L<(f: T) => T>>;
@@ -30,4 +30,4 @@ export const fromBoolean: (b: boolean) => Bool
= b => b ? True : False;
export const toBoolean: (b: Bool) => boolean
= b => toNative(b._(fromNative(true))._(fromNative(false)));
= b => $n(b._(_n(true))._(_n(false)));
+18
View File
@@ -0,0 +1,18 @@
import { L, _ } from './core';
import { Bool } from './bool';
import { toNumber as $n, Num, Zero, fromNumber as _n, eq as eqn } from './num';
export type Char = Num;
export const Null: Char
= Zero;
export const eq: L<(l: Char) => L<(r: Char) => Bool>>
= eqn;
export const fromChar: (c: string) => Char
= c => _n(c.charCodeAt(0));
export const toChar: (c: Char) => string
= c => String.fromCharCode($n(c));
+2
View File
@@ -2,3 +2,5 @@ export * from './core';
export * from './bool';
export * from './num';
export * from './list';
export * from './char';
export * from './pair';
+8 -5
View File
@@ -1,6 +1,6 @@
import { L, P, _, fromNative, toNative, undef } from './core';
import { Num, fromNumber, toNumber } from './num';
import { toNative as $n, L, P, _, fromNative as _n, undef } from './core';
import { Bool, False, True, and, iif, or } from './bool';
import { toNumber as $nn, Num, fromNumber as _nn } from './num';
export type List<T extends P> = L<<R extends P>(z: R) => L<(f: L<(x: T) => L<(xs: List<T>) => R>>) => R>>;
@@ -13,6 +13,9 @@ export const Cons: L<<T extends P>(x: T) => L<(xs: List<T>) => List<T>>>
export const cons: L<<T extends P>(x: T) => L<(xs: List<T>) => List<T>>>
= Cons;
export const concat: L<<T extends P>(l: List<T>) => L<(r: List<T>) => List<T>>>
= _(l => _(r => l._(r)._(_(x => _(xs => cons._(x)._(concat._(xs)._(r)))))));
export const head: L<<T extends P>(xs: List<T>) => T>
= _(xs => xs._(undef)._(_(x => _(_xs => x))));
@@ -74,10 +77,10 @@ export const fromArray: <T extends P>(xs: T[]) => List<T>
= xs => xs.length === 0 ? Nil : Cons._(xs[0])._(new L(() => fromArray(xs.slice(1)).value));
export const fromNumberArray: (xs: number[]) => List<Num>
= xs => xs.length === 0 ? Nil : Cons._(fromNumber(xs[0]))._(new L(() => fromNumberArray(xs.slice(1)).value));
= xs => xs.length === 0 ? Nil : Cons._(_nn(xs[0]))._(new L(() => fromNumberArray(xs.slice(1)).value));
export const toArray: <T extends P>(xs: List<T>) => P[]
= xs => toNative(xs._(fromNative([]))._(_(x => _(xs => fromNative([x, ...toArray(xs)])))));
= xs => $n(xs._(_n([]))._(_(x => _(xs => _n([x, ...toArray(xs)])))));
export const toNumberArray: (xs: List<Num>) => number[]
= xs => toNative(xs._(fromNative([]))._(_(x => _(xs => fromNative([toNumber(x), ...toNumberArray(xs)])))));
= xs => $n(xs._(_n([]))._(_(x => _(xs => _n([$nn(x), ...toNumberArray(xs)])))));
+2 -2
View File
@@ -1,5 +1,5 @@
import { toNative as $n, L, P, _, fromNative as _n, id } from './core';
import { Bool, False, True } from './bool';
import { L, P, _, fromNative, id, toNative } from './core';
export type Num = L<<T extends P>(z: T) => L<(n: L<(p: Num) => T>) => T>>;
@@ -42,4 +42,4 @@ export const fromNumber: (n: number) => Num
= n => !n ? Zero : Succ._(new L(() => fromNumber(n - 1).value));
export const toNumber: (n: Num) => number
= n => toNative(n._(fromNative(0))._(_(p => fromNative(1 + toNumber(p)))));
= n => $n(n._(_n(0))._(_(p => _n(1 + toNumber(p)))));
+31
View File
@@ -0,0 +1,31 @@
import { L, P, _ } from './core';
export type Pair<F extends P, S extends P> = L<<R extends P>(f: L<(f: F) => L<(s: S) => R>>) => R>;
export const Pair: L<<F extends P, S extends P>(f: F) => L<(s: S) => Pair<F, S>>>
=
_(<F extends P, S extends P>(f: F) =>
_((s: S) =>
_(<R extends P>(p: L<(f: F) => L<(s: S) => R>>) => p._(f)._(s))));
export const fst: L<<F extends P, S extends P>(p: Pair<F, S>) => F>
= _(p => p._(_(f => _(_s => f))));
export const snd: L<<F extends P, S extends P>(p: Pair<F, S>) => S>
= _(p => p._(_(_f => _(s => s))));
export const first: L<<F extends P, FR extends P, S extends P>(t: L<(f: F) => FR>) => L<(p: Pair<F, S>) => Pair<FR, S>>>
=
_(<F extends P, FR extends P>(t: L<(f: F) => FR>) =>
_(<S extends P>(p: Pair<F, S>) => p._(_(f => _(s => Pair._(t._(f))._(s))))));
export const second: L<<F extends P, S extends P, SR extends P>(t: L<(s: S) => SR>) => L<(p: Pair<F, S>) => Pair<F, SR>>>
=
_(<S extends P, SR extends P>(t: L<(s: S) => SR>) =>
_(<F extends P>(p: Pair<F, S>) => p._(_(f => _(s => Pair._(f)._(t._(s)))))));
export const both: L<<F extends P, FR extends P, S extends P, SR extends P>(tf: L<(f: F) => FR>) => L<(ts: L<(s: S) => SR>) =>L<(p: Pair<F, S>) => Pair<FR, SR>>>>
=
_(<F extends P, FR extends P>(tf: L<(f: F) => FR>) =>
_(<S extends P, SR extends P>(ts: L<(s: S) => SR>) =>
_((p: Pair<F, S>) => p._(_(f => _(s => Pair._(tf._(f))._(ts._(s))))))));
+28
View File
@@ -0,0 +1,28 @@
import { toNative as $n, L, _, fromNative as _n } from './core';
import { Bool } from './bool';
import { fromNumber as _nn } from './num';
import { toChar as $c, Char, fromChar as _c, eq as eqc } from './char';
import { Cons as ConsL, List, Nil, concat as concatl, eq as eql } from './list';
export type String = List<Char>;
export const Empty: String
= Nil;
export const Cons: L<(c: Char) => L<(s: String) => String>>
= ConsL;
export const cons: L<(c: Char) => L<(s: String) => String>>
= Cons;
export const concat: L<(l: String) => L<(r: String) => String>>
= concatl;
export const eq: L<(l: String) => L<(r: String) => Bool>>
= eql._(eqc);
export const fromString: (xs: string) => String
= xs => xs.length === 0 ? Nil : Cons._(_c(xs[0]))._(new L(() => fromString(xs.slice(1)).value));
export const toString: (xs: String) => string
= xs => $n(xs._(_n(''))._(_(x => _(xs => _n($c(x) + toString(xs))))));