【问题标题】:Getting the count of an iteration completion elixir获取迭代完成长生不老药的计数
【发布时间】: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


    【解决方案1】:

    Enum.sum/1 的帮助下,在主循环中创建累加器以收集日期计数:

    Enum.reduce(1..total_days, {start_date, 0}, fn _i, {dates, counts} ->
      day_of_week = Calendar.Date.day_of_week_name(dates)
      more_counts =
        schedule[day_of_week]
        |> get_head_tail()
        |> Enum.map(fn(x) ->
          x
          |> iterate(dates, timezone)
          |> t_download(interval, t_agent)
        end)
        |> Enum.sum()
      dates =
        dates
        |> Calendar.DateTime.to_erl()
        |> Calendar.DateTime.from_erl(timezone, {123456, 6})
        |> ambiguous_handle()
        |> Calendar.DateTime.add!(86400)
    
      {dates, more_counts + counts}
    end)
    

    应更新以下方法以传递数字。

    def t_download(start_end, interval, agent, acc \\ 0)
    def t_download([], _interval, _t_agent, acc), do: acc
    def t_download([starting, ending], interval, t_agent, acc) do
      t_do_loop(starting, ending, interval, t_agent, acc)
    end
    
    defp t_do_loop(starting, ending, _interval, _t_agent, acc) when starting >= ending, do: acc
    defp t_do_loop(starting, ending, interval, t_agent, acc) do
      t_do_loop(starting + interval, ending, interval, t_agent, acc + 1)
    end
    

    上面将返回一个元组,其中日期作为第一个元素(您的实现之前返回的内容)并且算作第二个元素。

    【讨论】:

    • 你的意思是计算每一天吗?那么我们可以用元组加上所有的计数吗?
    • 上面的代码已经总结了一切。检查返回的 acc。
    • 也符合|> iterate(acc, timezone)的acc来自哪里?
    • 这是剩余的,抱歉,已修复。
    • 好的,我将开始日期设置为2019-07-01T00:00:00+05:00,结束日期设置为2019-07-05T00:00:00+05:00,因此总天数为5。该方法只返回了这个{#DateTime<2019-07-06 00:00:00.123456+01:00 IST Europe/Dublin>, 0}
    猜你喜欢
    • 2016-02-01
    • 1970-01-01
    • 2018-01-13
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多