Skip to content

Commit

Permalink
small fix to reduce API
Browse files Browse the repository at this point in the history
  • Loading branch information
j50n committed Oct 18, 2023
1 parent 2f50261 commit a6b7ec8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion site/scripts/process/output.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { run } from "https://deno.land/x/proc@0.21.2/mod.ts";
import { run } from "https://deno.land/x/proc@0.21.3/mod.ts";

await run("echo", "Hello, world.").forEach((it) => console.dir(it));

Expand Down
32 changes: 16 additions & 16 deletions site/scripts/process/perf1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function* countArray() {
for (let j = 0; j < 1_000; j++) {
arr[j] = i * 1_000 + j;
}
yield arr
yield arr;
}
}

Expand All @@ -36,10 +36,10 @@ Deno.bench("naive async add with range and filter/map/reduce", async () => {
});

Deno.bench("async grouped add with filter/map/reduce", async () => {
/*
* By combining into arrays and doing the calculations on the groups of values,
* and only reducing at the end, the speedup is about 50x.
*/
/*
* By combining into arrays and doing the calculations on the groups of values,
* and only reducing at the end, the speedup is about 50x.
*/
await enumerate(countArray())
.map((items) => {
return items
Expand All @@ -51,20 +51,20 @@ Deno.bench("async grouped add with filter/map/reduce", async () => {
});

Deno.bench("async grouped add with filter/map/reduce as for loop", async () => {
/*
* This is only a little bit faster. Memory allocation overhead. You have the
* same problem with fast-add if you don't cheat and pre-allocate the array.
* Still, about 30% faster than the version using higher order functions.
*/
/*
* This is only a little bit faster. Memory allocation overhead. You have the
* same problem with fast-add if you don't cheat and pre-allocate the array.
* Still, about 30% faster than the version using higher order functions.
*/
await enumerate(countArray())
.map((items) => {
let acc = 0
for(const item of items){
if(item % 2 === 0){
acc += item * 2
}
let acc = 0;
for (const item of items) {
if (item % 2 === 0) {
acc += item * 2;
}
return acc
}
return acc;
})
.reduce((acc, item) => acc + item);
});
Expand Down
22 changes: 11 additions & 11 deletions src/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,22 @@ export class Enumerable<T> implements AsyncIterable<T> {
filterFn?: (item: T) => boolean | Promise<boolean>,
): Promise<number> {
if (filterFn == null) {
let count = 0
for await(const _item of this.iter){
count++
let count = 0;
for await (const _item of this.iter) {
count++;
}
return count
return count;
} else {
let count = 0
for await (const item of this.iter){
if(filterFn(item)){
count ++
let count = 0;
for await (const item of this.iter) {
if (filterFn(item)) {
count++;
}
}
return count
return count;
}
}

/**
* Filter the sequence to exclude the items that pass a test. This returns the
* inverse of {@link filter}.
Expand Down Expand Up @@ -473,7 +473,7 @@ export class Enumerable<T> implements AsyncIterable<T> {
*/
async reduce<U>(
reduceFn: (acc: U, item: T, index: number) => U | Promise<U>,
zero?: U,
zero: U,
): Promise<U>;

async reduce<U>(
Expand Down

0 comments on commit a6b7ec8

Please sign in to comment.