Skip to content

Commit

Permalink
Merge branch 'main' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Mar 10, 2021
2 parents b511737 + 4434250 commit fda54d8
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
109 changes: 109 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import test from "ava";
import {
all,
and,
any,
append,
concat,
concatMap,
contains,
dropWhile,
empty,
Expand All @@ -22,11 +28,20 @@ import {
last,
map,
maximum,
minimum,
or,
partition,
partitionWhile,
prepend,
product,
remove,
removeFirst,
scan,
scan1,
scanRight,
scanRight1,
slice,
sum,
tail,
takeWhile
} from "./index";
Expand Down Expand Up @@ -212,6 +227,100 @@ test("maximum", t => {
t.is(maximum([]), null);
});

test("minimum", t => {
t.is(minimum([1, 2, 3]), 1);
t.is(minimum([2, 3, 4, 1, 2, 3]), 1);
t.is(minimum([]), null);
});

test("sum", t => {
t.is(sum([1, 2, 3]), 6);
t.is(sum([]), 0);
});

test("product", t => {
t.is(product([1, 2, 3]), 6);
t.is(product([]), 1);
});

test("and", t => {
t.true(and([true, true, true]));
t.false(and([true, false, true]));
t.true(and([]));
});

test("or", t => {
t.true(or([true, false, true]));
t.false(or([false, false, false]));
t.false(or([]));
});

test("any", t => {
t.true(any([1, 2, 3], e => e > 2));
t.false(any([1, 2, 3], e => e > 4));
});

test("all", t => {
t.true(all([1, 2, 3], e => e < 4));
t.false(all([1, 2, 3], e => e > 2));
});

test("concat", t => {
t.deepEqual(concat([[1, 2], [], [3], [4, 5]]), [1, 2, 3, 4, 5]);
t.deepEqual(concat([[], []]), []);
});

test("prepend", t => {
t.deepEqual(prepend([1, 2, 3])([4, 5, 6]), [1, 2, 3, 4, 5, 6]);
t.deepEqual(prepend<number>([])([4, 5, 6]), [4, 5, 6]);
t.deepEqual(prepend([1, 2, 3])([]), [1, 2, 3]);
});

test("append", t => {
t.deepEqual(append([4, 5, 6])([1, 2, 3]), [1, 2, 3, 4, 5, 6]);
t.deepEqual(append<number>([])([1, 2, 3]), [1, 2, 3]);
t.deepEqual(append([4, 5, 6])([]), [4, 5, 6]);
});

test("concatMap", t => {
t.deepEqual(
concatMap(["1,2,3", "4,5,6"], s => s.split(",")),
["1", "2", "3", "4", "5", "6"]
);
});

test("scan", t => {
t.deepEqual(
scan([1, 2, 3], (a, e, i) => a + e * i, 0),
[0, 2, 8]
);
t.deepEqual(
scan(["a", "b", "c"], (a, e, i) => `${a} ${i} ${e}`, "_"),
["_ 0 a", "_ 0 a 1 b", "_ 0 a 1 b 2 c"]
);
});

test("scan1", t => {
t.deepEqual(
scan1([1, 2, 3], (a, e, i) => a + e * i),
[1, 3, 9]
);
});

test("scanRight", t => {
t.deepEqual(
scanRight(["a", "b", "c"], (a, e, i) => `${a} ${i} ${e}`, "_"),
["_ 2 c 1 b 0 a", "_ 2 c 1 b", "_ 2 c"]
);
});

test("scanRight1", t => {
t.deepEqual(
scanRight1(["a", "b", "c"], (a, e, i) => `${a} ${i} ${e}`),
["c 1 b 0 a", "c 1 b", "c"]
);
});

type Result<T> = Success<T> | Error;

interface Success<T> {
Expand Down
48 changes: 48 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,30 @@ export function scanFn<T, U>(
return array => scan(array, f, initial);
}

export function scan1<T>(
array: ArrayLike<T>,
f: (accumulator: T, element: T, index: number) => T
): T[] {
if (array.length === 0) {
return [];
}

let accumulator = array[0];
const result: T[] = copy({0: accumulator, length: array.length});

for (let i = 1; i < array.length; ++i) {
result[i] = accumulator = f(accumulator, array[i], i);
}

return result;
}

export function scan1Fn<T>(
f: (accumulator: T, element: T, index: number) => T
): (array: ArrayLike<T>) => T[] {
return array => scan1(array, f);
}

export function scanRight<T, U>(
array: ArrayLike<T>,
f: (accumulator: U, element: T, index: number) => U,
Expand All @@ -630,6 +654,30 @@ export function scanRightFn<T, U>(
return array => scanRight(array, f, initial);
}

export function scanRight1<T>(
array: ArrayLike<T>,
f: (accumulator: T, element: T, index: number) => T
): T[] {
if (array.length === 0) {
return [];
}

let accumulator = array[array.length - 1];
const result: T[] = copy({[array.length - 1]: accumulator, length: array.length});

for (let i = array.length - 2; i >= 0; --i) {
result[i] = accumulator = f(accumulator, array[i], i);
}

return result;
}

export function scanRight1Fn<T>(
f: (accumulator: T, element: T, index: number) => T
): (array: ArrayLike<T>) => T[] {
return array => scanRight1(array, f);
}

export function partition<T, U extends T>(
array: ArrayLike<T>,
predicate: (element: T) => element is U
Expand Down

0 comments on commit fda54d8

Please sign in to comment.