I\'m rebuilding something in Elixir from some code I built in C#.
It was pretty hacked together, but works perfectly (although not on Linux, hence rebuild).
This might help too:
count_animals_in_area = fn (area, acc) ->
acc = case Map.has_key?(area, "duck") do
true ->
Map.put(acc, "ducks", (acc["ducks"] + area["duck"]))
false ->
acc
end
acc = case Map.has_key?(area, "goose") do
true ->
Map.put(acc, "geese", (acc["geese"] + area["goose"]))
false ->
acc
end
acc = case Map.has_key?(area, "cat") do
true ->
Map.put(acc, "cats", (acc["cats"] + area["cat"]))
false ->
acc
end
acc
end
count_animals_in_areas = fn(areas) ->
acc = %{ "ducks" => 0,
"geese" => 0,
"cats" => 0 }
IO.inspect Enum.reduce areas, acc, count_animals_in_area
end
t1 = [ %{"duck" => 3, "goose" => 4, "cat" => 1},
%{"duck" => 7, "goose" => 2},
%{"goose" => 12}]
IO.puts "JEA: begin"
count_animals_in_areas.(t1)
IO.puts "JEA: end"
Output:
iex(31)> c "count_animals.exs"
JEA: begin
%{"cats" => 1, "ducks" => 10, "geese" => 18}
JEA: end
[]
I am just learning Elixir so the above is undoubtedly suboptimal, but, hopefully slightly informative.