Junaid Farooq
Junaid Farooq

Reputation: 2608

Enveloping data in Maps in Elixir

I am getting image counts using this method in my Elixir app:

Enum.each(0..23, fn(per_hour) ->
  count = hour(camera_exid, date_unix, per_hour)
end)

I want to map per_hour and count into a Map, which I can later pass that to a view. Something in the format of:

[%{hour: per_hour, count: count}, %{hour: per_hour, count: count}, ...]

Upvotes: 0

Views: 54

Answers (1)

Sheharyar
Sheharyar

Reputation: 75760

Enum.each simply applies a function over each item in a list, it doesn't return anything. You should use Enum.map/2:

list = Enum.map 0..23, fn per_hour ->
  %{
      hour:  per_hour,
      count: hour(camera_exid, date_unix, per_hour)
   }
end

Assuming your methods/variables camera_exid and date_unix are available to you in the scope, your final output would be of the format:

[%{hour: per_hour, count: count}, %{hour: per_hour, count: count}, ...]

Upvotes: 1

Related Questions