I have a list:
a = [1,2,4,5,6,7,8,9,9,88,88]
In Python, it's simple to get last n items:
a[-n:]
Whats equivalent in Elixir?
Use Enum.take/2
with a negative value:
iex(1)> list = [1, 2, 4, 5, 6, 7, 8, 9, 9, 88, 88]
iex(2)> Enum.take(list, -4) |> IO.inspect(charlists: :as_lists)
[9, 9, 88, 88]
take(enumerable, count)
[...]
count
must be an integer. If a negativecount
is given, the lastcount
values will be taken. [...]