Skip to main content

dropRight

dropRight() function

Returns all but the last length elements of the given iterable.

Signature:
declare function dropRight<A>(length: number, iterable: Iterable<A>): IterableIterator<A>;

declare function dropRight<A>(length: number, iterable: AsyncIterable<A>): AsyncIterableIterator<A>;

declare function dropRight<A extends Iterable<unknown> | AsyncIterable<unknown>>(length: number): (iterable: A) => ReturnIterableIteratorType<A>;

Example

const iter = dropRight(2, [1, 2, 3, 4]);
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 2}
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
[1, 2, 3, 4],
dropRight(2),
toArray,
); // [1, 2]

pipe(
"abcde",
dropRight(2),
toArray,
); // ["a", "b", "c"]

await pipe(
Promise.resolve([1, 2, 3, 4]),
dropRight(2),
toArray,
); // [1, 2]

// with toAsync
await pipe(
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), Promise.resolve(4)],
toAsync,
dropRight(2),
toArray,
); // [1, 2]

see pipe, toAsync, toArray

Open Source Code