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

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -