Skip to content

Commit 3f2b352

Browse files
committed
feat: pass arguments to onError in Either.tryCatch
1 parent 967f10a commit 3f2b352

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

benchmarks/array.bench.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ import * as A from "../array.ts";
22
import * as N from "../number.ts";
33
import { pipe } from "../fn.ts";
44

5-
const count = 100_000;
5+
const count = 1_000_000;
66
const sorted = A.range(count);
77
const binarySearch = A.binarySearch(N.SortableNumber);
88
const monoSearch = A.monoSearch(N.SortableNumber);
99
const searches = pipe(A.range(count), A.map(() => Math.random() * count));
10+
const expensiveOperation = (n: number) =>
11+
((n ** Math.floor(n)) / (n ** (Math.floor(n) - 1))) +
12+
((n ** Math.floor(n - 2)) / (n ** Math.floor(n - 3)));
13+
const reducer = (acc: number, value: number) => acc + expensiveOperation(value);
14+
15+
Deno.bench("array native findIndex", { group: "binarySearch" }, () => {
16+
searches.forEach((value) => sorted.findIndex((n) => n <= value));
17+
});
1018

1119
Deno.bench("array binarySearch", { group: "binarySearch" }, () => {
1220
searches.forEach((value) => binarySearch(value, sorted));
@@ -15,3 +23,19 @@ Deno.bench("array binarySearch", { group: "binarySearch" }, () => {
1523
Deno.bench("array monoSearch", { group: "binarySearch" }, () => {
1624
searches.forEach((value) => monoSearch(value, sorted));
1725
});
26+
27+
Deno.bench("array map", { group: "map" }, () => {
28+
pipe(searches, A.map(expensiveOperation));
29+
});
30+
31+
Deno.bench("array native map", { group: "map" }, () => {
32+
searches.map(expensiveOperation);
33+
});
34+
35+
Deno.bench("array fold", { group: "fold" }, () => {
36+
pipe(searches, A.fold(reducer, 0));
37+
});
38+
39+
Deno.bench("array native reduce", { group: "fold" }, () => {
40+
searches.reduce(reducer, 0);
41+
});

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@baetheus/fun",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"exports": {
55
"./ideas/dux": "./ideas/dux.ts",
66
"./applicable": "./applicable.ts",

either.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ export function fromNullable<E>(
233233
*/
234234
export function tryCatch<E, A, AS extends unknown[]>(
235235
fn: (...as: AS) => A,
236-
onError: (e: unknown) => E,
236+
onError: (e: unknown, as: AS) => E,
237237
): (...as: AS) => Either<E, A> {
238238
return (...as: AS) => {
239239
try {
240240
return right(fn(...as));
241241
} catch (e) {
242-
return left(onError(e));
242+
return left(onError(e, as));
243243
}
244244
};
245245
}

0 commit comments

Comments
 (0)