UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

92 lines (91 loc) 3.38 kB
/** * list.ts: Supporting lists in the Scheme style, using pairs made * up of two-element JavaScript array (vector) * @author: Martin Henz * Translated to TypeScript by Evan Sebastian */ import type { Value } from '../types'; export type Pair<H, T> = [H, T]; export type List<T = unknown> = null | NonEmptyList<T>; type NonEmptyList<T> = Pair<T, any>; /** * constructs a pair using a two-element array\ * LOW-LEVEL FUNCTION, NOT SOURCE */ export declare function pair<H, T>(x: H, xs: T): Pair<H, T>; /** * returns true iff arg is a two-element array\ * LOW-LEVEL FUNCTION, NOT SOURCE */ export declare function is_pair(x: unknown): x is Pair<unknown, unknown>; /** * returns the first component of the given pair\ * LOW-LEVEL FUNCTION, NOT SOURCE * @throws an exception if the argument is not a pair */ export declare function head<T>(xs: Pair<T, unknown> | NonEmptyList<T>): T; export declare function head(xs: unknown): unknown; /** * returns the second component of the given pair\ * LOW-LEVEL FUNCTION, NOT SOURCE * @throws an exception if the argument is not a pair */ export declare function tail<T>(xs: NonEmptyList<T>): List<T>; export declare function tail<T>(xs: Pair<unknown, T>): T; export declare function tail(xs: unknown): unknown; /** * returns true if arg is exactly null\ * LOW-LEVEL FUNCTION, NOT SOURCE */ export declare function is_null(xs: unknown): xs is null; /** * makes a list out of its arguments\ * LOW-LEVEL FUNCTION, NOT SOURCE */ export declare function list<T>(...elements: T[]): List<T>; /** * recurses down the list and checks that it ends with the empty list null\ * LOW-LEVEL FUNCTION, NOT SOURCE */ export declare function is_list(xs: unknown): xs is List; /** * returns vector that contains the elements of the argument list * in the given order.\ * LOW-LEVEL FUNCTION, NOT SOURCE * @throws an exception if the argument is not a list */ export declare function list_to_vector<T>(lst: List<T>): T[]; /** * returns a list that contains the elements of the argument vector * in the given order\ * LOW-LEVEL FUNCTION, NOT SOURCE * @throws an exception if the argument is not a vector */ export declare function vector_to_list<T>(vector: T[]): List<T>; /** * changes the head of given pair xs to be x\ * LOW-LEVEL FUNCTION, NOT SOURCE * @throws an exception if the argument is not a pair */ export declare function set_head(xs: unknown, x: any): void; /** * changes the tail of given pair xs to be x\ * LOW-LEVEL FUNCTION, NOT SOURCE * @throws an exception if the argument is not a pair */ export declare function set_tail(xs: unknown, x: any): void; /** * Accumulate applies given operation op to elements of a list * in a right-to-left order, first apply op to the last element * and an initial element, resulting in r1, then to the second-last * element and r1, resulting in r2, etc, and finally to the first element * and r_n-1, where n is the length of the list. `accumulate(op,zero,list(1,2,3))` * results in `op(1, op(2, op(3, zero)))` */ export declare function accumulate<T, U>(op: (each: T, result: U) => U, initial: U, sequence: List<T>): U; /** * returns the length of a List xs. Throws an exception if xs is not a List */ export declare function length(xs: unknown): number; export declare function rawDisplayList(display: any, xs: Value, prepend: string): any; export {};