Skip to main content

reverse

reverse() function

Returns Iterable/AsyncIterable of the given elements in reverse order. Note:Evaluates all Iterable, except for array, and returns a lazy-reversed iterator.

Signature:
declare function reverse<T extends Iterable<unknown> | AsyncIterable<unknown>>(iterable: T): ReturnIterableIteratorType<T>;

Example

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

const iter2 = reverse("abc");
iter2.next(); // {value: "c", done: false}
iter2.next(); // {value: "b", done: false}
iter2.next(); // {value: "a", done: false}
iter2.next(); // {value: undefined, done: true}

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

pipe(
"abcde",
reverse,
toArray,
); // "edcba"

see pipe toArray

Open Source Code