I try to learn with wiritng the most-elixirish functional code, sometime with the help of you.
I am just writing a parsing module, which extracts some info from recieved JSON (different shapes), and come up with this functions:
def get_markets do
for {market, url} <- get_markets_urls() do # get_markets_urls just query my db for market name and it's url (about 5-10 records max.)
case get(url) do
{:ok, val} -> parse_markets(val.body, market)
{:error, reason} -> IO.puts "Error: #{reason}"
end
end
end
defp parse_markets(response, market) do
IO.puts "Parsing markets"
cond do
market == "Market1" ->
IO.puts Enum.map(response, &(Map.get(&1, "asset")))
market == "Market2" ->
Enum.map(Map.get(response, "data"), &(marketClient(&1, market)))
market == "Market3" ->
IO.puts Enum.map(response, &(&1))
true -> "Other"
end
end
As you can see, for every market I need to extract different info (because of different response shape).
Please, is this a good way to write it? Coud it be written by better, more functional way?
Also while there is nothing wrong with comprehensions it seems that your are holding on too tightly to iteration.
If you are going through the list for side effects I’d expect to see Enum.each/2 instead or if you wanted conditional transformation I’d expect some piped combination of Enum.filter/2 and Enum.map/2 (though comprehensions can have filters as well).
At the very least I’d make the comprehension look less like a loop, e.g.
A “Clean Coder” advice: I would prefer the pattern-match solution by @kokolegorille for one reason: You can add new “Markets” without changing the existing code but by just adding a new function-header for “Market4”.