# query(Repo, @sql, []).rows
rows = [{1, 1, 500, {{2015, 1, 1}, {14, 13, 20}}}, {2, 2, 300, {{2015, 1, 1}, {14, 13, 10}}}]
# Example 1
rows
|> Enum.map(fn {sequence, id, value, date} ->
%{sequence: sequence, id: id, value: value, date: Date.from(date)} end)
|> do_stuff
# Example 2
rows
|> Enum.map(fn row ->
%{sequence: elem(row, 0), id: elem(row, 1) , value: elem(row, 2) , date: Date.from(elem(row, 3)))}
end)
# Example 3
columns = [:sequence, :id, :value, :date]
rows
|> Enum.map(fn row ->
Enum.zip(columns, Tuple.to_list(row))
|> Enum.into(HashDict.new, fn
{key, value} when key == :date -> {key, Date.from(value)}
{key, value} -> {key, value}
end)
end)def match(<<"01", layout :: 20 - binary, version::2 - binary, date::20-binary, _ :: binary>>) do
%{code: 01, layout: String.rstrip(layout), version: version, date: date}
end
def match(<<"05", date :: 20 - binary, sender::2 - binary, sequence::20-binary, _ :: binary>>) do
%{code: 05, date: date, sender: sender, sequence: sequence}
end
def match(<<code :: 2 - binary, rest :: binary>>) do
%{code: String.to_integer(code), error: :unhandled}
end--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/4a35f4b1-2c57-49e5-9a64-d72d6ca7c6a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
iex> %{ name: a_name } = person
%{height: 1.88, name: "Dave"}
iex> a_name
"Dave"[{1, 1, 500, {{2015, 1, 1}, {14, 13, 20}}}, {2, 2, 300, {{2015, 1, 1}, {14, 13, 10}}}]
|> Enum.map(fn row ->
# Works
# {first_part_of_tuple, second_part_of_tuple, value, date} = row
# Does not work
%{sequence: first_part_of_tuple, id: second_part_of_tuple, value: value, date: date} = row
end)Enum.map(fn {s, i, v, d} ->
%{sequence: s, id: i, value: v, date: Date.from(d)} end)
|> do_stuff
{first_part_of_tuple, second_part_of_tuple, value, date} = row
first tests if row is a tuple of 4 elements, which it is, and then extracts the 4 elements of row into the variables first/second_part_of_tuple, value and data. The second match:
%{sequence: first_part_of_tuple, id: second_part_of_tuple, value: value, date: date} = row
does *not* build a map but first tests if row is a map, which it isn't as it is a tuple, and if it was a map then tests if it has the fields first/second_part_of_tuple, id, value and date and finally extracts the values of those files and binds the variables first/second_part_of_tuple, id, value and date to those values.
Basically a pattern tests the datatype, structure and extracts values. To build a structure you need to do it on the RHS. You could have written the Enum as
Enum.map(fn row ->
{first_part_of_tuple, second_part_of_tuple, value, date} = row
%{sequence: first_part_of_tuple, id: second_part_of_tuple, value: value, date: date}
end)
which matches row and then builds and returns a map. This is basically exactly the as your previous example 1 except you have moved the output of the function head into the body.
Robert
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/b8b84e36-b419-4a91-899b-02e61c320f33%40googlegroups.com.
Repo.transaction(fn ->
Ecto.Adapters.SQL.query(repo, @sql_query, [])
|> Util.DB.result_to_map(fn
{key, value} when key == "date" -> {key, Date.from(value)}
{key, value} -> {key, value}
end)
|> do_stuff
end)