Suzaku_Sakuya
How to return values with if/else and if condition in function
Hi everyone, I am new to Elixir. I have a function and in this function:
if type == "followers" do
users = ....
else
users = ....
end
if options != nil do
users = ....
end
How can I do this and make sure that the users in the first if/else block will be used in the next if block?
Marked As Solved
cmkarlsson
Instead of assigning users in the if clause you assign "the result"t of the if clause to the variable.
def yourfun() do
users = if type == "followers" do
....
else
....
end
if options != nil do
....
else
users
end
end
3
Also Liked
cmkarlsson
And if you want to avoid if else clauses this can be written using more functions.
def yourfun(users, type, options) do
users
|> filter_users(type)
|> transform_users(options)
end
def filter_users(users, "followers"), do: ....
def filter_users(users, "other"), do: ....
def filter_users(users, _), do: ....
def transform_users(users, nil), do: users
def transform_users(users, opts), do: ....
4
cmkarlsson
No worries. It takes some practice to get used to a more functional way of thinking. something I think we all encountered when we transitioned from traditional procedural/OOP to functional languages.
If your code starts having lots of if/else clauses or other conditionals they can generally be refactored in a more functional way. Remember that in elixir everything is an expression.
1
Popular in Questions
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database.
Dep...
New
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
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
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
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
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
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
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New








