I’m on my learning curve of Elixir with Exercism.
I have a question I’m stuck with:
- given an list of colors like
colors = [:orange, :orange, :black]
- and a color/value Map defined as follows:
@colors_map %{
black: 0,
brown: 1,
red: 2,
orange: 3,
yellow: 4,
green: 5,
blue: 6,
violet: 7,
grey: 8,
white: 9
}
My first try was to iterate on the colors list and call Map.get/3
function:
Enum.each(colors, fn c -> Map.get(@colors_map, c) end)
But I’m stuck with how to concatenate the values extracted from the Map as a number resulting from the concatenation of the values for the first 2 list values only.
For example, for the colors = [:orange, :orange, :black]
, I’d like to get:
33
(what corresponds to twice 3
for the :orange
key, `:black being the last value is dropped).
In short, I need to convert the provided list of colors to its numeric resistance value.
Update:
I alsotried this:
digits = Enum.take(colors, 2)
for x <- digits, do: Map.get(@colors_map, x)
This returns me a list [3, 3]
. How is it possible to call join
on the result list in case of the use of the comprehension?
Thank you.