Packages@accelint/coreArray
reverse
Returns a new array with elements in reversed order
Returns a new array with the order of elements reversed. Does not mutate the original array.
Usage
import { reverse } from '@accelint/core';
// Reverse an array
const reversed = reverse([1, 2, 3, 4, 5]);
// [5, 4, 3, 2, 1]Reference
function reverse<T>(
arr: T[]
): T[]Parameters
| Parameter | Type | Description |
|---|---|---|
arr | T[] | The array to reverse. |
Returns
Returns a new array with elements in reversed order.
Type Parameters
T- The type of elements in the array
Examples
Example: Reversing different types
import { reverse } from '@accelint/core';
const numbers = reverse([1, 2, 3, 4, 5]);
// [5, 4, 3, 2, 1]
const strings = reverse(['a', 'b', 'c']);
// ['c', 'b', 'a']Example: Original array unchanged
import { reverse } from '@accelint/core';
const original = [1, 2, 3];
const reversed = reverse(original);
console.log(original); // [1, 2, 3]
console.log(reversed); // [3, 2, 1]Example: Processing in reverse order
import { reverse } from '@accelint/core';
interface Event {
id: number;
timestamp: Date;
}
const events: Event[] = [
{ id: 1, timestamp: new Date('2026-07-13T10:00:00') },
{ id: 2, timestamp: new Date('2026-07-13T11:00:00') },
{ id: 3, timestamp: new Date('2026-07-13T12:00:00') }
];
// Show most recent first
const recentFirst = reverse(events);
// [{ id: 3, ... }, { id: 2, ... }, { id: 1, ... }]Example: Composing with other functions
import { reverse, map, pipe } from '@accelint/core';
const processInReverse = pipe(
reverse,
map((x: number) => x * 2)
);
processInReverse([1, 2, 3, 4]);
// [8, 6, 4, 2]Example: Palindrome check
import { reverse } from '@accelint/core';
function isPalindrome(arr: number[]): boolean {
const reversed = reverse(arr);
return arr.every((val, idx) => val === reversed[idx]);
}
isPalindrome([1, 2, 3, 2, 1]); // true
isPalindrome([1, 2, 3, 4, 5]); // falseGood to know: This is a pure function that always returns a new array. The original array is never modified.
Related
- slice - Extract portion of array
- map - Transform array elements
- reduceRight - Reduce from right to left