Skip to content

Commit

Permalink
issue-#57 - Updated set theory methods for 'list' type.
Browse files Browse the repository at this point in the history
  • Loading branch information
elycruz committed Aug 14, 2022
1 parent bd67a48 commit 09738b3
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 5,648 deletions.
5,735 changes: 117 additions & 5,618 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"@rollup/plugin-typescript": "^8.3.0",
"@types/jest": "^27.0.2",
"@types/node": "^13.13.47",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"del": "^5.1.0",
"eslint": "^8.1.0",
"eslint": "^8.22.0",
"jest": "^27.3.1",
"jest-cli": "^27.3.1",
"rollup": "^2.58.3",
"rollup": "^2.77.3",
"rollup-plugin-terser": "^7.0.2",
"ts-jest": "^27.0.7",
"ts-node": "^9.1.1",
Expand Down
12 changes: 5 additions & 7 deletions packages/fjl/src/list/complement.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
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 arrays.reduce((agg: T[], arr: T[]) =>
append(agg, difference(arr, arr0)), []);
},

/**
* 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)

;
12 changes: 5 additions & 7 deletions packages/fjl/src/list/difference.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {reduce} from "./utils";
import {sliceCopy} from "./utils/sliceCopy";
import {includes} from "../platform/slice";
import {Slice} from "../types";
import {reduce, sliceCopy} from "./utils";
import {includes} from "./includes";

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 +21,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);
8 changes: 4 additions & 4 deletions packages/fjl/src/list/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export const
* @param arr2 {Array}
* @returns {Array}
*/
union = <T>(arr1: Slice<T>, arr2: Slice<T>): Slice<T> =>
union = <T>(arr1: T[], arr2: T[]): T[] =>
append(arr1,
filter(elm => !includes(arr1, elm), arr2) as Slice<T>
filter(elm => !includes(arr1, elm), arr2) as T[]
),

$union = <T>(arr1: Slice<T>) =>
(arr2: Slice<T>): Slice<T> => union(arr1, arr2)
$union = <T>(arr1: T[]) =>
(arr2: T[]): T[] => union(arr1, arr2)
;
4 changes: 2 additions & 2 deletions packages/fjl/src/platform/slice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {Slice} from "../../types";

export const

concat = (...xss: Slice[]): Slice =>
(!xss ? xss : xss.shift().concat(...xss as any[])) as Slice,
concat = <X, XS extends Slice<X>>(...xss: XS[]): XS =>
(!xss ? xss : xss.shift().concat(...xss as XS[])) as XS,

$concat = a => (...b) => concat(a, ...b),

Expand Down
2 changes: 1 addition & 1 deletion packages/fjl/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface SliceBase<T = any> {

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

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

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

Expand Down
8 changes: 5 additions & 3 deletions packages/fjl/tests/list/test-complement.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import {expectEqual, expectFunction} from "../helpers";
import {complement} from "../../src";
import {Slice} from "../../src/types/data";

describe('#complement', () => {
it('should be a function', () => {
expectFunction(complement);
});
it('should return an empty list when receiving 2 or more values consisting of ' +
'`null`, `undefined` and/or empty list (`\'\'`, `[]`).', () => {
(<Slice<any>[]>[
(<any[][]>[
[undefined, undefined],
[null, null, '', null],
[[], null, undefined],
Expand All @@ -19,7 +18,10 @@ describe('#complement', () => {
['', undefined, ''],
[undefined, '', [], null]
])
.forEach(args => expectEqual(complement(...args), []));
.forEach(args => {
console.log(`complement(${args.join(', ')}) === []`);
expect(complement(...args)).toEqual([])
});
});
it('should return elements not in first list passed to it', () => {
const testCases: Array<[number[][], number, number[]]> = [
Expand Down
3 changes: 1 addition & 2 deletions packages/fjl/tests/list/test-union.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {append, range, union} from "../../src/list";
import {range, union} from "../../src/list";
import {expectEqual} from "../helpers";

describe('#union', () => {
Expand Down Expand Up @@ -55,7 +55,6 @@ describe('#union', () => {
});
});
it('should return an empty list when receiving empty lists', () => {
expectEqual(union('', ''), '');
expectEqual(union([], []), []);
});
});

0 comments on commit 09738b3

Please sign in to comment.