Skip to content

Commit fda54d8

Browse files
committed
Merge branch 'main' into alpha
2 parents b511737 + 4434250 commit fda54d8

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

index.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import test from "ava";
22
import {
3+
all,
4+
and,
5+
any,
6+
append,
7+
concat,
8+
concatMap,
39
contains,
410
dropWhile,
511
empty,
@@ -22,11 +28,20 @@ import {
2228
last,
2329
map,
2430
maximum,
31+
minimum,
32+
or,
2533
partition,
2634
partitionWhile,
35+
prepend,
36+
product,
2737
remove,
2838
removeFirst,
39+
scan,
40+
scan1,
41+
scanRight,
42+
scanRight1,
2943
slice,
44+
sum,
3045
tail,
3146
takeWhile
3247
} from "./index";
@@ -212,6 +227,100 @@ test("maximum", t => {
212227
t.is(maximum([]), null);
213228
});
214229

230+
test("minimum", t => {
231+
t.is(minimum([1, 2, 3]), 1);
232+
t.is(minimum([2, 3, 4, 1, 2, 3]), 1);
233+
t.is(minimum([]), null);
234+
});
235+
236+
test("sum", t => {
237+
t.is(sum([1, 2, 3]), 6);
238+
t.is(sum([]), 0);
239+
});
240+
241+
test("product", t => {
242+
t.is(product([1, 2, 3]), 6);
243+
t.is(product([]), 1);
244+
});
245+
246+
test("and", t => {
247+
t.true(and([true, true, true]));
248+
t.false(and([true, false, true]));
249+
t.true(and([]));
250+
});
251+
252+
test("or", t => {
253+
t.true(or([true, false, true]));
254+
t.false(or([false, false, false]));
255+
t.false(or([]));
256+
});
257+
258+
test("any", t => {
259+
t.true(any([1, 2, 3], e => e > 2));
260+
t.false(any([1, 2, 3], e => e > 4));
261+
});
262+
263+
test("all", t => {
264+
t.true(all([1, 2, 3], e => e < 4));
265+
t.false(all([1, 2, 3], e => e > 2));
266+
});
267+
268+
test("concat", t => {
269+
t.deepEqual(concat([[1, 2], [], [3], [4, 5]]), [1, 2, 3, 4, 5]);
270+
t.deepEqual(concat([[], []]), []);
271+
});
272+
273+
test("prepend", t => {
274+
t.deepEqual(prepend([1, 2, 3])([4, 5, 6]), [1, 2, 3, 4, 5, 6]);
275+
t.deepEqual(prepend<number>([])([4, 5, 6]), [4, 5, 6]);
276+
t.deepEqual(prepend([1, 2, 3])([]), [1, 2, 3]);
277+
});
278+
279+
test("append", t => {
280+
t.deepEqual(append([4, 5, 6])([1, 2, 3]), [1, 2, 3, 4, 5, 6]);
281+
t.deepEqual(append<number>([])([1, 2, 3]), [1, 2, 3]);
282+
t.deepEqual(append([4, 5, 6])([]), [4, 5, 6]);
283+
});
284+
285+
test("concatMap", t => {
286+
t.deepEqual(
287+
concatMap(["1,2,3", "4,5,6"], s => s.split(",")),
288+
["1", "2", "3", "4", "5", "6"]
289+
);
290+
});
291+
292+
test("scan", t => {
293+
t.deepEqual(
294+
scan([1, 2, 3], (a, e, i) => a + e * i, 0),
295+
[0, 2, 8]
296+
);
297+
t.deepEqual(
298+
scan(["a", "b", "c"], (a, e, i) => `${a} ${i} ${e}`, "_"),
299+
["_ 0 a", "_ 0 a 1 b", "_ 0 a 1 b 2 c"]
300+
);
301+
});
302+
303+
test("scan1", t => {
304+
t.deepEqual(
305+
scan1([1, 2, 3], (a, e, i) => a + e * i),
306+
[1, 3, 9]
307+
);
308+
});
309+
310+
test("scanRight", t => {
311+
t.deepEqual(
312+
scanRight(["a", "b", "c"], (a, e, i) => `${a} ${i} ${e}`, "_"),
313+
["_ 2 c 1 b 0 a", "_ 2 c 1 b", "_ 2 c"]
314+
);
315+
});
316+
317+
test("scanRight1", t => {
318+
t.deepEqual(
319+
scanRight1(["a", "b", "c"], (a, e, i) => `${a} ${i} ${e}`),
320+
["c 1 b 0 a", "c 1 b", "c"]
321+
);
322+
});
323+
215324
type Result<T> = Success<T> | Error;
216325

217326
interface Success<T> {

index.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,30 @@ export function scanFn<T, U>(
608608
return array => scan(array, f, initial);
609609
}
610610

611+
export function scan1<T>(
612+
array: ArrayLike<T>,
613+
f: (accumulator: T, element: T, index: number) => T
614+
): T[] {
615+
if (array.length === 0) {
616+
return [];
617+
}
618+
619+
let accumulator = array[0];
620+
const result: T[] = copy({0: accumulator, length: array.length});
621+
622+
for (let i = 1; i < array.length; ++i) {
623+
result[i] = accumulator = f(accumulator, array[i], i);
624+
}
625+
626+
return result;
627+
}
628+
629+
export function scan1Fn<T>(
630+
f: (accumulator: T, element: T, index: number) => T
631+
): (array: ArrayLike<T>) => T[] {
632+
return array => scan1(array, f);
633+
}
634+
611635
export function scanRight<T, U>(
612636
array: ArrayLike<T>,
613637
f: (accumulator: U, element: T, index: number) => U,
@@ -630,6 +654,30 @@ export function scanRightFn<T, U>(
630654
return array => scanRight(array, f, initial);
631655
}
632656

657+
export function scanRight1<T>(
658+
array: ArrayLike<T>,
659+
f: (accumulator: T, element: T, index: number) => T
660+
): T[] {
661+
if (array.length === 0) {
662+
return [];
663+
}
664+
665+
let accumulator = array[array.length - 1];
666+
const result: T[] = copy({[array.length - 1]: accumulator, length: array.length});
667+
668+
for (let i = array.length - 2; i >= 0; --i) {
669+
result[i] = accumulator = f(accumulator, array[i], i);
670+
}
671+
672+
return result;
673+
}
674+
675+
export function scanRight1Fn<T>(
676+
f: (accumulator: T, element: T, index: number) => T
677+
): (array: ArrayLike<T>) => T[] {
678+
return array => scanRight1(array, f);
679+
}
680+
633681
export function partition<T, U extends T>(
634682
array: ArrayLike<T>,
635683
predicate: (element: T) => element is U

0 commit comments

Comments
 (0)