Standard Toolkit
Packages@accelint/coreArray

shift

Returns a tuple of the first element and remaining elements of an array

Returns a tuple containing the first element (head) and the remaining elements (tail) of an array.

Usage

import { shift } from '@accelint/core';

// Destructure head and tail
const [head, tail] = shift([1, 2, 3, 4, 5]);
// head: 1
// tail: [2, 3, 4, 5]

Reference

function shift<T>(
  arr: T[]
): [T, T[]]

Parameters

ParameterTypeDescription
arrT[]The array to extract head and tail from.

Returns

Returns a tuple where the first element is the head (first element) and the second element is the tail (remaining elements).

Type Parameters

  • T - The type of elements in the array

Examples

Example: Processing first element separately

import { shift } from '@accelint/core';

const [first, rest] = shift([10, 20, 30, 40]);

console.log(`First: ${first}`); // "First: 10"
console.log(`Rest: ${rest}`); // "Rest: [20, 30, 40]"

Example: Recursive pattern

import { shift } from '@accelint/core';

function sumArray(arr: number[]): number {
  if (arr.length === 0) return 0;
  
  const [head, tail] = shift(arr);
  return head + sumArray(tail);
}

sumArray([1, 2, 3, 4, 5]); // 15

Example: Pattern matching style

import { shift } from '@accelint/core';

interface Item {
  id: number;
  value: string;
}

const items: Item[] = [
  { id: 1, value: 'first' },
  { id: 2, value: 'second' },
  { id: 3, value: 'third' }
];

const [current, remaining] = shift(items);
// current: { id: 1, value: 'first' }
// remaining: [{ id: 2, value: 'second' }, { id: 3, value: 'third' }]

Example: Queue operations

import { shift, push } from '@accelint/core';

// Dequeue operation
let queue = [1, 2, 3, 4];
const [item, newQueue] = shift(queue);
queue = newQueue;
// item: 1
// queue: [2, 3, 4]

// Enqueue operation
queue = push(queue)(5);
// queue: [2, 3, 4, 5]

Good to know: This function does not handle empty arrays. Ensure the array has at least one element before calling shift.

  • unshift - Add element to the start
  • push - Add element to the end
  • slice - Extract portion of array

On this page