ycv005

ycv005

Optimize the inner Match or Conditional Case

For every conditional assignment, every time I need to write a new condition. for example-

    ready_for_eligible_account =
      cond do
        price_plan["standard_plan"]["monthly"] == 0 ->
          true

        price_plan["standard_plan"]["monthly"] != 0 ->
          false
      end
      
    validation_msg = 
      case ready_for_eligible_account do
          true ->
            "Coupon Validated"
          false ->
            "Invalid Coupon"
      end

How can I do the same in 1 conditional case? Also, tell the general way so that I could handle more than 2 conditional case. Your help will be appreciated.

Marked As Solved

NobbZ

NobbZ

This can be easily rewritten as a simple assignement:

ready = price_plan["standard_plan"]["monthly"] == 0

And this is easier to grasp when written as an if rather than a case:

message = if ready, do: "Coupon Validated", else: "Invalid Coupon"

Also Liked

NobbZ

NobbZ

Your first expression whas of this kind:

result = cond do
  condition -> true
  not condition -> false
end

Which in its nature is very similar

result = if condition, do: true, else: false

Which can always be simplified to

result = condition

The second one is a bit different, but usually, whenever you have a case matching on true/false only, you usually can just replace it by an if, there might be cases where you want to explicitely true/false rather than truthy/falsey checks, then of course you need the case.

And to be honest, about any 2-clause-case that matches on an exact value rather than binding names, while the other arm uses _ can be rewritten into an if by just using Kernel.===/2 or Kernel.==/2, depending on the semantics you need.

Where Next?

Popular in Questions Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
albydarned
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

Other popular topics 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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement