Reputation: 11760
I tried
units = %{}
for s <- squares, u <- unitlist, s in u, do: Map.put(units, s, u)
which doesn't seem to work. I would like to create a map where the keys are in squares the values are in unitlist and the map should only contains those squares which are in the unitlist.
Ultimately, I'd like something like
units = for s <- squares, u <- unitlist, s in u, ????
Upvotes: 1
Views: 623
Reputation: 222118
which works
That code will not work like you probably expect it to. What it does is create a new Map on every iteration, but the units
map declared before will not be modified because variables in Elixir are immutable.
You can use the into
option with for
to create a map. For that, the body of the loop must return a 2-tuple of the key and value.
units = for s <- squares, u <- unitlist, s in u, into: %{}, do: {s, u}
Upvotes: 3