Method: Immutable::Vector#rotate

Defined in:
lib/immutable/_core.rb

#rotate(count = 1) ⇒ Vector

Return a new Vector with the same elements, but rotated so that the one at index count is the first element of the new vector. If count is positive, the elements will be shifted left, and those shifted past the lowest position will be moved to the end. If count is negative, the elements will be shifted right, and those shifted past the last position will be moved to the beginning.

Examples:

v = Immutable::Vector["A", "B", "C", "D", "E", "F"]
v.rotate(2)   # => Immutable::Vector["C", "D", "E", "F", "A", "B"]
v.rotate(-1)  # => Immutable::Vector["F", "A", "B", "C", "D", "E"]

Parameters:

  • count (Integer) (defaults to: 1)

    The number of positions to shift items by

Returns:



1526
1527
1528
1529
# File 'lib/immutable/_core.rb', line 1526

def rotate(count = 1)
  return self if (count % @size) == 0
  self.class.new(((array = to_a).frozen? ? array.rotate(count) : array.rotate!(count)).freeze)
end