Skip to content

Commit

Permalink
issue-#57 - Progress on change concrete 'Slice' requirements - We're …
Browse files Browse the repository at this point in the history
…now updating all methods that require slices to take an 'XS' that extends and 'Slice<T>' - Allows use of return values as their own types instead of having to cast things to 'Slice<T>' on method use cases.
  • Loading branch information
elycruz committed Aug 14, 2022
1 parent 09738b3 commit 1e51d96
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 50 deletions.
6 changes: 3 additions & 3 deletions packages/fjl/src/list/breakOnList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <X, XS extends Slice<X>>(pred: PredForSlice<X>, list: XS): [XS, XS] => {
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, XS extends Slice<T>>(pred: PredForSlice<T>) =>
(list: XS): [XS, XS] => breakOnList(pred, list)
;
6 changes: 3 additions & 3 deletions packages/fjl/src/list/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export const
/**
* Concatenates all the elements of a container of lists.
*/
concat = <T>(xs: Slice<T>[]): Slice<T> => {
concat = <T, XS extends Slice<T>>(xs: XS[]): XS => {
if (!xs || !xs.length) {
return [] as unknown as Slice<T>;
return [] as unknown as XS;
} 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 XS;
}
;
8 changes: 4 additions & 4 deletions packages/fjl/src/list/cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,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, XS extends Slice<T>>(n: number, xs: XS): XS =>
concat(replicate(n, xs)),

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

/**
* Generates a generator which cycles list (concatenate) to end of last yielded result - On first call last yielded
Expand Down
15 changes: 7 additions & 8 deletions packages/fjl/src/list/dropWhile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import {PredForSlice, Slice} from "../types";
export const

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

$dropWhile = <T>(p: PredForSlice<T>) =>
(xs: Slice<T>): Slice<T> => dropWhile(p, xs)
$dropWhile = <T, XS extends Slice<T>>(p: PredForSlice<T>) =>
(xs: XS): XS => dropWhile(p, xs)

;
10 changes: 5 additions & 5 deletions packages/fjl/src/list/dropWhileEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import {sliceTo} from "./utils/sliceTo";

export const

dropWhileEnd = <T>(p: PredForSlice<T>, list: Slice<T>): Slice<T> => {
dropWhileEnd = <T, XS extends Slice<T>>(p: PredForSlice<T>, list: XS): XS => {
const splitPoint =
findIndexWhereRight(
(x: T, i: number | string, xs: Slice<T>) => !p(x, i, xs),
(x: T, i: number | string, xs: XS) => !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);
},

$dropWhileEnd = <T>(p: PredForSlice<T>) =>
(list: Slice<T>): Slice<T> => dropWhileEnd(p, list)
$dropWhileEnd = <T, XS extends Slice<T>>(p: PredForSlice<T>) =>
(list: XS): XS => dropWhileEnd(p, list)

;
19 changes: 10 additions & 9 deletions packages/fjl/src/list/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@ 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, XS extends Slice<T>>(pred: PredForSlice<T>, xs: XS): XS => {
let ind = 0;
const limit = xs.length,
isString = typeOf(xs) === 'String';
let out = isString ? '' : [] as T[];
let out = (isString ? '' : []) as unknown as typeof xs;
if (!limit) {
return out as Slice<T>;
return out;
}
if (typeof xs === 'string') {
if (isString) {
for (; ind < limit; ind++) {
if (pred(xs[ind] as unknown as T, ind, xs)) {
if (pred(xs[ind], ind, xs)) {
// @ts-ignore
out += xs[ind];
}
}
} else {
for (; ind < limit; ind++) {
if (pred(xs[ind] as T, ind, xs)) {
if (pred(xs[ind], ind, xs)) {
(out as T[]).push(xs[ind] as T);
}
}
}
return out as Slice<T>;
return out;
},

$filter = <T>(pred: PredForSlice<T>) =>
(xs: Slice<T>): Slice<T> => filter(pred, xs)
$filter = <T, XS extends Slice<T>>(pred: PredForSlice<T>) =>
(xs: XS): XS => filter(pred, xs)

;
6 changes: 3 additions & 3 deletions packages/fjl/src/list/iterate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {Unary} from "../types";

export const

/**
* iterates `f(x)` and returns a list of repeated applications of `f` to `x` `limit` number of times.
*/
iterate = <T>(n: number, op: Unary<T>, x: T): T[] => {
let ind = 0,
lastX: T = x;
Expand All @@ -14,9 +17,6 @@ export const
return out;
},

/**
* iterate `f(x)` returns a list of repeated applications of `f` to `x` `limit` number of times.
*/
$iterate = <T>(n: number) =>
(op: Unary<T>) =>
(x: T): T[] => iterate(n, op, x),
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/list/repeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {id} from "../function";
export const

/**
* Returns a an array containing `x` repeated `n` number of times.
* Returns an array containing `x` repeated `n` number of times.
*/
repeat = <T>(n: number, x: T): T[] => n <= 0 ? [] : iterate(n, a => a, x),

Expand Down
6 changes: 3 additions & 3 deletions packages/fjl/src/list/splitAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const
* Splits `x` in two at given `index` (exclusive (includes element/character at
* given index in second part of returned list)).
*/
splitAt = <T>(ind: number, list: Slice<T>): [Slice<T>, Slice<T>] =>
splitAt = <T, XS extends Slice<T>>(ind: number, list: XS): [XS, XS] =>
[sliceTo(ind, list), sliceFrom(ind, list)],

$splitAt = <T>(ind: number) =>
(list: Slice<T>): [Slice<T>, Slice<T>] =>
$splitAt = <T, XS extends Slice<T>>(ind: number) =>
(list: XS): [XS, XS] =>
splitAt(ind, list);
2 changes: 1 addition & 1 deletion packages/fjl/src/list/tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/
import {Slice} from "../types";

export const tail = (xs: Slice): Slice => xs.slice(1);
export const tail = <T extends Slice>(xs: T): T => xs.slice(1) as typeof xs;
1 change: 0 additions & 1 deletion packages/fjl/src/list/union.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {append} from "./append";
import {filter} from "./filter";
import {includes, } from "../platform/slice";
import {Slice} from "../types";

export const
/**
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/list/utils/sliceFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const
/**
* Returns a slice of the given list from `startInd` to the end of the list.
*/
sliceFrom = <T = any, T2 extends Slice<T> = Slice<T>>(startInd: number, xs: T2): T2 => slice(startInd, undefined, xs) as T2,
sliceFrom = <T = any, T2 extends Slice<T> = Slice<T>>(startInd: number, xs: T2): T2 =>
slice(startInd, undefined, xs) as typeof xs,

/**
* Curried version of `sliceFrom`.
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/list/utils/sliceTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const
/**
* Slices from index `0` to given index.
*/
sliceTo = <T = any, T2 extends Slice<T> = Slice<T>>(toInd: number, xs: T2): T2 => slice(0, toInd, xs) as T2,
sliceTo = <T = any, T2 extends Slice<T> = Slice<T>>(toInd: number, xs: T2): T2 =>
slice(0, toInd, xs) as typeof xs,

/**
* Curried version of `sliceTo`.
Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/platform/slice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const
/**
* Same as `(Array|String).prototype.slice`.
*/
slice = (start: number, end: number, xs: Slice): Slice => xs.slice(start, end),
slice = <T, XS extends Slice<T>>(start: number, end: number, xs: XS): XS =>
xs.slice(start, end) as typeof xs,

/**
* Same as `(Array|String).prototype.slice`.
Expand Down
4 changes: 2 additions & 2 deletions packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export type Indexable<T = any> = StringIndexable<T> | NumberIndexable<T>;
export interface SliceBase<T = any> {
readonly length: number;

slice(from: number, to?: number): Slice<T>;
slice(from: number, to?: number): typeof this;

concat(...xs: (ConcatArray<T> | string | Slice<T>)[]): Slice<T>;
concat(...xs: (ConcatArray<T> | string | Slice<T>)[]): typeof this;

indexOf(x: T, position?: number): number;

Expand Down
3 changes: 2 additions & 1 deletion packages/fjl/src/types/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export type MapAccumOp<A = any, B = any, C = any, Ind = number | string, Functor

export type PredForIndexable<T = any> = IndexableTernaryPred<T, number | string, Indexable<T>>;

export type PredForSlice<T = any, T2 extends Slice<T> = Slice<T>> = ArrayTernaryPred<T, number | string, T2>;
export type PredForSlice<T = any, T2 extends Slice<T> = Slice<T>> =
ArrayTernaryPred<T, number | string, T2>;

export interface Foldable<T> extends Functor<T> {
reduce<RetT>(fn: ReduceOp<T, Foldable<T>, RetT>): RetT;
Expand Down
3 changes: 1 addition & 2 deletions packages/fjl/tests/list/test-cycle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {cycle} from "../../src";
import {Slice} from "../../src/types/data";

describe('#cycle', () => {
const arg = ['a'];
for (let i = 0; i < 5; i += 1) {
const expected = new Array(i).fill(arg, 0, i).flatMap(xs => xs),
result = cycle(i, arg) as Slice<string[]>;
result = cycle(i, arg);
((_result, _expected): void => {
it(`cycle(${i}, ["${arg.join('", "')}"]) === ["${_expected.join('", "')}"]`, () => {
expect(_result).toEqual(_expected);
Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/tests/list/test-difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('#difference', () => {
// @todo tablelize test messages
it('should return an empty list when first list passed in is empty, ' +
' there are no differences between passed in lists, ', () => {
(<[[Slice<any>, Slice<any>], Slice<any>][]>[
(<[Parameters<typeof difference>, Slice][]>[
[[[], []], []],
[['', ''], []],
[[null, undefined], []]
Expand Down

0 comments on commit 1e51d96

Please sign in to comment.