【发布时间】:2019-07-31 18:10:43
【问题描述】:
我有这个代码sn-p
1..total_days |> Enum.reduce(start_date, fn _i, acc ->
day_of_week = acc |> Calendar.Date.day_of_week_name
rec_head = get_head_tail(schedule[day_of_week])
rec_head |> Enum.each(fn(x) ->
iterate(x, acc, timezone) |> t_download(interval, t_agent)
end)
acc |> Calendar.DateTime.to_erl |> Calendar.DateTime.from_erl(timezone, {123456, 6}) |> ambiguous_handle |> Calendar.DateTime.add!(86400)
end)
它基本上是围绕total_days 循环。
时间表的价值是一整周。比如。
{"Monday":["00:00-23:59"],"Tuesday":["00:00-23:59"],"Wednesday":["00:00-23:59"],"Thursday":["00:00-23:59"],"Friday":["00:00-23:59"],"Saturday":["00:00-23:59"],"Sunday":["00:00-23:59"]}
其他方法如..
defp ambiguous_handle(value) do
case value do
{:ok, datetime} -> datetime
{:ambiguous, datetime} -> datetime.possible_date_times |> hd
end
end
defp get_head_tail([]), do: []
defp get_head_tail(nil), do: []
defp get_head_tail([head|tail]) do
[[head]|get_head_tail(tail)]
end
def t_download([], _interval, _t_agent), do: :noop
def t_download([starting, ending], interval, t_agent) do
t_do_loop(starting, ending, interval, t_agent)
end
defp t_do_loop(starting, ending, _interval, _t_agent) when starting >= ending, do: :noop
defp t_do_loop(starting, ending, interval, t_agent) do
Agent.update(t_agent, fn list -> ["true" | list] end)
t_do_loop(starting + interval, ending, interval, t_agent)
end
def download([], _camera_exid, _interval, _id, _requestor), do: :noop
def download([starting, ending], camera_exid, interval, id, requestor) do
do_loop(starting, ending, interval, camera_exid, id, requestor)
end
我实际上是在使用代理来计算循环通过download 的次数,它工作得很好,也给了我结果。
我的问题是,这是一种正确的做法吗?我只是在更新 Agent 的值。最后,我计算了真实值的总列表。
但我正在寻找更简单或更可靠的解决方案。迭代每次都会绕过所有这些方法。我正在运行它两次。
先做实际操作下载图片,然后统计可能的次数,可以下载多少张图片。
def iterate([], _check_time, _timezone), do: []
def iterate([head], check_time, timezone) do
[from, to] = String.split head, "-"
[from_hour, from_minute] = String.split from, ":"
[to_hour, to_minute] = String.split to, ":"
from_unix_timestamp = unix_timestamp(from_hour, from_minute, check_time, timezone)
to_unix_timestamp = unix_timestamp(to_hour, to_minute, check_time, timezone)
[from_unix_timestamp, to_unix_timestamp]
end
上面的整个过程实际上是在取进度值。随着每一天。并带有开始和结束时间。并循环播放。
【问题讨论】:
标签: elixir