2016-08-21 69 views
2

我想對一系列值的組合進行一些分析。帶解析的範圍問題

我有以下的功能,但由於某些原因,經過推導完成,並在函數體結束,該變量analytics仍然是一個空的列表,而這不是在每次迭代

修真內

有什麼建議嗎?

def handle_cast({:start}, state) do 
    intervals = [7, 30, 90] 
    groupings = ["day_of_week", "time_of_day"] 
    aggregators = [ 
     %{ 
     domain: "support", 
     metric: "new_conversations", 
     func: &App.get_new_conversations/2 
     }, 
     %{ 
     domain: "support", 
     metric: "closed_conversations", 
     func: &App.get_closed_conversations/2 
     }, 
     %{ 
     domain: "support", 
     metric: "median_response_time", 
     func: &App.get_median_response_time/2 
     }, 
    ] 

    Repo.transaction(fn -> 
     Repo.delete_all(Analytic) 

     analytics = [] 
     for interval <- intervals do 
     for grouping <- groupings do 
      for %{domain: domain, metric: metric, func: func} <- aggregators do 
      analytic = 
       func.(grouping, interval) 
       |> Enum.map(fn %{"app_id" => app_id, "data" => data} = result -> 
       %Analytic{app_id: app_id, domain: domain, metric: metric, grouping: grouping, interval_in_days: interval, data: data} 
       end) 

      analytics = [analytic|analytics] 
      end 
     end 
     end 
    end) 

    {:noreply, state} 
    end 

回答

3

Elixir中的變量是不可改變的,但是可重新定義。這意味着行analytics = [analytic|analytics]正在創建一個新列表並將其綁定到名爲analytics的變量作爲該塊的範圍。塊結束時,更改不會在for的下一次迭代中持久化。例如:

iex(1)> x = 1 
1 
iex(2)> for i <- 1..3 do 
...(2)> IO.puts(x); x = x + i; IO.puts(x) 
...(2)> end 
1 
2 
1 
3 
1 
4 

爲你寫的代碼,你可以使用for內返回他們的最後一個表達式的值的列表的事實和最for的返回值存儲analytics,但有一個小問題:最終會出現嵌套列表:

iex(1)> for i <- 1..2 do 
...(1)> for j <- 1..2 do 
...(1)>  for k <- 1..2 do 
...(1)>  {i, j, k} 
...(1)>  end 
...(1)> end 
...(1)> end 
[[[{1, 1, 1}, {1, 1, 2}], [{1, 2, 1}, {1, 2, 2}]], 
[[{2, 1, 1}, {2, 1, 2}], [{2, 2, 1}, {2, 2, 2}]]] 

但是,有一個簡單的解決方案! for接受多個<-條款在單個呼叫和自動返回一個平坦的列表:

iex(1)> for i <- 1..2, j <- 1..2, k <- 1..2 do 
...(1)> {i, j, k} 
...(1)> end 
[{1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {1, 2, 2}, {2, 1, 1}, {2, 1, 2}, {2, 2, 1}, 
{2, 2, 2}] 

使用這種方法,你的代碼就變成了:

analytics = 
    for interval <- intervals, 
     grouping <- groupings, 
     %{domain: domain, metric: metric, func: func} <- aggregators do 
     func.(grouping, interval) 
     |> Enum.map(fn %{"app_id" => app_id, "data" => data} = result -> 
     %Analytic{app_id: app_id, domain: domain, metric: metric, grouping: grouping, interval_in_days: interval, data: data} 
     end) 
    end 
    end 
end 

這應該給你,你是最有可能希望相同的輸出從原來的代碼。

+0

完美,非常感謝! – Tarlen