Skip to main content
Module

x/aitertools/mod.ts>takeEnd

Well-tested utility functions dealing with async iterables
Go to Latest
function takeEnd
import { takeEnd } from "https://deno.land/x/aitertools@0.5.0/mod.ts";

Takes a specified number of elements from the end of an async iterable.

import { takeEnd } from "./take.ts";
import { range } from "./range.ts";

const iterable = takeEnd(range(10), 3);
for await (const value of iterable) console.log(value);

The above example will print the following 3 lines:

7
8
9

If the iterable is shorter than the specified number, the whole elements are taken.

import { takeEnd } from "./take.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; }
const iterable = takeEnd(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

Type Parameters

T

The type of the elements in the source and the returned async iterable.

Parameters

source: Iterable<T> | AsyncIterable<T>

The async iterable to take elements from. It can be either finite or infinite.

count: number

The number of elements to take. It must be a finite integer.

Returns

AsyncIterableIterator<T>

An async iterable that yields the last count elements from the source iterable.