A.Vector.take

You're seeing just the function take, go back to A.Vector module for more information.

Specs

take(t(val), integer()) :: t(val) when val: value()

Takes an amount of elements from the beginning or the end of the vector.

If a positive amount is given, it takes the amount elements from the beginning of the vector.

If a negative amount is given, the amount of elements will be taken from the end.

If amount is 0, it returns the empty vector.

Time complexity is:

  • effective constant time when amount is positive, as the vector structure can be shared
  • linear when amount is negative, as the vector needs to be reconstructed.

Examples

iex> A.Vector.new(0..100) |> A.Vector.take(10)
vec([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
iex> A.Vector.new([:only_one]) |> A.Vector.take(1000)
vec([:only_one])
iex> A.Vector.new(0..10) |> A.Vector.take(-5)
vec([6, 7, 8, 9, 10])