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
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 ![]()
Also Liked
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.
AstonJ
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









