bsollish-terakeet

bsollish-terakeet

Checking if an enum is empty - Credo vs Compiler

Credo is smart enough to check for (something like) this:

assert length(the_list) == 0

with this response:

Checking if an enum is empty should be done by using
Enum.empty?(enum)
or
list == []

My question is, why isn’t the compiler smart enough to fix it “under the hood”? Seems odd.
I believe Scala does exactly that.

Most Liked

michalmuskala

michalmuskala

The expressions length(list) == 0 and list == [] have slightly different behaviour when the variable list isn’t actually a proper list:

  • length(list) will fail if the input is not a proper list and this means that the whole clause will fail - even it might seem it would succeed otherwise - e.g. length(value) >= 0 used as a guard would fail for input [1 | 2].
  • list == [] will just return false for anything that is not an empty list.

Because of this difference, such a conversion is not safe to be performed automatically by the compiler - it’s extremely hard to prove it would be safe. A compiler has to be 100% sure an optimisation is safe to perform it. On the other hand, in 99% of cases it actually is safe and the programmer can switch it.

bsollish-terakeet

bsollish-terakeet

Thanks for the response, I hadn’t considered improper lists.

The problem, however, as I see it is that if I do (somehow) have an improper list:

foo = [1 | 2]

and I run the following checks:

is_list(foo)
Enum.empty?(foo)

I come to the incorrect conclusion that I have a list with one or more items.
Performance implications aside, it almost seems better/preferable to do length(foo) == 0, because at least that blows up with an argument error. Neither of the Credo suggestions do.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Improper lists are still lists, this isn’t an incorrect conclusion. If you want to make sure something is a proper list, do a dedicated check for that. If you want to know if a list is non empty, checking equality with [] is the clearest thing to do.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
aalberti333
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
romenigld
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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New

Other popular topics Top

msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43591 214
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement