lanycrost

lanycrost

How To Implement if...else if...else condition

Hi everyone!

I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work properly for me.
I want implement this logic.

if *** do

else if *** do

else

end

I try use cond condition, but i don’t know how implement else logic to this control flow structures.

Thanks!

Marked As Solved

NobbZ

NobbZ

def userselect(%{id: id} = user), do: …
def userselect(%{email: email} = user), do: …
def userselect(%{username: username} = user), do: …
def userselect(_), do: …

Since you now have a match on id, email, or username you may be able to even rewrite the “inner” block a bit.

Since all the case are identical, I’d even move them into a helper function, making your blocks look like this:

defp sel_user(nil), do: {:error, "Something went wrong"}
defp sel_user(user), do: {:ok, user}

def userselect(%{id: id}), do: User |> Repo.get(id) |> sel_user()
def userselect(%{email: email}), do: User |> Repo.get(email) |> sel_user()
def userselect(%{username: username}), do: User |> Repo.get(username) |> sel_user()
def userselect(_), do: sel_user(nil)

Next step were to create those function heads from a list using metaprogramming, they are the same except for the key to match on… But to be honest, I’d do that only for more than 5 keys, you don’t gain much for less, maybe even you loose a lot if meta-ing to early :wink:

14
Post #5

Also Liked

peerreynders

peerreynders

iex> cond do
...>   2 + 2 == 5 ->                                # 1 if expression (equivalent)
...>     "This is never true"                       # 1 
...>   2 * 2 == 3 ->                                # 2 if-else expression (equivalent)
...>     "Nor this"                                 # 2
...>   true ->                                      # 3 else expression (equivalent)
...>     "This is always true (equivalent to else)" # 3
...> end
"This is always true (equivalent to else)"

It’s important to remember that cond, case and even if are expressions , not statements (i.e. not control flow structures) - think: ternary operator in JavaScript, (C, C++, C#, Java) - i.e. they are fundamentally designed to return a value - though the compiler doesn’t yell at you if you ignore the returned value. Functions with multiple clauses work similarly as functions always return values. Many situations can be implemented with either case expressions or multi-clause functions and both support guards which is essentially is a condition that refines the pattern match.

13
Post #3
AstonJ

AstonJ

I had a feeling multiple def clauses might be more appropriate :003:

@NobbZ’s code is much more readable/refactorable/maintainable and the preferred way of doing things (according to @pragdave) because each function is responsible only for one thing :023:

NobbZ

NobbZ

Any reason why you don’t simply use nested if, if you need a nested if?

And an “else” branch in a cond is simply true.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement