From 42467e2c25eae32c85addeda10efac808387d17b Mon Sep 17 00:00:00 2001 From: Ely De La Cruz <603428+elycruz@users.noreply.github.com> Date: Sat, 1 Jun 2024 16:05:28 -0400 Subject: [PATCH] feat: issue-#57,#113,#114,#115,#116 - Type fixes --- packages/fjl-validator/src/ValidationUtils.ts | 4 +-- packages/fjl/src/list/concatMap.ts | 16 ++++++--- packages/fjl/src/list/map.ts | 12 +++---- packages/fjl/src/list/mapAccumL.ts | 26 +++++++------- packages/fjl/src/list/mapAccumR.ts | 34 ++++++++++--------- packages/fjl/src/list/zipWith3.ts | 4 +-- packages/fjl/src/types/data.ts | 2 +- packages/fjl/src/types/list.ts | 9 +++-- 8 files changed, 58 insertions(+), 49 deletions(-) diff --git a/packages/fjl-validator/src/ValidationUtils.ts b/packages/fjl-validator/src/ValidationUtils.ts index 6a6dc001..86afe4b7 100644 --- a/packages/fjl-validator/src/ValidationUtils.ts +++ b/packages/fjl-validator/src/ValidationUtils.ts @@ -33,7 +33,7 @@ export interface ValidatorResult { value?: T; } -export type Validator = Unary>; +export type Validator = Unary>; export const @@ -72,7 +72,7 @@ export const * @param options {...Object} * @returns {Object} */ - toValidationOptions = (...options: ValidatorOptions[]): ValidatorOptions => + toValidationOptions = (...options: ValidatorOptions[]): ValidatorOptions => assignDeep(defineEnumProps([ [Object, 'messageTemplates', {}], [Boolean, 'valueObscured', false], diff --git a/packages/fjl/src/list/concatMap.ts b/packages/fjl/src/list/concatMap.ts index 714c954d..9374f753 100644 --- a/packages/fjl/src/list/concatMap.ts +++ b/packages/fjl/src/list/concatMap.ts @@ -1,17 +1,25 @@ import {concat} from "../_platform/slice"; import {map} from "./map"; -import {MapOp, Slice} from "../types"; +import type {Ternary} from "../types"; export const /** * Map a function over all the elements of a container of containers and concatenate the results. */ - concatMap = (fn: MapOp, arr: Slice[]): any[] => + concatMap = ( + fn: Ternary, + arr: TS[] + ): ReturnType[] => concat(map(fn, arr)), - $concatMap = (fn: MapOp) => - (arr: T[][]): any[] => + /** + * Curried version of `concatMap`. + */ + $concatMap = ( + fn: Ternary + ) => + (arr: TS[]): ReturnType[] => concatMap(fn, arr) ; diff --git a/packages/fjl/src/list/map.ts b/packages/fjl/src/list/map.ts index 85042c86..7acec511 100644 --- a/packages/fjl/src/list/map.ts +++ b/packages/fjl/src/list/map.ts @@ -5,9 +5,9 @@ export const /** * Maps a function over an "array type" container of values. */ - map = ( - fn: Ternary, - xs: T[] + map = ( + fn: Ternary, + xs: TS ): ReturnType[] => { const limit = xs.length, out = new (xs.constructor as ArrayTypeConstructor)(limit); @@ -20,8 +20,8 @@ export const /** * Curried version of `map` method. */ - $map = - (fn: Ternary) => - (xs: T[]): ReturnType[] => + $map = + (fn: Ternary) => + (xs: TS): ReturnType[] => map(fn, xs) ; diff --git a/packages/fjl/src/list/mapAccumL.ts b/packages/fjl/src/list/mapAccumL.ts index 4d141d57..a67363d1 100644 --- a/packages/fjl/src/list/mapAccumL.ts +++ b/packages/fjl/src/list/mapAccumL.ts @@ -1,4 +1,4 @@ -import {Quaternary} from "../types"; +import type {Quaternary} from "../types"; /** * Performs a `reduce` and a `map` all in one (from left-to-right); @@ -7,16 +7,16 @@ import {Quaternary} from "../types"; * returns a tuple containing the aggregated value, * and the collected mapped values. */ -export const mapAccumL = ( - op: (agg?: Agg, x?: X, i?: number, xs?: X[]) => [Agg, X], - zero: Agg, - xs: X[] - ): [Agg, typeof xs] => { +export const mapAccumL = ( + op: Quaternary, + zero: AccumVal, + xs: Bs + ): [AccumVal, MapOfBs] => { const limit = xs.length; - if (!limit) return [zero, xs.slice(0)]; + if (!limit) return [zero, xs.slice(0) as unknown as MapOfBs]; - const mapped = xs.slice(0, 0); + const mapped = []; let agg = zero; for (let i = 0; i < limit; i++) { @@ -26,12 +26,12 @@ export const mapAccumL = ( mapped.push(tuple[1]); } - return [agg, mapped]; + return [agg, mapped as unknown as MapOfBs]; }, - $mapAccumL = ( - op: Quaternary + $mapAccumL = ( + op: Quaternary, ) => - (zero: Agg) => - (xs: X[]): [Agg, typeof xs] => + (zero: AccumVal) => + (xs: Bs): [AccumVal, MapOfBs] => mapAccumL(op, zero, xs); diff --git a/packages/fjl/src/list/mapAccumR.ts b/packages/fjl/src/list/mapAccumR.ts index 7450d6b7..b49b07b3 100644 --- a/packages/fjl/src/list/mapAccumR.ts +++ b/packages/fjl/src/list/mapAccumR.ts @@ -1,37 +1,39 @@ -import {MapAccumOp, Slice} from "../types"; +import {Quaternary, Slice} from "../types"; export const /** - * Performs a map and a reduce, all in one (from right-to-left), on each item, and returns the result of the reduce, - * and the map, as a tuple. + * Performs a map and a reduce, all in one (from right-to-left), on each item in the given array, and + * returns the result of the reduce, and the map, as a tuple, respectively. */ - mapAccumR = ( - op: MapAccumOp, - zero: A, - xs: Slice - ): [A, Slice] => { - const limit = xs.length; + mapAccumR = , MapOfBs extends Slice>( + op: Quaternary, + zero: AccumVal, + bs: Bs + ): [AccumVal, MapOfBs] => { + const limit = bs.length; - if (!limit) return [zero, xs.slice(0) as unknown as Slice]; + if (!limit) return [zero, bs.slice(0) as unknown as MapOfBs]; const mapped = []; let agg = zero; for (let i = limit - 1; i >= 0; i--) { - const tuple = op(agg, xs[i] as B, i); + const tuple = op(agg, bs[i] as B, i, bs); agg = tuple[0]; mapped.push(tuple[1]); } - return [agg, mapped]; + return [agg, mapped as unknown as MapOfBs]; }, /** * Curried version of `mapAccumR`. */ - $mapAccumR = (op: MapAccumOp) => - (zero: A) => - (xs: Slice): [A, Slice] => - mapAccumR(op, zero, xs); + $mapAccumR = , MapOfBs extends Slice>( + op: Quaternary, + ) => + (zero: AccumVal) => + (bs: Bs): [AccumVal, MapOfBs] => + mapAccumR(op, zero, bs); diff --git a/packages/fjl/src/list/zipWith3.ts b/packages/fjl/src/list/zipWith3.ts index e69df7c5..580a2250 100644 --- a/packages/fjl/src/list/zipWith3.ts +++ b/packages/fjl/src/list/zipWith3.ts @@ -6,9 +6,9 @@ export const /** * Zips 3 lists using tupling function. */ - zipWith3 = (op: ZipWith3Op, xs1: T[], xs2: T[], xs3: T[]): T[][] => zipWithN(op, xs1, xs2, xs3), + zipWith3 = (op: ZipWith3Op, xs1: T[], xs2: T[], xs3: T[]): T[][] => zipWithN(op, xs1, xs2, xs3), - $zipWith3 = (op: ZipWith3Op) => + $zipWith3 = (op: ZipWith3Op) => (xs1: T[]) => (xs2: T[]) => (xs3: T[]): T[][] => zipWith3(op, xs1, xs2, xs3); diff --git a/packages/fjl/src/types/data.ts b/packages/fjl/src/types/data.ts index 52d80a43..c49e22d8 100644 --- a/packages/fjl/src/types/data.ts +++ b/packages/fjl/src/types/data.ts @@ -5,7 +5,7 @@ /** * Represents types that have a `length` property, and that are "number" indexable. */ -export type NumberIndexable = ({ +export type NumberIndexable = ({ length: number; [index: number]: T; } | diff --git a/packages/fjl/src/types/list.ts b/packages/fjl/src/types/list.ts index 6f4f6071..b88e77a8 100644 --- a/packages/fjl/src/types/list.ts +++ b/packages/fjl/src/types/list.ts @@ -20,15 +20,14 @@ export type MapOp = (x: T, i?: nu * * Reduce operation function type. */ -export type ReduceOp = (agg: ZeroT, x?: T, i?: number | keyof FtrT, xs?: FtrT) => ZeroT; +export type ReduceOp = + (agg: ZeroT, x?: T, i?: number | keyof FtrT, xs?: FtrT) => ZeroT; /** - * @deprecated Use `Quinary` instead. - * * "Map + Accumulate", A.K.A. Map-Reduce, function type. */ -export type MapAccumOp> = - (agg?: A, b?: B, i?: Ind, bs?: Functor) => [A, C]; +export type MapAccumOp=Slice> = + (agg?: AccumVal, b?: B, i?: Index, bs?: SliceOfBs) => [AccumVal, MapOfB]; /** * @deprecated Use `TernaryPred` instead.