Skip to content

Commit

Permalink
issue-#57 - Removed usages of 'Slice' type in fjl sources, except whe…
Browse files Browse the repository at this point in the history
…re type is defined (needed to quickly find other usages (via IDE, etc.)).
  • Loading branch information
elycruz committed Aug 10, 2022
1 parent bd67a48 commit 12b5dc3
Show file tree
Hide file tree
Showing 63 changed files with 180 additions and 5,791 deletions.
5,568 changes: 4 additions & 5,564 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/fjl/src/list/breakOnList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {findIndexWhere} from "./utils/findexIndexWhere";
import {of} from "../object/of";
import {reverse} from "./reverse";
import {splitAt} from "./splitAt";
import {Slice, PredForSlice} from "../types";
import {PredForArray} from "../types";
import {sliceFrom} from "./utils/sliceFrom";

export const
Expand All @@ -20,12 +20,12 @@ export const
* breakOnList (> 9) [1,2,3] == ([1,2,3],[])
* ```
*/
breakOnList = <T>(pred: PredForSlice<T>, list: Slice<T>): [Slice<T>, Slice<T>] => {
breakOnList = <T>(pred: PredForArray<T>, list: T[]): [T[], T[]] => {
const splitPoint = findIndexWhere(negateF3(pred), list);
return splitPoint === -1 ?
[of(list), sliceFrom(0, list)] : reverse(splitAt(splitPoint, list));
},

$breakOnList = <T>(pred: PredForSlice<T>) =>
(list: Slice<T>): [Slice<T>, Slice<T>] => breakOnList(pred, list)
$breakOnList = <T>(pred: PredForArray<T>) =>
(list: T[]): [T[], T[]] => breakOnList(pred, list)
;
11 changes: 5 additions & 6 deletions packages/fjl/src/list/complement.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import {reduce} from "./utils/reduce";
import {append} from "./append";
import {difference} from "./difference";
import {Slice} from '../types';

export const

/**
* Returns the complement of list 0 and the reset of the passed in arrays.
*/
complement = <T>(...arrays: Slice<T>[]): Slice<T> => {
complement = <T>(...arrays: T[][]): T[] => {
if (!arrays.length) return [];
const [arr0] = arrays;
return reduce((agg: Slice<T>, arr: Slice<T>) =>
append(agg, difference(arr, arr0) as Slice<T>), [], arrays)
return reduce((agg: T[], arr: T[]) =>
append(agg, difference(arr, arr0) as T[]), [], arrays)
},

/**
* Returns the complement of list 0 and the reset of the passed in arrays.
*/
$complement = <T>(xs1: Slice<T>) =>
(xs2: Slice<T>, ...arrays: Slice<T>[]): Slice<T> => complement(xs1, xs2, ...arrays)
$complement = <T>(xs1: T[]) =>
(xs2: T[], ...arrays: T[][]): T[] => complement(xs1, xs2, ...arrays)

;
7 changes: 3 additions & 4 deletions packages/fjl/src/list/concat.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {append} from './append';
import {sliceCopy} from './utils/sliceCopy';
import {Slice} from "../types";

export const

/**
* Concatenates all the elements of a container of lists.
*/
concat = <T>(xs: Slice<T>[]): Slice<T> => {
concat = <T>(xs: T[][]): T[] => {
if (!xs || !xs.length) {
return [] as unknown as Slice<T>;
return [] as unknown as T[];
} else if (xs.length === 1) {
const item0 = xs[0];
return item0 && item0.slice ? sliceCopy(item0) : item0;
}
return append(...xs) as Slice<T>;
return append(...xs) as T[];
}
;
7 changes: 3 additions & 4 deletions packages/fjl/src/list/cycle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {concat} from "./concat";
import {replicate} from "./replicate";
import {Slice} from "../types";
import {genIterator} from "./iterate";
import {isType} from "../object";

Expand All @@ -9,11 +8,11 @@ export const
/**
* Replicates a list `limit` number of times and appends the results (concat)
*/
cycle = <T>(n: number, xs: Slice<T>): Slice<T[]> =>
concat(replicate(n, xs) as unknown as Slice<T[]>[]),
cycle = <T>(n: number, xs: T[]): T[][] =>
concat(replicate(n, xs)),

$cycle = <T>(n: number) =>
(xs: Slice<T>): Slice<T[]> => cycle(n, xs),
(xs: T[]): T[][] => cycle(n, xs),

/**
* Generates a generator which cycles list (concatenate) to end of last yielded result - On first call last yielded
Expand Down
7 changes: 3 additions & 4 deletions packages/fjl/src/list/difference.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {reduce} from "./utils";
import {sliceCopy} from "./utils/sliceCopy";
import {includes} from "../platform/slice";
import {Slice} from "../types";

export const

/**
* Returns the difference of list 1 from list 2.
* @reference https://mathworld.wolfram.com/SetDifference.html
*/
difference = <T>(array1: Slice<T>, array2: Slice<T>): Slice<T> => {
difference = <T>(array1: T[], array2: T[]): T[] => {
if (array1 && !array2) {
return sliceCopy(array1);
} else if (!array1 && array2 || (!array1 && !array2)) {
Expand All @@ -23,5 +22,5 @@ export const
/**
* Curried version of `$difference`.
*/
$difference = <T>(array1: Slice<T>) =>
(array2: Slice<T>): Slice<T> => difference(array1, array2);
$difference = <T>(array1: T[]) =>
(array2: T[]): T[] => difference(array1, array2);
13 changes: 6 additions & 7 deletions packages/fjl/src/list/dropWhile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ import {length} from "./length";
import {findIndexWhere} from "./utils";
import {slice} from "../platform/slice";
import {sliceFrom} from "./utils/sliceFrom";
import {PredForSlice, Slice} from "../types";

export const

/**
* Returns an list without elements that match predicate.
*/
dropWhile = <T>(p: PredForSlice<T>, xs: Slice<T>): Slice<T> => {
dropWhile = <T>(p: PredForArray<T>, xs: T[]): T[] => {
const limit = length(xs),
splitPoint =
findIndexWhere(
(x: T, i: number | string, xs: Slice<T>) => !p(x, i, xs),
(x: T, i: number | string, xs: T[]) => !p(x, i, xs),
xs
) as number;
return splitPoint === -1 ?
sliceFrom(limit, xs) as Slice<T> :
slice(splitPoint, limit, xs) as Slice<T>;
sliceFrom(limit, xs) as T[] :
slice(splitPoint, limit, xs) as T[];
},

$dropWhile = <T>(p: PredForSlice<T>) =>
(xs: Slice<T>): Slice<T> => dropWhile(p, xs)
$dropWhile = <T>(p: PredForArray<T>) =>
(xs: T[]): T[] => dropWhile(p, xs)

;
12 changes: 6 additions & 6 deletions packages/fjl/src/list/dropWhileEnd.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import {findIndexWhereRight} from "./utils";
import {of} from "../object/of";
import {Slice, PredForSlice} from "../types";
import {sliceTo} from "./utils/sliceTo";
import {PredForArray} from "../types";

export const

dropWhileEnd = <T>(p: PredForSlice<T>, list: Slice<T>): Slice<T> => {
dropWhileEnd = <T>(p: PredForArray<T>, list: T[]): T[] => {
const splitPoint =
findIndexWhereRight(
(x: T, i: number | string, xs: Slice<T>) => !p(x, i, xs),
(x: T, i: number | string, xs: T[]) => !p(x, i, xs),
list
) as number;
if (splitPoint === -1) {
return of(list);
}
return sliceTo(splitPoint + 1, list) as Slice<T>;
return sliceTo(splitPoint + 1, list) as T[];
},

$dropWhileEnd = <T>(p: PredForSlice<T>) =>
(list: Slice<T>): Slice<T> => dropWhileEnd(p, list)
$dropWhileEnd = <T>(p: PredForArray<T>) =>
(list: T[]): T[] => dropWhileEnd(p, list)

;
5 changes: 2 additions & 3 deletions packages/fjl/src/list/elemIndex.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {indexOf} from "../platform/slice";
import {Slice} from "../types";

export const

elemIndex = <T>(xs: Slice<T>, x: T): number | undefined => {
elemIndex = <T>(xs: T[], x: T): number | undefined => {
const foundInd = indexOf(xs, x) as number;
return foundInd !== -1 ? foundInd : undefined;
},

$elemIndex = <T>(xs: Slice<T>) =>
$elemIndex = <T>(xs: T[]) =>
(x: T): number | undefined => elemIndex(xs, x)
;

11 changes: 5 additions & 6 deletions packages/fjl/src/list/filter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {Slice, PredForSlice} from "../types";
import {typeOf} from "../object/typeOf";

export const

/**
* Filters a structure of elements using given predicate (`pred`) (same as `[].filter`).
*/
filter = <T>(pred: PredForSlice<T>, xs: Slice<T>): Slice<T> => {
filter = <T>(pred: PredForArray<T>, xs: T[]): T[] => {
let ind = 0;
const limit = xs.length,
isString = typeOf(xs) === 'String';
let out = isString ? '' : [] as T[];
if (!limit) {
return out as Slice<T>;
return out as T[];
}
if (typeof xs === 'string') {
for (; ind < limit; ind++) {
Expand All @@ -27,10 +26,10 @@ export const
}
}
}
return out as Slice<T>;
return out as T[];
},

$filter = <T>(pred: PredForSlice<T>) =>
(xs: Slice<T>): Slice<T> => filter(pred, xs)
$filter = <T>(pred: PredForArray<T>) =>
(xs: T[]): T[] => filter(pred, xs)

;
10 changes: 5 additions & 5 deletions packages/fjl/src/list/foldl1.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import {uncons} from "./uncons";
import {reduce} from "./utils/reduce";
import {ReduceOp, Slice} from "../types";
import {ReduceOp} from "../types";

export const

/**
* A variant of `foldl` except that this one doesn't require the starting point value. The starting point/value will be pulled
* out from a copy of the container.
*/
foldl1 = <T, RetT>(op: ReduceOp<T, Slice<T>, RetT>, xs: Slice<T>): RetT => {
foldl1 = <T, RetT>(op: ReduceOp<T, T[], RetT>, xs: T[]): RetT => {
const parts = uncons(xs);
if (!parts) return;
const [_head, _tail]: [T, Slice<T>] = parts;
const [_head, _tail]: [T, T[]] = parts;
return reduce(op, _head as unknown as RetT, _tail) as RetT;
},

$foldl1 = <T, RetT>(op: ReduceOp<T, Slice<T>, RetT>) =>
(xs: Slice<T>): RetT => foldl1(op, xs)
$foldl1 = <T, RetT>(op: ReduceOp<T, T[], RetT>) =>
(xs: T[]): RetT => foldl1(op, xs)

;
8 changes: 4 additions & 4 deletions packages/fjl/src/list/foldr1.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {unconsr} from "./unconsr";
import {reduceRight} from "./utils/reduceRight";
import {Slice, ReduceOp} from "../types";
import {ReduceOp} from "../types";

export const

/**
* A variant of `foldr` except that this one doesn't require the starting point/value. The starting point/value will be pulled
* out from a copy of the container.
*/
foldr1 = <T, ZeroT>(op: ReduceOp<T, Slice<T>, ZeroT>, xs: Slice<T>): ZeroT | [] => {
foldr1 = <T, ZeroT>(op: ReduceOp<T, T[], ZeroT>, xs: T[]): ZeroT | [] => {
const parts = unconsr(xs);
return !parts ? [] : reduceRight(op, parts[1] as unknown as ZeroT, parts[0]);
},

$foldr1 = <T, ZeroT>(op: ReduceOp<T, Slice<T>, ZeroT>) =>
(xs: Slice<T>): ZeroT | [] => foldr1(op, xs)
$foldr1 = <T, ZeroT>(op: ReduceOp<T, T[], ZeroT>) =>
(xs: T[]): ZeroT | [] => foldr1(op, xs)

;
8 changes: 4 additions & 4 deletions packages/fjl/src/list/forEach.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {length} from "./length";
import {ForEachOp, Slice} from "../types";
import {ForEachOp} from "../types";

export const

/**
* For each function (same as `[].forEach` except in functional format).
*/
forEach = <T>(fn: ForEachOp<T | Slice<T>, Slice<T>>, list: Slice<T>): void => {
forEach = <T>(fn: ForEachOp<T | T[], T[]>, list: T[]): void => {
const limit = length(list);
if (!limit) {
return;
Expand All @@ -17,7 +17,7 @@ export const
}
},

$forEach = <T>(fn: ForEachOp<T | Slice<T>, Slice<T>>) =>
(list: Slice<T>): void => forEach(fn, list)
$forEach = <T>(fn: ForEachOp<T | T[], T[]>) =>
(list: T[]): void => forEach(fn, list)

;
3 changes: 1 addition & 2 deletions packages/fjl/src/list/group.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {groupBy} from "./groupBy";
import {Slice} from "../types/data";
import {equal} from "../boolean";

export const
Expand All @@ -11,4 +10,4 @@ export const
* It is a special case of groupBy, which allows the programmer to supply
* their own equality test.
*/
group = <T = any>(xs: Slice<T>): Slice<T>[] => groupBy(equal, xs) as Slice<T>[];
group = <T = any>(xs: T[]): T[][] => groupBy(equal, xs) as T[][];
9 changes: 4 additions & 5 deletions packages/fjl/src/list/groupBy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {length} from "./length";
import {sliceCopy} from "./utils/sliceCopy";
import {Slice} from "../types";
import {BinaryPred} from "../types";

export const

/**
* Groups given items by given predicate.
*/
groupBy = <T>(equalityOp: BinaryPred<T>, xs: Slice<T>): Slice<T>[] => {
groupBy = <T>(equalityOp: BinaryPred<T>, xs: T[]): T[][] => {
// Bail if empty list
if (!xs) {
return [];
Expand All @@ -22,7 +21,7 @@ export const
}

// Groupings
const groups: Slice<T>[] = [];
const groups: T[][] = [];

// Initialize variables for tracking
let ind = 1,
Expand All @@ -46,7 +45,7 @@ export const

// If original incoming slice is a string, return a slice of strings.
if(xs.constructor === String) {
return groups.map(_xs => (_xs as T[]).join('')) as unknown as Slice<T>[];
return groups.map(_xs => (_xs as T[]).join('')) as unknown as T[][];
}

return groups;
Expand All @@ -56,6 +55,6 @@ export const
* Curried version of `$groupBy`.
*/
$groupBy = <T>(equalityOp: BinaryPred<T>) =>
(xs: Slice<T>): Slice<T>[] => groupBy(equalityOp, xs)
(xs: T[]): T[][] => groupBy(equalityOp, xs)

;
3 changes: 1 addition & 2 deletions packages/fjl/src/list/init.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {lastIndex} from './utils';
import {Slice} from "../types";

export const
/**
* Returns everything except last item of list as new list.
*/
init = <T>(xs: Slice<T>): Slice<T> => xs.slice(0, lastIndex(xs));
init = <T>(xs: T[]): T[] => xs.slice(0, lastIndex(xs));
3 changes: 1 addition & 2 deletions packages/fjl/src/list/inits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {length} from "./length";
import {sliceTo} from "./utils/sliceTo";
import {Slice} from "../types";

export const
/**
Expand All @@ -9,7 +8,7 @@ export const
* shallowEquals(inits('abc'), ['','a','ab','abc'])
* ```
*/
inits = <T>(xs: Slice<T>): Slice<T>[] | T[] => {
inits = <T>(xs: T[]): T[][] | T[] => {
const limit = length(xs),
agg: [any[]] | any[] = [];
let ind = 0;
Expand Down
Loading

0 comments on commit 12b5dc3

Please sign in to comment.