So I am curious in how Elixir fetches an item from a keyword list internally.
Let’s say we have the following list: [one: 'blue', two: 'red', three: 'yellow'] it will be represented internally as [{:one, 'blue'}, {:two, 'red'}, ...] a list with tuples that said how does the process to fetch these values work, what works well, what doesn’t? Could someone point me to the function that does this and describe the process in more detail?
Let’s say we have the following list: [one: ‘blue’, two: ‘red’, three: ‘yellow’] it will be represented internally as [{:one, “blue”}, {:two, " red"}, …]
No, it won’t … It will be more like [{:one, 'blue'}, {:two, 'red'}, ...].
As others have pointed out this is not suitable for large lists but keyword lists are usually smallish.
I use the above construct a lot for a number of list based “data structures”. I don’t know if the choice of lists:keyfind was intentional over the naive recursive implementation done in erlang’s proplists but if I have to speculate (which I feel forced to) I would say it is
The use of :lists.keyfind is an optimisation. The function is significantly faster, since it is implemented as a BIF in C. But yes, if we were to implement it in Elixir, it would look very similar to what @cmkarlsson presented.