Skip to content

Commit

Permalink
fjl - Parameterized some functions, and removed some curlys.
Browse files Browse the repository at this point in the history
  • Loading branch information
elycruz committed Dec 17, 2023
1 parent d5741cc commit 5bcfe97
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 32 deletions.
3 changes: 3 additions & 0 deletions packages/fjl/AUXILLARY-TODOS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Auxiliary Todos

- [ ] Switch to node/deno built-in testing tools (better "managed" dependency alternative).
4 changes: 2 additions & 2 deletions packages/fjl/src/boolean/equal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Equality combinator.
*/
export const equal = (a: any, b: any) => a === b,
export const equal = <T = any>(a: T, b: T) => a === b,

/**
* Curried version of `equal`
*/
$equal = (a: any) => (b: any) => a === b;
$equal = <T>(a: T) => (b: T) => a === b;
6 changes: 3 additions & 3 deletions packages/fjl/src/function/bind.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {UnitNary} from "../types";
import {Nary, UnitNary} from "../types";

/**
* Functional `bind` - ignores first `#Function.bind()` argument.
*/
export const bind = <F extends UnitNary>(fn: F, ...args: any[]): ReturnType<F> =>
export const bind = <F extends UnitNary>(fn: F, ...args: any[]): Nary<any, ReturnType<F>> =>
fn.bind(null, ...args),

/**
* Curried version of `bind`.
*/
$bind = <F extends UnitNary>(fn: F) => (...args: any[]): ReturnType<F> =>
$bind = <F extends UnitNary>(fn: F) => (...args: any[]): Nary<any, ReturnType<F>> =>
fn.bind(null, ...args);
4 changes: 2 additions & 2 deletions packages/fjl/src/function/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T = any>(...fns: Unary<T>[]): Unary<T> =>
(arg0: T): T => reduceRight((value: T, fn: Unary) => fn(value), arg0, fns);
6 changes: 2 additions & 4 deletions packages/fjl/src/list/utils/findIndexWhereRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
3 changes: 1 addition & 2 deletions packages/fjl/src/list/utils/findIndicesWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
6 changes: 2 additions & 4 deletions packages/fjl/src/list/utils/findexIndexWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
3 changes: 1 addition & 2 deletions packages/fjl/src/list/utils/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ export const
reduce = (op: ReduceOp, agg: any, xs: NumberIndexable): ReturnType<typeof op> => {
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;
},

Expand Down
3 changes: 1 addition & 2 deletions packages/fjl/src/list/utils/reduceRight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ export const
reduceRight = (op: ReduceOp, agg: any, xs: NumberIndexable): ReturnType<typeof op> => {
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;
},

Expand Down
5 changes: 1 addition & 4 deletions packages/fjl/src/list/utils/sliceCopy.ts
Original file line number Diff line number Diff line change
@@ -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)

;
3 changes: 1 addition & 2 deletions packages/fjl/src/list/utils/sliceFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/list/utils/sliceTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/list/utils/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const
* Returns an array with the given indices swapped.
*/
swap = <T>(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;
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/list/utils/toShortest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const
* @curried Up-to two params.
*/
$toShortest = <T>(list1: NumberIndexable<T>) =>
(list2: NumberIndexable<T>, ...lists: NumberIndexable<T>[]): NumberIndexable<T>[] => toShortest(list1, list2, ...lists)
(list2: NumberIndexable<T>, ...lists: NumberIndexable<T>[]): NumberIndexable<T>[] =>
toShortest(list1, list2, ...lists)

;
4 changes: 2 additions & 2 deletions packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export type NumberIndexable<T = unknown> = ({
[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.
});

/**
Expand Down

0 comments on commit 5bcfe97

Please sign in to comment.