Today I Learned

hashrocket A Hashrocket project

Get last nth elements from Enumerable in #Elixir

You probably know about Enum.take(n) where n is a number dictating how many elements you want to take from an Enumerable. Use it like this:

[1, 2, 3, 4, 5]
|> Enum.take(3)
# => [1, 2, 3]

But how can you get the last 3 elements? Just use a negative number!

[1, 2, 3, 4, 5]
|> Enum.take(-3)
# => [3, 4, 5]
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.