我有一份清单,如此
list = [[title, description, ~N[2018-01-01 23:00:07], comment, user],
[title, description, ~N[2018-03-02 12:10:18], comment, user]]
现在我需要将每个NaiveDateTime转换为Erlang日期(或Unix时间戳)。
我相信我可以做点像
new_list = Enum.map(list,&modify_date/1)
def modify_date(list) do
##
end
但我无法弄清楚如何让modify_date/1
只影响第三个元素。有什么想法吗?
答案 0 :(得分:3)
SELECT
(SELECT AVG(TicketPrice) FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2016-01-01' and '2016-12-31'
GROUP BY TicketDate) as Col1,
(SELECT AVG(TicketPrice) FROM ticket
WHERE TicketPrice between 552 and 1302
AND AirlineID=1
AND TicketDate between '2017-01-01' and '2017-12-31'
GROUP BY TicketDate) as Col2
答案 1 :(得分:2)
Elixir中解决此问题的最简单,最惯用的方法是直接进行模式匹配。与目前为止提出的其他解决方案相比,这将大大提高可读性。
关于使用modify_date([title, description, date, comment, user])
,你是正确的,如果你模仿这样的匹配:
A.run()
您可以轻松选择所需的任何数据并进行相应操作。
下面介绍了一个完整的解决方案,您可以将其作为 defmodule A do
def run do
[["title", "description", ~N[2018-01-01 23:00:07], "comment", "user"], ["title", "description", ~N[2018-03-02 12:10:18], "comment", "user"]]
|> process
end
def process(list) do
Enum.map(list, &modify_date/1)
end
def modify_date([title, description, date, comment, user]) do
unix_timestamp = date
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.to_unix()
[title, description, unix_timestamp, comment, user]
end
end
运行:
.*
答案 2 :(得分:0)
您也可以将列表转移到元组。
list = [["fds", "fds", ~N[2018-01-01 23:00:07], "sfd", "fdsa"],
["fds", "fds", ~N[2018-03-02 12:10:18], "fds", "fdsa"]]
Enum.map(list, fn x ->
IO.inspect x|> List.to_tuple |> elem(2)
end)