From 5bcfe973fabe5e5b7ad2ccd5d4eb9be75fe6f81d Mon Sep 17 00:00:00 2001 From: Ely De La Cruz <603428+elycruz@users.noreply.github.com> Date: Sat, 16 Dec 2023 19:44:20 -0500 Subject: [PATCH] fjl - Parameterized some functions, and removed some curlys. --- packages/fjl/AUXILLARY-TODOS.md | 3 +++ packages/fjl/src/boolean/equal.ts | 4 ++-- packages/fjl/src/function/bind.ts | 6 +++--- packages/fjl/src/function/compose.ts | 4 ++-- packages/fjl/src/list/utils/findIndexWhereRight.ts | 6 ++---- packages/fjl/src/list/utils/findIndicesWhere.ts | 3 +-- packages/fjl/src/list/utils/findexIndexWhere.ts | 6 ++---- packages/fjl/src/list/utils/reduce.ts | 3 +-- packages/fjl/src/list/utils/reduceRight.ts | 3 +-- packages/fjl/src/list/utils/sliceCopy.ts | 5 +---- packages/fjl/src/list/utils/sliceFrom.ts | 3 +-- packages/fjl/src/list/utils/sliceTo.ts | 2 +- packages/fjl/src/list/utils/swap.ts | 2 +- packages/fjl/src/list/utils/toShortest.ts | 3 ++- packages/fjl/src/types/data.ts | 4 ++-- 15 files changed, 25 insertions(+), 32 deletions(-) create mode 100644 packages/fjl/AUXILLARY-TODOS.md diff --git a/packages/fjl/AUXILLARY-TODOS.md b/packages/fjl/AUXILLARY-TODOS.md new file mode 100644 index 00000000..a591214d --- /dev/null +++ b/packages/fjl/AUXILLARY-TODOS.md @@ -0,0 +1,3 @@ +# Auxiliary Todos + +- [ ] Switch to node/deno built-in testing tools (better "managed" dependency alternative). diff --git a/packages/fjl/src/boolean/equal.ts b/packages/fjl/src/boolean/equal.ts index d75ac6cb..d3a91486 100644 --- a/packages/fjl/src/boolean/equal.ts +++ b/packages/fjl/src/boolean/equal.ts @@ -1,9 +1,9 @@ /** * Equality combinator. */ -export const equal = (a: any, b: any) => a === b, +export const equal = (a: T, b: T) => a === b, /** * Curried version of `equal` */ - $equal = (a: any) => (b: any) => a === b; + $equal = (a: T) => (b: T) => a === b; diff --git a/packages/fjl/src/function/bind.ts b/packages/fjl/src/function/bind.ts index ad9127dc..4f1be2fe 100644 --- a/packages/fjl/src/function/bind.ts +++ b/packages/fjl/src/function/bind.ts @@ -1,13 +1,13 @@ -import {UnitNary} from "../types"; +import {Nary, UnitNary} from "../types"; /** * Functional `bind` - ignores first `#Function.bind()` argument. */ -export const bind = (fn: F, ...args: any[]): ReturnType => +export const bind = (fn: F, ...args: any[]): Nary> => fn.bind(null, ...args), /** * Curried version of `bind`. */ - $bind = (fn: F) => (...args: any[]): ReturnType => + $bind = (fn: F) => (...args: any[]): Nary> => fn.bind(null, ...args); diff --git a/packages/fjl/src/function/compose.ts b/packages/fjl/src/function/compose.ts index 12e58862..877b5fa1 100644 --- a/packages/fjl/src/function/compose.ts +++ b/packages/fjl/src/function/compose.ts @@ -5,5 +5,5 @@ import {Unary} from "../types"; * Composes all functions passed in from right to left passing each function's return value to * the function on the left of itself. */ -export const compose = (...fns: Unary[]): Unary => - (arg0: Unary): any => reduceRight((value: any, fn: Unary) => fn(value), arg0, fns); +export const compose = (...fns: Unary[]): Unary => + (arg0: T): T => reduceRight((value: T, fn: Unary) => fn(value), arg0, fns); diff --git a/packages/fjl/src/list/utils/findIndexWhereRight.ts b/packages/fjl/src/list/utils/findIndexWhereRight.ts index 0328ffc2..cd431e09 100644 --- a/packages/fjl/src/list/utils/findIndexWhereRight.ts +++ b/packages/fjl/src/list/utils/findIndexWhereRight.ts @@ -9,10 +9,8 @@ export const pred: TernaryPred, xs: NumberIndexable ): number => { - for (let ind = xs.length - 1; ind >= 0; ind -= 1) { - const predicateFulfilled = !!pred(xs[ind], ind, xs); - if (predicateFulfilled) return ind; - } + for (let ind = xs.length - 1; ind >= 0; ind -= 1) + if (pred(xs[ind], ind, xs)) return ind; return -1; }, diff --git a/packages/fjl/src/list/utils/findIndicesWhere.ts b/packages/fjl/src/list/utils/findIndicesWhere.ts index aecec44b..a95358d0 100644 --- a/packages/fjl/src/list/utils/findIndicesWhere.ts +++ b/packages/fjl/src/list/utils/findIndicesWhere.ts @@ -11,9 +11,8 @@ export const ): number[] | undefined => { const limit = xs.length; const out = []; - for (let ind = 0; ind < limit; ind++) { + for (let ind = 0; ind < limit; ind++) if (pred(xs[ind], ind, xs)) out.push(ind); - } return out.length ? out : undefined; }, diff --git a/packages/fjl/src/list/utils/findexIndexWhere.ts b/packages/fjl/src/list/utils/findexIndexWhere.ts index 16e89816..501f82ed 100644 --- a/packages/fjl/src/list/utils/findexIndexWhere.ts +++ b/packages/fjl/src/list/utils/findexIndexWhere.ts @@ -13,10 +13,8 @@ export const xs: NumberIndexable ): number => { const limit = xs.length; - for (let ind = 0; ind < limit; ind += 1) { - const predicateFulfilled = pred(xs[ind], ind, xs); - if (predicateFulfilled) return ind; - } + for (let ind = 0; ind < limit; ind += 1) + if (pred(xs[ind], ind, xs)) return ind; return -1; }, diff --git a/packages/fjl/src/list/utils/reduce.ts b/packages/fjl/src/list/utils/reduce.ts index 71a858c6..2afb4aec 100644 --- a/packages/fjl/src/list/utils/reduce.ts +++ b/packages/fjl/src/list/utils/reduce.ts @@ -9,9 +9,8 @@ export const reduce = (op: ReduceOp, agg: any, xs: NumberIndexable): ReturnType => { const limit = xs.length; let result = agg; - for (let i = 0; i < limit; i += 1) { + for (let i = 0; i < limit; i += 1) result = op(result, xs[i], i, xs); - } return result; }, diff --git a/packages/fjl/src/list/utils/reduceRight.ts b/packages/fjl/src/list/utils/reduceRight.ts index da4ff454..ef948b49 100644 --- a/packages/fjl/src/list/utils/reduceRight.ts +++ b/packages/fjl/src/list/utils/reduceRight.ts @@ -8,9 +8,8 @@ export const reduceRight = (op: ReduceOp, agg: any, xs: NumberIndexable): ReturnType => { const limit = xs.length; let result = agg; - for (let ind = limit - 1; ind >= 0; ind--) { + for (let ind = limit - 1; ind >= 0; ind--) result = op(result, xs[ind], ind, xs); - } return result; }, diff --git a/packages/fjl/src/list/utils/sliceCopy.ts b/packages/fjl/src/list/utils/sliceCopy.ts index b6a88d60..aa804d05 100644 --- a/packages/fjl/src/list/utils/sliceCopy.ts +++ b/packages/fjl/src/list/utils/sliceCopy.ts @@ -1,11 +1,8 @@ -import {sliceFrom} from "./sliceFrom"; - export const /** * Returns a copy of a slice (E.g., an array and/or a string). */ - sliceCopy = (xs: string | any[]): typeof xs => - sliceFrom(0, xs) + sliceCopy = (xs: string | any[]): typeof xs => xs.slice(0) ; diff --git a/packages/fjl/src/list/utils/sliceFrom.ts b/packages/fjl/src/list/utils/sliceFrom.ts index 5410fa2c..8ea32326 100644 --- a/packages/fjl/src/list/utils/sliceFrom.ts +++ b/packages/fjl/src/list/utils/sliceFrom.ts @@ -6,8 +6,7 @@ export const /** * Returns a slice of the given list from `startInd` to the end of the list. */ - sliceFrom = (startInd: number, xs: Slice): typeof xs => - slice(startInd, undefined, xs), + sliceFrom = (startInd: number, xs: Slice): typeof xs => xs.slice(startInd), /** * Curried version of `sliceFrom`. diff --git a/packages/fjl/src/list/utils/sliceTo.ts b/packages/fjl/src/list/utils/sliceTo.ts index 615a4892..4545e306 100644 --- a/packages/fjl/src/list/utils/sliceTo.ts +++ b/packages/fjl/src/list/utils/sliceTo.ts @@ -7,7 +7,7 @@ export const * Slices from index `0` to given index. */ sliceTo = (toInd: number, xs: Slice): typeof xs => - slice(0, toInd, xs), + xs.slice(0, toInd), /** * Curried version of `sliceTo`. diff --git a/packages/fjl/src/list/utils/swap.ts b/packages/fjl/src/list/utils/swap.ts index c5056e0a..4a43ae64 100644 --- a/packages/fjl/src/list/utils/swap.ts +++ b/packages/fjl/src/list/utils/swap.ts @@ -6,7 +6,7 @@ export const * Returns an array with the given indices swapped. */ swap = (ind1: number, ind2: number, list: T[]): T[] => { - const out = sliceCopy(list) as T[], + const out = list.slice(0) as T[], tmp = out[ind1]; out[ind1] = out[ind2]; out[ind2] = tmp; diff --git a/packages/fjl/src/list/utils/toShortest.ts b/packages/fjl/src/list/utils/toShortest.ts index 4774c10f..21ff43eb 100644 --- a/packages/fjl/src/list/utils/toShortest.ts +++ b/packages/fjl/src/list/utils/toShortest.ts @@ -23,6 +23,7 @@ export const * @curried Up-to two params. */ $toShortest = (list1: NumberIndexable) => - (list2: NumberIndexable, ...lists: NumberIndexable[]): NumberIndexable[] => toShortest(list1, list2, ...lists) + (list2: NumberIndexable, ...lists: NumberIndexable[]): NumberIndexable[] => + toShortest(list1, list2, ...lists) ; diff --git a/packages/fjl/src/types/data.ts b/packages/fjl/src/types/data.ts index ef4e9041..11e53dfd 100644 --- a/packages/fjl/src/types/data.ts +++ b/packages/fjl/src/types/data.ts @@ -7,14 +7,14 @@ export type NumberIndexable = ({ [index: number]: T; } | /** - * Support for `string` type + * Else support `string`. */ { readonly length: number; readonly [index: number]: any; // for `String`/`string`, type here // should actually be `string`, this is too strict, however, for // a Sum type so `any` is used instead (works fine) - // interchangeably targeting `string`, and/or `Array` types. + // interchangeably targeting `string`, and/or `Array` types, from overall type. }); /**