2016-08-21 47 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