We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
To efficiently work with and transform lists in Elixir, you will likely need utilize a list reversing function from time to time. Your best bet is to reach for the Erlang implementation which is available as part of the lists module.
Here are a couple examples of how to use it:
> :lists.reverse([1,2,3])
[3, 2, 1]
> :lists.reverse([1, :a, true, "what", 5])
[5, "what", true, :a, 1]
Note: though I said "transform lists" above, what is actually going on is that a new version of the list representing my transformation is being created, per Elixir's functional nature.
Elixir now has a built-in function for reversing lists. In fact, it works with anything that implements the Enumerable protocol.
> Enum.reverse([1,2,3,4,5])
[5, 4, 3, 2, 1]
> Enum.reverse(%{a: 1, b: 2, c: 3})
[c: 3, b: 2, a: 1]
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.