@mirei/ts-collections
    Preparing search index...

    Function rotate

    • Returns a deferred sequence that rotates the elements by the specified offset while preserving length.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • shift: number

        Number of positions to rotate. Positive values move elements toward the end (left rotation); negative values move them toward the beginning (right rotation).

      Returns IEnumerable<TElement>

      A sequence containing the same elements shifted by the requested amount.

      The source is buffered sufficiently to honour the rotation. Rotation amounts larger than the length of source are normalised by that length, which may require buffering the full sequence.

      const numbers = [1, 2, 3, 4, 5];
      const rotated = rotate(numbers, 2).toArray();
      console.log(rotated); // [3, 4, 5, 1, 2]

      const rotatedNegative = rotate(numbers, -2).toArray();
      console.log(rotatedNegative); // [4, 5, 1, 2, 3]