Suzaku_Sakuya

Suzaku_Sakuya

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?

Showing Posts 1 to 5

cmkarlsson

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
kokolegorille

kokolegorille

Just to add to previous post…

If You don’t do this, You will have a scoping problem, and users disappear after the end of the block :slight_smile:

cmkarlsson

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: ....
Suzaku_Sakuya

Suzaku_Sakuya OP

thanks so much @cmkarlsson, you solution help me. I am a former Ruby on Rails developer but now switch to Elixir. It still new to me but it is great in many way

cmkarlsson

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews