Sebb

Sebb

Exploit "Errors in guards" in defguard

I have this helper function where I exploit the Errors in Guards feature.

def non_empty(val, _msg) when length(val) > 0, do: {:ok, val}
def non_empty(val, _msg) when map_size(val) > 0, do: {:ok, val}
def non_empty(val, _msg) when tuple_size(val) > 0, do: {:ok, val}
def non_empty(_val, msg), do: {:error, msg}

(I can call non_empty({:foo}, "") though length({:foo}) raises)

Is there a way to put this into a defguard - without using is_list/1 etc?

defguard enum_size... ???

Marked As Solved

eksperimental

eksperimental

This is how I would rewrite your clauses into just two.

  def non_empty(val, _msg)
      when is_list(val) and val != []
      when is_map(val) and map_size(val) > 0
      when is_tuple(val) and tuple_size(val) > 0 do
    {:ok, val}
  end

  def non_empty(_val, msg), do: {:error, msg}

by checking for the type, it does not raise (anyway that is not a problem if you only have one when clause), but in my example it is needed, othewise later when clauses will never be evaluated if any prior clause raises.
Personally I consider good practice to check for types.

Also Liked

eksperimental

eksperimental

@kartheek definitely yours is more performant.

Here’s another version I think even simpler, that builds on your idea.
defguard is_non_empty(term) when term not in [{}, [], %{}]

kartheek

kartheek

size is constant time O(1), where as length is linear time -O(N) - Naming Conventions — Elixir v1.13.2

Isn’t non_empty for lists a bit more expensive operation - depending on size of list?

Enum.empty?/1 just compares to [] when it is list.

def empty?(enumerable) when is_list(enumerable) do
    enumerable == []
end

Maybe your non_empty can be changed to this ?

def non_empty(val, _msg) when map_size(val) > 0, do: {:ok, val}
def non_empty(val, _msg) when tuple_size(val) > 0, do: {:ok, val}
def non_empty([], msg), do: {:error, msg}
def non_empty(val, _msg) when is_list(val), do: {:ok, val}
def non_empty(_val, msg), do: {:error, msg}

or something like this only if you pass enumerable or tuple, else this will return {:ok, val} for strings, numbers, etc

  def non_empty([], msg), do: {:error, msg}
  def non_empty({}, msg), do: {:error, msg}
  def non_empty(%{} = val, msg) when map_size(val) == 0, do: {:error, msg}
  def non_empty(val, _msg), do: {:ok, val}
eksperimental

eksperimental

please don’t! It totally changes the logic of the function, unless you are fine with it.
As mentioned by @karthheek, it will return {:ok, } in case of an uncovered type.

non_empty(0, "it is empty") will return {:ok, 0}

Where Next?

Popular in Questions Top

Harrisonl
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
JDanielMartinez
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
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
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
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
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

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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