Hello, I have a list and want to move it to map or tuple or I have a 3 map records of Repo.all
, then after editing this record I want to save these to a map
[a, b, c] |> Enum.map(fn item -> item + 1 end)
now I need to save them to a map like
[a, b, c] |> Enum.map(fn item -> item + 1 end) |> ADD_TO_MAP_OR_TUPLE
how can I do this?
NobbZ
May 29, 2019, 10:03am
2
What do you want to look the result like?
Perhaps take a look at List.to_tuple/1
.
1 Like
shahryarjb:
now I need to save them to a map like
[a, b, c] |> Enum.map(fn item -> item + 1 end) |> ADD_TO_MAP_OR_TUPLE
To produce a map, you might use Enum.into/3
directly:
[:a, :b, :c] |> Enum.into(%{}, fn item -> {item, to_string(item)} end)
#⇒ %{a: "a", b: "b", c: "c"}
3 Likes
mudasobwa:
[:a, :b, :c] |> Enum.into(%{}, fn item → {item, to_string(item)} end)
This is what I need, but it converts to a list, but I need tuple too, is there a cod for tuple ?
1 Like
shahryarjb:
it converts to a list
I am lost. It does not convert anything to the list. Please clarify what output do you expect to receive.
2 Likes
Im sorry I made a mistake that output is a map like %{a: "a", b: "b", c: "c"}
now I need output that be a tuple like this: {a, b, c}
Please refer to the answer by @NobbZ then. |> List.to_tuple()
after map
would do.
2 Likes
it doesn’t work I tested it,
%{a: "a", b: "b", c: "c"} |> List.to_tuple
** (ArgumentError) argument error
:erlang.list_to_tuple(%{a: "a", b: "b", c: "c"})
if there is no module I think I should delete keys and move the value to new tuple.
or I can convert map to list and list to tuple
You are trying to convert map to tuple with List.to_tuple/1
. It obviously does not work. It works on your initial mapped data:
3 Likes
Thank you very much, I wrote many code today I think I need to go bed