I am trying to grasp the difference between Lists and Tuples in Elixir
. From the Basic Types section of Elixir Guides, I understand that:
- Lists are stored as Linked Items
- Updating a List is fast (only when prepending)
- Fetching List items is slow
- Fetching List information (size/length) is slow
- Tuple Elements are stored together
- Getting Tuple information is fast
- Fetching Tuple elements is fast
- Modifying Tuples is expensive
Okay, that's all fine but I'm still not sure what to use when. I see that most of the methods return a tuple but everywhere else Lists are used, and many methods accept Lists as input, not tuples. By the stated points above, shouldn't Tuples be used for passing data around, since reading from a tuple of user-given values would be fast?
I also noticed Tuples aren't enumerable, what's up with that? Wouldn't using Enum
over them be faster than using it on Lists?
If someone could help me understand them better, possibly by giving a few examples of what to use when, that'd be awesome.