UNPKG

macoolka-collection

Version:

`macoolka-collection` Define Data Collection Interface.

433 lines (432 loc) 18.2 kB
import { Kind, URIS, URIS2, URIS3, Kind2, Kind3, HKT } from 'fp-ts/HKT'; import { Subset } from './Subset'; import { PipeableFunctorWithIndex1, PipeableChain1, PipeableFilterableWithIndex1, PipeableFoldable1, PipeableAlt1, PipeableExtend1 } from 'fp-ts/pipeable'; import { Monoid } from 'fp-ts/Monoid'; import { Monad1 } from 'fp-ts/Monad'; import { Applicative, Applicative1, Applicative2, Applicative3, Applicative2C } from 'fp-ts/Applicative'; import { Unfoldable1 } from 'fp-ts/Unfoldable'; import * as E from 'fp-ts/Either'; import * as O from 'fp-ts/Option'; import { Ord } from 'fp-ts/Ord'; import { Eq } from 'fp-ts/Eq'; import { Show } from 'fp-ts/Show'; import { Alternative1 } from 'fp-ts/Alternative'; import { Witherable1 } from 'fp-ts/Witherable'; import { TraversableWithIndex1 } from 'fp-ts/TraversableWithIndex'; declare type EnforceNonEmptyRecord<R> = keyof R extends never ? never : R; export interface GroupOption<A, B> { getValue: (a: A) => B; show: Show<B>; } export interface Typeclass<F extends URIS, I = number> extends Subset<F>, PipeableChain1<F> { /** * Returns a new `Collection` with values passed through a mapper function. * * @example * import { mapWithIndex } from 'fp-ts/List' * * assert.deepStrictEqual(mapWithIndex((i,a)=>a+i)([1, 2, 3]), [1, 3, 5]) * * @since 0.5.0 */ readonly mapWithIndex: PipeableFunctorWithIndex1<F, number>['mapWithIndex']; /** * Returns a new `Collection` with values passed through a mapper function. * * @example * import { map } from 'fp-ts/List' * * assert.deepStrictEqual(map(a=>a+1)([1, 2, 3]), [2, 3, 4]) * * @since 0.5.0 */ readonly map: PipeableChain1<F>['map']; readonly ap: PipeableChain1<F>['ap']; readonly apFirst: PipeableChain1<F>['apFirst']; readonly apSecond: PipeableChain1<F>['apSecond']; readonly zero: Alternative1<F>['zero']; readonly alt: PipeableAlt1<F>['alt']; readonly chain: PipeableChain1<F>['chain']; readonly chainFirst: PipeableChain1<F>['chainFirst']; readonly flatten: PipeableChain1<F>['flatten']; readonly reduce: PipeableFoldable1<F>['reduce']; readonly foldMap: PipeableFoldable1<F>['foldMap']; readonly reduceRight: PipeableFoldable1<F>['reduceRight']; foldM<M extends URIS>(M: Monad1<M>): <A, B>(fa: Kind<F, A>, b: B, f: (b: B, a: A) => Kind<M, B>) => Kind<M, B>; traverse_<M extends URIS3>(M: Applicative3<M>): <R, E, A, B>(fa: Kind<F, A>, f: (a: A) => Kind3<M, R, E, B>) => Kind3<M, R, E, void>; traverse_<M extends URIS2>(M: Applicative2<M>): <E, A, B>(fa: Kind<F, A>, f: (a: A) => Kind2<M, E, B>) => Kind2<M, E, void>; traverse_<M extends URIS2, E>(M: Applicative2C<M, E>): <A, B>(fa: Kind<F, A>, f: (a: A) => Kind2<M, E, B>) => Kind2<M, E, void>; traverse_<M extends URIS>(M: Applicative1<M>): <A, B>(fa: Kind<F, A>, f: (a: A) => Kind<M, B>) => Kind<M, void>; traverse_<M>(M: Applicative<M>): <A, B>(fa: Kind<F, A>, f: (a: A) => HKT<M, B>) => HKT<M, void>; traverse_<M>(M: Applicative<M>): <A, B>(fa: Kind<F, A>, f: (a: A) => HKT<M, B>) => HKT<M, void>; of: <A>(a: A) => Kind<F, A>; sequenceT<T extends Array<Kind<F, any>>>(...t: T & { 0: Kind<F, any>; }): Kind<F, any>; sequenceS<NER extends Record<string, Kind<F, any>>>(r: EnforceNonEmptyRecord<NER>): Kind<F, { [K in keyof NER]: [NER[K]] extends [Kind<F, infer A>] ? A : never; }>; sequenceS<NER extends Record<string, Kind<F, any>>>(r: EnforceNonEmptyRecord<NER>): Kind<F, any>; readonly unfold: <A, B>(b: B, f: (b: B) => O.Option<[A, B]>) => Kind<F, A>; readonly reduceWithIndex: <A, B>(b: B, f: (i: I, b: B, a: A) => B) => (fa: Kind<F, A>) => B; readonly foldMapWithIndex: <M>(M: Monoid<M>) => <A>(f: (i: I, a: A) => M) => (fa: Kind<F, A>) => M; readonly reduceRightWithIndex: <A, B>(b: B, f: (i: I, a: A, b: B) => B) => (fa: Kind<F, A>) => B; readonly filter: PipeableFilterableWithIndex1<F, number>['filter']; readonly filterMap: PipeableFilterableWithIndex1<F, number>['filterMap']; readonly partition: PipeableFilterableWithIndex1<F, number>['partition']; readonly partitionMap: PipeableFilterableWithIndex1<F, number>['partitionMap']; readonly compact: PipeableFilterableWithIndex1<F, number>['compact']; readonly separate: PipeableFilterableWithIndex1<F, number>['separate']; readonly sequence: Witherable1<F>['sequence']; readonly traverse: Witherable1<F>['traverse']; readonly wilt: Witherable1<F>['wilt']; readonly wither: Witherable1<F>['wither']; readonly traverseWithIndex: TraversableWithIndex1<F, number>['traverseWithIndex']; readonly filterWithIndex: PipeableFilterableWithIndex1<F, number>['filterWithIndex']; readonly filterMapWithIndex: PipeableFilterableWithIndex1<F, number>['filterMapWithIndex']; readonly partitionWithIndex: PipeableFilterableWithIndex1<F, number>['partitionWithIndex']; readonly partitionMapWithIndex: PipeableFilterableWithIndex1<F, number>['partitionMapWithIndex']; readonly extend: PipeableExtend1<F>['extend']; readonly duplicate: PipeableExtend1<F>['duplicate']; /** * Extracts from an list of `Either` all the `Right` elements. All the `Right` elements are extracted in order * * @example * import { rights } from 'fp-ts/List' * import { right, left } from 'fp-ts/Either' * * assert.deepStrictEqual(rights([right(1), left('foo'), right(2)]), [1, 2]) * * @since 0.5.0 */ rights<E, A>(as: Kind<F, E.Either<E, A>>): Kind<F, A>; /** * Extracts from an list of `Either` all the `Left` elements. All the `Left` elements are extracted in order * * @example * import { lefts } from 'fp-ts/List' * import { left, right } from 'fp-ts/Either' * * assert.deepStrictEqual(lefts([right(1), left('foo'), right(2)]), ['foo']) * * @since 0.5.0 */ lefts<E, A>(as: Kind<F, E.Either<E, A>>): Kind<F, E>; /** * Sort the elements of an list in increasing order, creating a new list * * @example * import { sort } from 'fp-ts/List' * import { ordNumber } from 'fp-ts/Ord' * * assert.deepStrictEqual(sort(ordNumber)([3, 2, 1]), [1, 2, 3]) * * @since 0.5.0 */ sort<A>(O: Ord<A>): (as: Kind<F, A>) => Kind<F, A>; /** * Sort the elements of an list in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, * etc... * * @example * import { sortBy } from 'fp-ts/List' * import { ord, ordString, ordNumber } from 'fp-ts/Ord' * * interface Person { * name: string * age: number * } * const byName = ord.contramap(ordString, (p: Person) => p.name) * const byAge = ord.contramap(ordNumber, (p: Person) => p.age) * * const sortByNameByAge = sortBy([byName, byAge]) * * const persons = [{ name: 'a', age: 1 }, { name: 'b', age: 3 }, { name: 'c', age: 2 }, { name: 'b', age: 2 }] * assert.deepStrictEqual(sortByNameByAge(persons), [ * { name: 'a', age: 1 }, * { name: 'b', age: 2 }, * { name: 'b', age: 3 }, * { name: 'c', age: 2 } * ]) * * @since 0.5.0 */ sortBy<A>(ords: Array<Ord<A>>): (as: Kind<F, A>) => Kind<F, A>; /** * Returns the maximum value in this `collection`. * If any values are comparatively equivalent, the first one found will be returned. * * @example * import { max } from 'fp-ts/List' * import { ordNumber } from 'fp-ts/Ord' * * assert.deepStrictEqual(max(ordNumber)([3, 2, 1]), 3) * * @since 0.5.0 */ max<A>(O: Ord<A>): (as: Kind<F, A>) => O.Option<A>; /** * Like max, but also accepts a comparatorValueMapper which allows for comparing by more sophisticated means: * Compare the elements of an list in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, * etc... * * @example * import { maxBy } from 'fp-ts/List' * import { ord, ordString, ordNumber } from 'fp-ts/Ord' * * interface Person { * name: string * age: number * } * const byName = ord.contramap(ordString, (p: Person) => p.name) * const byAge = ord.contramap(ordNumber, (p: Person) => p.age) * * const maxByNameByAge = maxBy([byName, byAge]) * * const persons = [{ name: 'a', age: 1 }, { name: 'b', age: 3 }, { name: 'c', age: 2 }, { name: 'b', age: 2 }] * assert.deepStrictEqual(sortByNameByAge(persons), * { name: 'c', age: 2 } * ) * * @since 0.5.0 */ maxBy<A>(ords: Array<Ord<A>>): (as: Kind<F, A>) => O.Option<A>; /** * Returns the minimum value in this `collection`. * If any values are comparatively equivalent, the first one found will be returned. * * @example * import { min } from 'fp-ts/List' * import { ordNumber } from 'fp-ts/Ord' * * assert.deepStrictEqual(min(ordNumber)([3, 2, 1]), 1) * * @since 0.5.0 */ min<A>(O: Ord<A>): (as: Kind<F, A>) => O.Option<A>; /** * Like min, but also accepts a Array which allows for comparing by more sophisticated means: * Compare the elements of an list in increasing order, where elements are compared using first `ords[0]`, then `ords[1]`, * etc... * * @example * import { minBy } from 'fp-ts/List' * import { ord, ordString, ordNumber } from 'fp-ts/Ord' * * interface Person { * name: string * age: number * } * const byName = ord.contramap(ordString, (p: Person) => p.name) * const byAge = ord.contramap(ordNumber, (p: Person) => p.age) * * const minByNameByAge = minBy([byName, byAge]) * * const persons = [{ name: 'a', age: 1 }, { name: 'b', age: 3 }, { name: 'c', age: 2 }, { name: 'b', age: 2 }] * assert.deepStrictEqual(sortByNameByAge(persons), * { name: 'a', age: 1 } * ) * * @since 0.5.0 */ minBy<A>(ords: Array<Ord<A>>): (as: Kind<F, A>) => O.Option<A>; /** * Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new list. * * If one input list is short, excess elements of the longer list are discarded. * * @example * import { zipWith } from 'fp-ts/List' * * assert.deepStrictEqual(zipWith([1, 2, 3], ['a', 'b', 'c', 'd'], (n, s) => s + n), ['a1', 'b2', 'c3']) * * @since 0.5.0 */ zipWith<A, B, C>(fa: Kind<F, A>, fb: Kind<F, B>, f: (a: A, b: B) => C): Kind<F, C>; /** * Takes two arrays and returns an list of corresponding pairs. If one input list is short, excess elements of the * longer list are discarded * * @example * import { zip } from 'fp-ts/List' * * assert.deepStrictEqual(zip([1, 2, 3], ['a', 'b', 'c', 'd']), [[1, 'a'], [2, 'b'], [3, 'c']]) * * @since 0.5.0 */ zip<A, B>(fa: Kind<F, A>, fb: Kind<F, B>): Kind<F, [A, B]>; /** * The function is reverse of `zip`. Takes an list of pairs and return two corresponding arrays * * @example * import { unzip } from 'fp-ts/List' * * assert.deepStrictEqual(unzip([[1, 'a'], [2, 'b'], [3, 'c']]), [[1, 2, 3], ['a', 'b', 'c']]) * * @since 0.5.0 */ unzip<A, B>(as: Kind<F, [A, B]>): [Kind<F, A>, Kind<F, B>]; /** * Rotate an list to the right by `n` steps * * @example * import { rotate } from 'fp-ts/List' * * assert.deepStrictEqual(rotate(2)([1, 2, 3, 4, 5]), [4, 5, 1, 2, 3]) * * @since 0.5.0 */ rotate(n: number): <A>(as: Kind<F, A>) => Kind<F, A>; /** * Test if a value is a member of an list. Takes a `Eq<A>` as a single * argument which returns the function to use to search for a value of type `A` in * an list of type `Kind<F, A>`. * * @example * import { includes } from 'fp-ts/List' * import { eqNumber } from 'fp-ts/Eq' * * assert.strictEqual(includes(eqNumber)(1, [1, 2, 3]), true) * assert.strictEqual(includes(eqNumber)(4, [1, 2, 3]), false) * * @since 0.5.0 */ includes<A>(E: Eq<A>): (a: A, as: Kind<F, A>) => boolean; /** * Remove duplicates from an list, keeping the first occurance of an element. * * @example * import { uniq } from 'fp-ts/List' * import { eqNumber } from 'fp-ts/Eq' * * assert.deepStrictEqual(uniq(eqNumber)([1, 2, 1]), [1, 2]) * * @since 0.5.0 */ uniq<A>(E: Eq<A>): (as: Kind<F, A>) => Kind<F, A>; /** * Creates an list of unique values, in order, from all given arrays using a `Eq` for equality comparisons * * @example * import { union } from 'fp-ts/List' * import { eqNumber } from 'fp-ts/Eq' * * assert.deepStrictEqual(union(eqNumber)([1, 2], [2, 3]), [1, 2, 3]) * * @since 0.5.0 */ union<A>(E: Eq<A>): (xs: Kind<F, A>, ys: Kind<F, A>) => Kind<F, A>; /** * Creates an list of unique values that are included in all given arrays using a `Eq` for equality * comparisons. The order and references of result values are determined by the first list. * * @example * import { intersection } from 'fp-ts/List' * import { eqNumber } from 'fp-ts/Eq' * * assert.deepStrictEqual(intersection(eqNumber)([1, 2], [2, 3]), [2]) * * @since 0.5.0 */ intersection<A>(E: Eq<A>): (xs: Kind<F, A>, ys: Kind<F, A>) => Kind<F, A>; /** * Creates an list of list values not included in the other given list using a `Eq` for equality * comparisons. The order and references of result values are determined by the first list. * * @example * import { difference } from 'fp-ts/List' * import { eqNumber } from 'fp-ts/Eq' * * assert.deepStrictEqual(difference(eqNumber)([1, 2], [2, 3]), [1]) * * @since 0.5.0 */ difference<A>(E: Eq<A>): (xs: Kind<F, A>, ys: Kind<F, A>) => Kind<F, A>; toRecord<A extends Record<string, any>, K extends keyof A>(key: K): (as: Kind<F, A>) => Record<string, Omit<A, K>>; fromRecord<K extends string>(key: K): <A extends Record<string, any>>(as: Record<string, A>) => Kind<F, A & { [key in K]: string; }>; scanLeft<A, B>(b: B, f: (b: B, a: A) => B): (as: Kind<F, A>) => Kind<F, B>; /** * Fold an list from the right, keeping all intermediate results instead of only the final result * * @example * import { scanRight } from 'fp-ts/List' * * assert.deepStrictEqual(scanRight(10, (a: number, b) => b - a)([1, 2, 3]), [4, 5, 7, 10]) * * @since 0.5.0 */ scanRight<A, B>(b: B, f: (a: A, b: B) => B): (as: Kind<F, A>) => Kind<F, B>; /** * Array comprehension * * ``` * [ f(x, y, ...) | x ← xs, y ← ys, ..., g(x, y, ...) ] * ``` * * @example * import { comprehension } from 'fp-ts/Array' * import { tuple } from 'fp-ts/function' * * assert.deepStrictEqual(comprehension([[1, 2, 3], ['a', 'b']], tuple, (a, b) => (a + b.length) % 2 === 0), [ * [1, 'a'], * [1, 'b'], * [3, 'a'], * [3, 'b'] * ]) * * @since 2.0.0 */ comprehension<A, B, C, D, R>(input: [Kind<F, A>, Kind<F, B>, Kind<F, C>, Kind<F, D>], f: (a: A, b: B, c: C, d: D) => R, g?: (a: A, b: B, c: C, d: D) => boolean): Kind<F, R>; comprehension<A, B, C, R>(input: [Kind<F, A>, Kind<F, B>, Kind<F, C>], f: (a: A, b: B, c: C) => R, g?: (a: A, b: B, c: C) => boolean): Kind<F, R>; comprehension<A, R>(input: [Kind<F, A>], f: (a: A) => R, g?: (a: A) => boolean): Kind<F, R>; comprehension<A, B, R>(input: [Kind<F, A>, Kind<F, B>], f: (a: A, b: B) => R, g?: (a: A, b: B) => boolean): Kind<F, R>; comprehension<A, R>(input: [Kind<F, A>], f: (a: A) => boolean, g?: (a: A) => R): Kind<F, R>; comprehension<R>(input: [F, Kind<F, any>], f: (...xs: any[]) => R, g: (...xs: any[]) => boolean): Kind<F, R>; /** * A useful recursion pattern for processing an `collection` to produce a new `collection`, often used for "chopping" up the input * `collection`. Typically chop is called with some function that will consume an initial prefix of the array and produce a * value and the rest of the array. * * @example * import { Eq, eqNumber } from 'fp-ts/Eq' * import { chop, spanLeft } from 'fp-ts/Array' * * const group = <A>(S: Eq<A>): ((as: Array<A>) => Array<Array<A>>) => { * return chop(as => { * const { init, rest } = spanLeft((a: A) => S.equals(a, as[0]))(as) * return [init, rest] * }) * } * assert.deepStrictEqual(group(eqNumber)([1, 1, 2, 3, 3, 4]), [[1, 1], [2], [3, 3], [4]]) * * @since 2.0.0 */ chop<A, B>(f: (as: Kind<F, A>) => [B, Kind<F, A>]): (as: Kind<F, A>) => Kind<F, B>; foldLeft<A, B>(onNil: () => B, onCons: (head: A, tail: Kind<F, A>) => B): (as: Kind<F, A>) => B; foldRight<A, B>(onNil: () => B, onCons: (init: Kind<F, A>, last: A) => B): (as: Kind<F, A>) => B; /** * Group by a mehtod * @example * pipe( * from([1, 1, 2, 1, 1, 3, 3, 4]), * groupByOption({ getValue: (a: number) => a, show: showNumber }), * expect({ * '1': from([1, 1, 1, 1]), * '2': from([2]), * '3': from([3, 3]), * '4': from([4]) * }).toEqual * ) * */ groupBy<A, B>({ getValue, show: { show } }: GroupOption<A, B>): (as: Kind<F, A>) => Record<string, Kind<F, A>>; getShow<A>(S: Show<A>): Show<Kind<F, A>>; getEq: <A>(eq: Eq<A>) => Eq<Kind<F, A>>; getMonoid: <A = never>() => Monoid<Kind<F, A>>; getOrd: <A>(O: Ord<A>) => Ord<Kind<F, A>>; collection: Monad1<F> & Unfoldable1<F> & Alternative1<F> & Witherable1<F> & TraversableWithIndex1<F, number>; } export declare function initTypeclass<F extends URIS>(option: Subset<F>): Typeclass<F>; export {};