This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e98c8a
commit 177c754
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export function stepBy<T>(n: number): (_: Iterator<T>) => Iterator<T> { | ||
return (iter) => { | ||
let isFirstTake = true; | ||
|
||
return { | ||
next() { | ||
if (isFirstTake) { | ||
isFirstTake = false; | ||
return iter.next(); | ||
} else { | ||
for (let i = 0; i < n - 1; i++) { | ||
const result = iter.next(); | ||
if (result.done) return result; | ||
} | ||
|
||
return iter.next(); | ||
} | ||
}, | ||
}; | ||
}; | ||
} | ||
|
||
export function asyncStepBy<T>( | ||
n: number, | ||
): (_: AsyncIterator<T>) => AsyncIterator<T> { | ||
return (iter) => { | ||
let isFirstTake = true; | ||
|
||
return { | ||
async next() { | ||
if (isFirstTake) { | ||
isFirstTake = false; | ||
return iter.next(); | ||
} else { | ||
for (let i = 0; i < n - 1; i++) { | ||
const result = await iter.next(); | ||
if (result.done) return result; | ||
} | ||
|
||
return iter.next(); | ||
} | ||
}, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { assertObjectMatch } from "../dev_deps.ts"; | ||
import { toAsync } from "./toAsync.ts"; | ||
|
||
import { asyncStepBy, stepBy } from "./stepBy.ts"; | ||
|
||
Deno.test("skip", () => { | ||
const a = [0, 1, 2, 3, 4, 5]; | ||
|
||
const iter = stepBy(2)(a.values()); | ||
assertObjectMatch(iter.next(), { value: 0 }); | ||
assertObjectMatch(iter.next(), { value: 2 }); | ||
assertObjectMatch(iter.next(), { value: 4 }); | ||
assertObjectMatch(iter.next(), { done: true }); | ||
}); | ||
|
||
Deno.test("asyncSkip", async () => { | ||
const a = [0, 1, 2, 3, 4, 5]; | ||
|
||
const iter = asyncStepBy(2)(toAsync(a.values())); | ||
assertObjectMatch(await iter.next(), { value: 0 }); | ||
assertObjectMatch(await iter.next(), { value: 2 }); | ||
assertObjectMatch(await iter.next(), { value: 4 }); | ||
assertObjectMatch(await iter.next(), { done: true }); | ||
}); |