elixir - How to create a map from a comprehension? -
i tried
units = %{} s <- squares, u <- unitlist, s in u, do: map.put(units, s, u)
which doesn't seem work. create map keys in squares values in unitlist , map should contains squares in unitlist.
ultimately, i'd like
units = s <- squares, u <- unitlist, s in u, ????
which works
that code not work expect to. create new map on every iteration, units
map declared before not modified because variables in elixir immutable.
you can use into
option for
create map. that, body of loop must return 2-tuple of key , value.
units = s <- squares, u <- unitlist, s in u, into: %{}, do: {s, u}
Comments
Post a Comment