otuv

otuv

Case with greater than

Hi,

Simple thing but I cannot seem to find it anywhere. How do I do a case with greater than?

As in:

case Kernel.length my_list do
0 →
{ :ok, :no_data }
> 0 →
{ :ok, my_list }
_ →
{ :error, :some_general_error }

Or should I structure my code some other way?

Marked As Solved

axelson

axelson

Scenic Core Team

You’d want to use a guard in this case. Which would look like this

case Kernel.length(my_list) do
  0 ->
    { :ok, :no_data }
  n when n > 0 ->
    { :ok, my_list }
  _ ->
    { :error, :some_general_error }

Although Kernel.length/1 would never return a negative value so you’d probably be better off with Enum.empty?/1 for the code you’ve listed.

Also Liked

otuv

otuv

I decided to go for the empty? way. Thanks.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement