UNPKG

macoolka-collection

Version:

`macoolka-collection` Define Data Collection Interface.

215 lines (214 loc) 7.96 kB
/** * Subset Collection Interface * @desczh * 子集接口 * @file * @since 0.2.0 */ import { Kind, URIS } from 'fp-ts/HKT'; import { Option } from 'fp-ts/Option'; import { Predicate, Refinement } from 'fp-ts/function'; import { IndexWriterCollection } from './IndexWriterCollection'; export interface SubsetCollectionInput<URI extends URIS> { /** * Returns a new Collection of the same type representing a portion of this * Collection from start up to but not including end. * * If begin is negative, it is offset from the end of the Collection. e.g. * `slice(-2)` returns a Collection of the last two entries. If it is not * provided the new Collection will begin at the beginning of this Collection. * * If end is negative, it is offset from the end of the Collection. e.g. * `slice(0, -1)` returns a Collection of everything but the last entry. If * it is not provided, the new Collection will continue through the end of * this Collection. * @desczh * 从集合的开始位置到结尾位置(不包括结尾)返回一个新集合, * @example * import { slice } from 'fp-ts/slice' * * assert.deepStrictEqual(slice(1)([1, 2, 3]), [2, 3]) * assert.deepStrictEqual(slice([0,2]), [1, 2]) * * @since 0.5.0 */ slice: (begin: number, end?: number) => <A>(f: Kind<URI, A>) => Kind<URI, A>; } export interface Subset<F extends URIS> extends SubsetCollectionInput<F>, IndexWriterCollection<F> { /** * Get all but the first element of an list, creating a new list, or `None` if the list is empty * @desczh * 得到除了第一个元素以外的所有元素 * @example * import { tail } from 'fp-ts/List' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(tail([1, 2, 3]), some([2, 3])) * assert.deepStrictEqual(tail([]), none) * * @since 0.5.0 */ tail<A>(as: Kind<F, A>): Option<Kind<F, A>>; /** * Get all but the last element of an list, creating a new list, or `None` if the list is empty * @desczh * 得到除了最后一个元素以外的所有元素 * @example * import { head } from 'fp-ts/List' * import { some, none } from 'fp-ts/Option' * * assert.deepStrictEqual(head([1, 2, 3]), some([1, 2])) * assert.deepStrictEqual(head([]), none) * * @since 0.5.0 */ head<A>(as: Kind<F, A>): Option<Kind<F, A>>; /** * Keep only a number of elements from the start of an list, creating a new list. * `n` must be a natural number * @desczh * 得到从开始位置截取的指定数量的集合 * @example * import { takeLeft } from 'fp-ts/List' * * assert.deepStrictEqual(takeLeft(2)([1, 2, 3]), [1, 2]) * * @since 0.5.0 */ takeLeft(n: number): <A>(as: Kind<F, A>) => Kind<F, A>; /** * Keep only a number of elements from the end of an list, creating a new list. * `n` must be a natural number * @desczh * 得到从结尾位置截取的指定数量的集合 * @example * import { takeRight } from 'fp-ts/List' * * assert.deepStrictEqual(takeRight(2)([1, 2, 3, 4, 5]), [4, 5]) * * @since 0.5.0 */ takeRight(n: number): <A>(as: Kind<F, A>) => Kind<F, A>; /** * Returns a new Collection of the same type which includes entries from this Collection as long as the predicate returns true. * @desczh * 从开始位置截取集合,截取直到指定条件第一次不为true * @example * import { takeLeftWhile } from 'fp-ts/List' * * assert.deepStrictEqual(takeLeftWhile((n: number) => n % 2 === 0)([2, 4, 3, 6]), [2, 4]) * * @since 0.5.0 */ takeLeftWhile<A, B extends A>(refinement: Refinement<A, B>): (as: Kind<F, A>) => Kind<F, B>; takeLeftWhile<A>(predicate: Predicate<A>): (as: Kind<F, A>) => Kind<F, A>; /** * Returns a new Collection of the same type which includes entries from this Collection as long as the predicate returns false. * @desczh * 从开始位置截取集合,截取直到指定条件第一次为true * @example * import { takeLeftUntil } from 'fp-ts/List' * * assert.deepStrictEqual(takeLeftUntil((n: number) => n % 2 === 1)([2, 4, 3, 6]), [2, 4]) * * @since 0.5.0 */ takeLeftUntil<A, B extends A>(refinement: Refinement<A, B>): (as: Kind<F, A>) => Kind<F, B>; takeLeftUntil<A>(predicate: Predicate<A>): (as: Kind<F, A>) => Kind<F, A>; /** * Split an list into two parts: * 1. the longest initial subarray for which all elements satisfy the specified predicate * 2. the remaining elements * @desczh * 拆分一个集合到两个部分 * 1. 第一部分为takeLeftWhile截取的内容 * 2. 第二部分为剩余的内容 * @example * import { spanLeft } from 'fp-ts/List' * * assert.deepStrictEqual(spanLeft((n: number) => n % 2 === 1)([1, 3, 2, 4, 5]), { init: [1, 3], rest: [2, 4, 5] }) * * @since 0.5.0 */ spanLeft<A, B extends A>(refinement: Refinement<A, B>): (as: Kind<F, A>) => { init: Kind<F, A>; rest: Kind<F, A>; }; spanLeft<A>(predicate: Predicate<A>): (as: Kind<F, A>) => { init: Kind<F, A>; rest: Kind<F, A>; }; /** * Skip a number of elements from the start of an list, creating a new list * @desczh * 从开始位置跳过指定数量的子集 * @example * import { skipLeft } from 'fp-ts/List' * * assert.deepStrictEqual(skipLeft(2)([1, 2, 3]), [3]) * * @since 0.5.0 */ skipLeft(n: number): <A>(as: Kind<F, A>) => Kind<F, A>; /** * Skip a number of elements from the end of an list, creating a new list * @desczh * 从结尾位置跳过指定数量的子集 * @example * import { skipRight } from 'fp-ts/List' * * assert.deepStrictEqual(skipRight(2)([1, 2, 3, 4, 5]), [1, 2, 3]) * * @since 0.5.0 */ skipRight(n: number): <A>(as: Kind<F, A>) => Kind<F, A>; /** * Returns a new Collection of the same type which includes entries starting from when predicate first returns false. * @desczh * 截取takeLeftWhile剩余部分 * @example * import { skipLeftWhile } from 'fp-ts/List' * * assert.deepStrictEqual(skipLeftWhile((n: number) => n % 2 === 1)([1, 3, 2, 4, 5]), [2, 4, 5]) * * @since 0.5.0 */ skipLeftWhile<A>(predicate: Predicate<A>): (as: Kind<F, A>) => Kind<F, A>; /** * Returns a new `Collection` of the same type which includes entries starting from when predicate first returns true. * @desczh * 截取takeLeftUntil剩余部分 * @example * import { skipLeftUntil } from 'fp-ts/List' * * assert.deepStrictEqual(skipLeftUntil((n: number) => n % 2 === 0)([1, 3, 2, 4, 5]), [2, 4, 5]) * * @since 0.5.0 */ skipLeftUntil<A>(predicate: Predicate<A>): (as: Kind<F, A>) => Kind<F, A>; /** * Returns a new Collection with 0 size and no values in constant time. * @desczh * 清除集合,返回空集合 * @example * import { clear } from 'fp-ts/List' * * assert.deepStrictEqual(clear([1, 2, 3, 4]), [])) * * @since 0.5.0 */ clear: <A>(as: Kind<F, A>) => Kind<F, A>; /** * Splits an list into two pieces, the first piece has `n` elements. * @desczh * 在指定的位置拆分集合 * @example * import { splitAt } from 'fp-ts/List' * * assert.deepStrictEqual(splitAt(2)([1, 2, 3, 4, 5]), [[1, 2], [3, 4, 5]]) * * @since 0.5.0 */ splitAt(n: number): <A>(as: Kind<F, A>) => [Kind<F, A>, Kind<F, A>]; } export declare function initSubset<URI extends URIS>(option: SubsetCollectionInput<URI> & IndexWriterCollection<URI>): Subset<URI>;