Skip to content

Commit 3fd828b

Browse files
committed
feat(product): add product function
1 parent fdb2ced commit 3fd828b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
notEmpty,
2727
only,
2828
prefixMatch,
29+
product,
2930
push,
3031
remove,
3132
removeFirst,
@@ -256,3 +257,9 @@ test("sum", async t => {
256257
t.is(await sum(asyncIterable([1, 2, 3])), 6);
257258
t.is(await sum(asyncIterable([])), 0);
258259
});
260+
261+
test("product", async t => {
262+
t.is(await product(asyncIterable([1, 2, 3])), 6);
263+
t.is(await product(asyncIterable([1, 2, 3, 2])), 12);
264+
t.is(await product(asyncIterable([])), 1);
265+
});

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,3 +830,9 @@ export async function sum(iterable: AsyncIterableLike<number>): Promise<number>
830830
}
831831

832832
export const asyncSum = sum;
833+
834+
export async function product(iterable: AsyncIterableLike<number>): Promise<number> {
835+
return fold(iterable, (product, element) => product * element, 1);
836+
}
837+
838+
export const asyncProduct = product;

0 commit comments

Comments
 (0)