import { take } from "https://deno.land/x/aitertools@0.6.0/src/take.ts";
Takes a specified number of elements from the beginning of an async iterable.
import { take } from "./take.ts";
import { count } from "./infinite.ts";
const iterable = take(count(0, 5), 3);
for await (const value of iterable) {
console.log(value);
}
The above example will print the following 3 lines:
0
5
10
If the iterable is shorter than the specified number, the whole elements are taken.
import { take } from "./take.ts";
async function* gen() { yield "foo"; yield "bar"; yield "baz"; }
const iterable = take(gen(), 5);
for await (const value of iterable) console.log(value);
The above example will print only 3 elements, because gen()
yields only 3
elements:
foo
bar
baz