tiinvo
Version:
A library of types and utilities for your TypeScript and JavaScript projects
718 lines (717 loc) • 19.6 kB
TypeScript
import type * as Fn from './Fn.js';
import type * as Functors from './Functors.js';
import * as Sequence from './Sequence.js';
declare const sortsymbol: unique symbol;
/**
* A sorted sequence is a `Sequence.t<a>` which all elements stored in it are sorted by a `Comparable<a>` functor.
*/
export type T<a> = Sequence.T<a> & {
[sortsymbol]: Functors.Comparable<a>;
};
/**
* Makes an immutable `SortedSequence.T<A>` from a `Comparable<a>` and (optionally) a list of arguments as initial values
*
* @example
*
* ```ts
* import { SortedSequence, Num, Str } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num.cmp, 10, 20, 30)
* const s1 = SortedSequence.make(Str.cmp, 'hello', 'world')
*
* SortedSequence.guardOf(Num.guard)(s0) // true
* SortedSequence.guardOf(Num.guard)(s1) // false
* SortedSequence.guardOf(Str.guard)(s0) // false
* SortedSequence.guardOf(Str.guard)(s1) // true
* ```
*
* @template A element's type
* @param mod the Comparable functor
* @param args a list of initial values
* @returns the SortedSequence
* @group Factories
* @since 4.0.0
*/
export declare function make<A>(mod: Functors.Comparable<A>, ...args: A[]): T<A>;
/**
* Makes an immutable `SortedSequence.T<A>` from a `ComparableModule<a>` and (optionally) a list of arguments as initial values
*
* @example
*
* ```ts
* import { SortedSequence, Num, Str } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 10, 20, 30)
* const s1 = SortedSequence.make(Str, 'hello', 'world')
*
* SortedSequence.guardOf(Num.guard)(s0) // true
* SortedSequence.guardOf(Num.guard)(s1) // false
* SortedSequence.guardOf(Str.guard)(s0) // false
* SortedSequence.guardOf(Str.guard)(s1) // true
* ```
*
* @template A element's type
* @param mod the Comparable module functor
* @param args a list of initial values
* @returns the SortedSequence
* @group Factories
* @since 4.0.0
*/
export declare function make<A>(mod: Functors.ComparableModule<A>, ...args: A[]): T<A>;
/**
* Checks if the parameter `x` is a `SortedSequence.t<unknown>`
*
* @example
*
* ```ts
* import { SortedSequence, Str } from 'tiinvo'
*
* const s = SortedSequence.make(Str)
*
* SortedSequence.guard(s) // true
* SortedSequence.guard([]) // false
* ```
*
* @param x the value to guard
* @returns
* - `true` if x is a `SortedSequence.T<unknown>`
* - `false` otherwise
* @group Guards
* @since 4.0.0
*/
export declare const guard: (x: unknown) => x is T<unknown>;
/**
* Checks if the parameter `x` is a `SortedSequence.T<A>` using a `Guardable<A>` functor.
*
* @example
*
* ```ts
* import { SortedSequence, Num, Str } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 1, 2)
* const s1 = SortedSequence.make(Str, 'hello', 'world')
*
* SortedSequence.guardOf(Str.guard, s0) // false
* SortedSequence.guardOf(Str.guard, s1) // true
* ```
*
* @template A the expected sequence elements type
* @param g the elements guard
* @param x the value to check
* @returns
* - `true` if `x` is a `SortedSequence.T<A>`
* - `false` otherwise
* @group Guardables
* @since 4.0.0
*/
export declare function guardOf<A>(g: Functors.Guardable<A>, x: unknown): x is T<A>;
/**
* Checks if the parameter `x` is a `SortedSequence.T<A>` using a `GuardableModule<A>` functor.
*
* @example
*
* ```ts
* import { SortedSequence, Num, Str } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 1, 2)
* const s1 = SortedSequence.make(Str, 'hello', 'world')
*
* SortedSequence.guardOf(Str, s0) // false
* SortedSequence.guardOf(Str, s1) // true
* ```
*
* @template A the expected sequence elements type
* @param g the elements guard module
* @param x the value to check
* @returns
* - `true` if `x` is a `SortedSequence.T<A>`
* - `false` otherwise
* @group Guardables
* @since 4.0.0
*/
export declare function guardOf<A>(g: Functors.GuardableModule<A>, x: unknown): x is T<A>;
/**
* Returns a guard which checks if the parameter `x` is a `SortedSequence.T<A>`
*
* @example
*
* ```ts
* import { SortedSequence, Num, Str } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 1, 2)
* const s1 = SortedSequence.make(Str, 'hello', 'world')
* const isStrSortedList = SortedSequence.guardOf(Str.guard);
*
* isStrSortedList(s0) // false
* isStrSortedList(s1) // true
* ```
*
* @template A the expected sequence elements type
* @param g the elements guard
* @returns the guard which takes an argument `y` and returns
* - `true` if `y` is a `SortedSequence.T<A>`
* - `false` otherwise
* @group Guardables
* @since 4.0.0
*/
export declare function guardOf<A>(g: Functors.Guardable<A>): (x: unknown) => x is T<A>;
/**
* Returns a guard which checks if the parameter `x` is a `SortedSequence.T<A>`
*
* @example
*
* ```ts
* import { SortedSequence, Num, Str } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 1, 2)
* const s1 = SortedSequence.make(Str, 'hello', 'world')
* const isStrSortedList = SortedSequence.guardOf(Str);
*
* isStrSortedList(s0) // false
* isStrSortedList(s1) // true
* ```
*
* @template A the expected sequence elements type
* @param g the elements guard
* @returns the guard which takes an argument `y` and returns
* - `true` if `y` is a `SortedSequence.T<A>`
* - `false` otherwise
* @group Guardables
* @since 4.0.0
*/
export declare function guardOf<A>(g: Functors.GuardableModule<A>): (x: unknown) => x is T<A>;
/**
* Compares two `SortedSequence.T<A>`.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s1 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s2 = SortedSequence.make<number>(Num, 0, 1, 2, 3)
*
* SortedSequence.cmp(s0, s1) // 0
* SortedSequence.cmp(s0)(s1) // 0
*
* SortedSequence.cmp(s0, s2) // -1
* SortedSequence.cmp(s0)(s2) // -1
*
* SortedSequence.cmp(s2, s0) // 1
* SortedSequence.cmp(s2)(s0) // 1
* ```
*
* @template A SortedSequence element's type
* @param a the first sequence
* @param b the last sequence
* @returns
* - -1 if `a` is less than `b`
* - 0 if `a` equals `b`
* - 1 if `a` is greater than `b`
* @group Comparables
* @since 4.0.0
*/
export declare function cmp<A extends T<any>>(a: A, b: A): Functors.ComparableResult;
/**
* Returns a unary function which compares two `SortedSequence.T<A>`.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s1 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s2 = SortedSequence.make<number>(Num, 0, 1, 2, 3)
* const s3 = SortedSequence.make<number>(Num, 0, 1)
*
* const cmp0 = SortedSequence.cmp(s0);
*
* cmp0(s1) // 0
* cmp0(s2) // 1
* cmp0(s3) // -1
* ```
*
* @template A `SortedSequence.T` type
* @param a the first sequence
* @returns the unary function which returns
* - -1 if `b` is less than `a`
* - 0 if `b` equals `a`
* - 1 if `b` is greater than `a`
* @group Comparables
* @since 4.0.0
*/
export declare function cmp<A extends T<any>>(a: A): Fn.Unary<A, Functors.ComparableResult>;
/**
* Checks if two sorted lists are equal
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s1 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s2 = SortedSequence.make<number>(Num, 0, 1, 2, 3)
*
* SortedSequence.eq(s0, s1) // true
* SortedSequence.eq(s0)(s1) // true
*
* SortedSequence.eq(s0, s2) // false
* SortedSequence.eq(s0)(s2) // false
* ```
* @template A `SortedSequence.T` type
* @param a the first sequence
* @param b the second sequence
* @returns
* - `true` if `b` equals `a`
* - `false` otherwise
* @group Comparables
* @since 4.0.0
*/
export declare function eq<A extends T<any>>(a: A, b: A): boolean;
/**
* Returns a unary function which checks if two sorted lists are equal
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s1 = SortedSequence.make<number>(Num, 0, 1, 2)
* const s2 = SortedSequence.make<number>(Num, 0, 1, 2, 3)
*
* SortedSequence.eq(s0, s1) // true
* SortedSequence.eq(s0)(s1) // true
*
* SortedSequence.eq(s0, s2) // false
* SortedSequence.eq(s0)(s2) // false
* ```
* @template A `SortedSequence.T` type
* @param a the first sequence
* @returns the unary function which returns
* - `true` if `b` equals `a`
* - `false` otherwise
* @group Comparables
* @since 4.0.0
*/
export declare function eq<A extends T<any>>(a: A): Fn.Unary<A, boolean>;
/**
* Maps a `SortedSequence.T<A>` to a `SortedSequence.t<b>` using the functor `Functors.Mappable<a, b>`.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s = SortedSequence.make<number>(Num, 3, 1, 2)
* const m = Num.mul(2)
*
* SortedSequence.map(s, m) // SortedSequence.t(2, 4, 6)
* SortedSequence.map(m)(s) // SortedSequence.t(2, 4, 6)
* ```
*
* @template A the SortedSequence element's type
* @template B the mapped SortedSequence element's type
* @param a the SortedSequence
* @param m the Mappable functor
* @returns the mapped `SortedSequence.T<B>`
* @group Mappables
* @since 4.0.0
*/
export declare function map<A, B>(a: T<A>, m: Functors.Mappable<A, B>): T<B>;
/**
* Returns a unary function which maps a `SortedSequence.T<A>` to
* a `SortedSequence.t<b>` using the functor `Functors.Mappable<a, b>`.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const m = SortedSequence.map(Num.mul(2))
*
* m(SortedSequence.make<number>(Num, 3, 1, 2)) // SortedSequence.t(2, 4, 6)
* m(SortedSequence.make<number>(Num, 9, 4, 8)) // SortedSequence.t(8, 16, 18)
* ```
*
* @template A the SortedSequence element's type
* @template B the mapped SortedSequence element's type
* @param a the SortedSequence
* @returns the unary function which maps `SortedSequence.T<A>` to `SortedSequence.T<B>`
* @group Mappables
* @since 4.0.0
*/
export declare function map<A, B>(a: Functors.Mappable<A, B>): Fn.Unary<T<A>, T<B>>;
/**
* Adds an element to the end of the `Sequence.t<a>` without mutating the original one.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 10, 20)
*
* SortedSequence.add(s0, 30) // SortedSequence(10, 20, 30)
* SortedSequence.add(30)(s0) // SortedSequence(10, 20, 30)
* ```
*
* @template A the SortedSequence type
* @template B the added value type
* @param a the SortedSequence
* @param b the added value
* @returns the new SortedSequence
* @group Operables
* @since 4.0.0
*/
export declare function add<A extends T<any>, B>(a: A, b: B): T<A & B>;
/**
* Returns a unary function which adds an element to the end of the `Sequence.t<a>` without mutating the original one.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 10, 20)
*
* SortedSequence.add(s0, 30) // SortedSequence(10, 20, 30)
* SortedSequence.add(30)(s0) // SortedSequence(10, 20, 30)
* ```
*
* @template A the SortedSequence type
* @template B the added value type
* @param a the added value type
* @returns the unary function
* @group Operables
* @since 4.0.0
*/
export declare function add<A extends T<any>, B>(a: B): Fn.Unary<A, T<A & B>>;
/**
* Concatenates two `SortedSequence.T<A>` and `SortedSequence.T<A>`
* and return a new `SortedSequence.T<A>`.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make<number>(Num, 10, 20)
* const s1 = SortedSequence.make<number>(Num, 30, 40)
*
* SortedSequence.concat(s0, s1) // SortedSequence(10, 20, 30, 40)
* SortedSequence.concat(s1)(s0) // SortedSequence(10, 20, 30, 40)
* ```
*
* @template A the SortedSequence type
* @param a the first SortedSequence
* @param b the second SortedSequence
* @returns the concatenated SortedSequence
* @group Operables
* @since 4.0.0
*/
export declare function concat<A>(a: T<A>, b: T<A>): T<A>;
/**
* Returns a unary function which concatenates two `SortedSequence.T<A>`
* and `SortedSequence.T<A>` and return a new `SortedSequence.T<A>`.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make<number>(Num, 10, 20)
* const s1 = SortedSequence.make<number>(Num, 30, 40)
*
* SortedSequence.concat(s1)(s0) // SortedSequence(10, 20, 30, 40)
* ```
*
* @template A the SortedSequence type
* @param a the second SortedSequence
* @returns the unary function
* @group Operables
* @since 4.0.0
*/
export declare function concat<A>(a: T<A>): <A>(x: T<A>) => T<A>;
/**
* Counts the number of elements that satisfy a given predicate
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s = SortedSequence.make(Num, 10, 20, 30)
*
* SortedSequence.count(s, Num.gt(10)) // 2
* SortedSequence.count(Num.gt(10))(s) // 2
* ```
*
* @template A the Sequence's element type
* @param a the sequence
* @param p the Filterable functor
* @returns the number of elements which satisfy the predicate `p`
* @group Accessors
* @since 4.0.0
*/
export declare const count: typeof Sequence.count;
/**
* Gets an element at a specific index.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s = SortedSequence.make(Num, 'hello', 'world')
*
* SortedSequence.get(s, 0) // 'hello'
* SortedSequence.get(s, 9) // null
* ```
*
* @template A the Sequence's element type
* @param a the sequence
* @param i the index of the element
* @returns
* - `Result.Ok<A>` if `i` is in bound
* - `Result.Err` if `i` is out of bound or negative
* @group Accessors
* @since 4.0.0
*/
export declare const get: typeof Sequence.get;
/**
* Gets first element if any
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 10, 20, 30)
* const s1 = SortedSequence.make(Num)
*
* SortedSequence.first(s0) // 10
* SortedSequence.first(s1) // null
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the first element of the sequence:
* - `Option.Some<A>` if the sequence has at least one element
* - `Option.None` otherwise
* @group Accessors
* @since 4.0.0
*/
export declare const first: <A>(t: Sequence.T<A>) => import("./Option.js").T<A>;
/**
* Gets last element if any.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s0 = SortedSequence.make(Num, 10, 20, 30)
* const s1 = SortedSequence.make(Num)
*
* SortedSequence.last(s0) // 30
* SortedSequence.last(s1) // null
*
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns
* - `Option.Some<A>` if the sequence is not empty
* - `Option.None` otherwise
* @group Accessors
* @since 4.0.0
*/
export declare const last: <A>(t: Sequence.T<A>) => import("./Option.js").T<A>;
/**
* Gets the length of a `SortedSequence.T<A>`
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s = SortedSequence.make(Num, 1, 2, 3)
*
* SortedSequence.length(s) // 3
* ```
*
* @since 4.0.0
*/
export declare const length: <A>(t: Sequence.T<A>) => number;
/**
* Gets values of a `SortedSequence.T<A>` as an immutable indexed object.
*
* @example
*
* ```ts
* import { SortedSequence, Str } from 'tiinvo'
*
* const s = SortedSequence.make(Str, 'hello', 'world')
*
* SortedSequence.values(s) // { 0: 'hello', 1: 'world' }
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the sequence values as an immutable dictionary
* @group Accessors
* @since 4.0.0
*/
export declare const values: <A>(t: Sequence.T<A>) => Record<number, A>;
/**
* Returns `true` if the sorted list is empty.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s = SortedSequence.make(Num)
* const s1 = SortedSequence.make(Num, 10)
*
* SortedSequence.empty(s) // true
* SortedSequence.empty(s1) // false
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns
* - 'true' if the sequence is empty
* - 'false' otherwise
* @group Predicates
* @since 4.0.0
*/
export declare const empty: <A>(t: Sequence.T<A>) => boolean;
/**
* Returns `true` if the sorted list is populated.
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const s = SortedSequence.make(Num, 10, 20, 30)
*
* SortedSequence.populated(s) // true
* SortedSequence.populated(SortedSequence.make(Num)) // false
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns
* - 'true' if the sequence is populated
* - 'false' otherwise
* @group Predicates
* @since 4.0.0
*/
export declare const populated: <a>(t: Sequence.T<a>) => boolean;
/**
* Converts a `SortedSequence.T<A>` to an array of `a[]`
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const sl = SortedSequence.make(Num, 3, 2, 1)
*
* SortedSequence.toArray(sl) // [1, 2, 3]
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the output
* @group Serializables
* @since 4.0.0
*/
export declare const toArray: <A>(t: Sequence.T<A>) => A[];
/**
* Converts a `SortedSequence.T<A>` to a jsonizable value
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const sl = SortedSequence.make(Num, 3, 2, 1)
*
* SortedSequence.toJSON(sl) // [1, 2, 3]
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the output
* @group Serializables
* @since 4.0.0
*/
export declare const toJSON: <a>(t: Sequence.T<a>) => a[];
/**
* Converts a `SortedSequence.T<A>` to a set of `Set<a>`
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const sl = SortedSequence.make(Num, 3, 2, 1)
*
* SortedSequence.toMap(sl) // Map([0, 1], [1, 2], [2, 3])
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the output
* @group Serializables
* @since 4.0.0
*/
export declare const toMap: <A>(t: Sequence.T<A>) => Map<string, A>;
/**
* Converts a `SortedSequence.T<A>` to a set of `Set<a>`
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const sl = SortedSequence.make(Num, 3, 2, 1)
*
* SortedSequence.toSet(sl) // Set(1, 2, 3)
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the output
* @group Serializables
* @since 4.0.0
*/
export declare const toSet: <A>(t: Sequence.T<A>) => Set<A>;
/**
* Converts a `SortedSequence.T<A>` to a string
*
* @example
*
* ```ts
* import { SortedSequence, Num } from 'tiinvo'
*
* const sl = SortedSequence.make(Num, 3, 2, 1)
*
* SortedSequence.toString(sl) // "1,2,3"
* ```
*
* @template A the Sequence's element type
* @param t the sequence
* @returns the output
* @group Serializables
* @since 4.0.0
*/
export declare const toString: <A>(t: Sequence.T<A>) => string;
export {};