rabet

rabet

Learning / proper use of cond (and other Elixir style)

Hey all dear Elixir fans and pros,

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?

Thanks for the hints!

R.

Most Liked

kokolegorille

kokolegorille

Maybe You can improve with multiple functions… like this

defp parse_markets(response, "Market1"), do: #whatever
defp parse_markets(response, "Market2"), do: #whatever
defp parse_markets(response, "Market3"), do: #whatever
defp parse_markets(response, _), do: #whatever
peerreynders

peerreynders

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.

def get_markets, 
  do: for {name, url} <- get_market_urls()
          do: get_market_data(name, url)

vs

def get_markets,
  do: Enum.map(get_market_urls(), fn {name,url} -> get_market_data(name,url) end)

or

def get_market_data({name,url}) do: 
  ...
end

def get_markets,
  do: Enum.map(get_market_urls(), &get_market_data/1)

(In general I’d dial back on the use of inline anonymous functions that is so common in JavaScript)

NobbZ

NobbZ

As all your branches do compare for equality of market against a string literal a case were much more idiomatic.

Also I’m not quite sure why in the parse_markets/2 you are returning :ok (an atom) most of the time but "Other" (a string) on unknown markets.

Last Post!

iboard

iboard

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”.

Where Next?

Popular in Questions Top

New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement