Skip to content

Commit

Permalink
Add chunked()
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Casterline committed Apr 25, 2023
1 parent 19072f8 commit 86fae0f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
57 changes: 57 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ interface LazyShared<T> {
* removing it from the iterator.
*/
peekable(): Peekable<T> | PeekableAsync<T>

/**
* Iterate by chunks of a given size formed from the underlying Iterator.
*
* Each chunk will be exactly `size` until the last chunk, which may be
* from 1-size items long.
*/
chunked(size: number): LazyShared<T[]>
}

export class Lazy<T> implements Iterable<T>, LazyShared<T> {
Expand Down Expand Up @@ -488,6 +496,30 @@ export class Lazy<T> implements Iterable<T>, LazyShared<T> {
let inner = this.#inner
return Peekable.from(inner)
}

/**
* Iterate by chunks of a given size formed from the underlying Iterator.
*
* Each chunk will be exactly `size` until the last chunk, which may be
* from 1-size items long.
*/
chunked(size: number): Lazy<T[]> {
let inner = this.#inner
let gen = function* generator() {
let out: T[] = []
for (const item of inner) {
out.push(item)
if (out.length >= size) {
yield out
out = []
}
}
if (out.length > 0) {
yield out
}
}
return lazy(gen())
}
}


Expand Down Expand Up @@ -792,6 +824,31 @@ export class LazyAsync<T> implements AsyncIterable<T>, LazyShared<T> {
let inner = this.#inner
return PeekableAsync.from(inner)
}

/**
* Iterate by chunks of a given size formed from the underlying Iterator.
*
* Each chunk will be exactly `size` until the last chunk, which may be
* from 1-size items long.
*/
chunked(size: number): LazyAsync<T[]> {
let inner = this.#inner
let gen = async function* generator() {
let out: T[] = []
for await (const item of inner) {
out.push(item)
if (out.length >= size) {
yield out
out = []
}
}
if (out.length > 0) {
yield out
}
}
return lazy(gen())
}

}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/chunk_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { range } from "../mod.ts";
import { assertEquals, testBoth } from "./helpers.ts";

Deno.test(async function chunked(t: Deno.TestContext) {
await testBoth(t, range({to: 10}), async (iter) => {
let chunks = await iter.chunked(3).toArray()

assertEquals(chunks, [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9],
])
})

})

0 comments on commit 86fae0f

Please sign in to comment.