{"id":10991,"date":"2024-07-20T15:46:46","date_gmt":"2024-07-20T15:46:46","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=10991"},"modified":"2024-07-20T15:46:46","modified_gmt":"2024-07-20T15:46:46","slug":"guard-clauses-in-elixir","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/guard-clauses-in-elixir\/","title":{"rendered":"Guard clauses in Elixir"},"content":{"rendered":"<p>Guard clauses in Elixir allow us to extend the standard parameter pattern match to evaluating against a predicate.<\/p>\n<p>For example<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndefmodule Guard do\r\n  def check(value) when value == nil do\r\n    IO.puts &quot;Value is nil&quot;\r\n  end\r\n\r\n  def check(value) when is_integer(value) or is_float(value) do\r\n    IO.puts &quot;#{value} is number&quot;\r\n  end\r\n\r\n  def check(value) when is_atom(value) do\r\n    IO.puts &quot;#{value} is atom&quot;\r\n  end\r\n\r\nend\r\n<\/pre>\n<p>We can see the use of the <strong>when<\/strong> keyword, also for multiple guards we use the <em>or<\/em> keyword. The right side of the <em>when<\/em> clauses is a predicate, hence should return true or false.<\/p>\n<p>In these examples if we enter <em>Guard.check(nil)<\/em> the result is &#8220;Value is nil&#8221; if you check 123 or 123.4 (for example) the second guarded function is called etc. <\/p>\n<p>Now one must remember that Elixir will evaluate functions from the top to the bottom, hence if the <em>is_atom<\/em> check is moved to the top of the functions, a <em>nil<\/em> will match as an atom and hence never reach the &#8220;Value is nil&#8221; returning function.<\/p>\n<p>As we&#8217;ve seen, we supply a predicate to the function which means we can check the value for things like it&#8217;s type.  We can also handle ranges, for example maybe we have one function that handle division but guards against a denominator of 0, then we might create two functions like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndef div(a, b) when b == 0 do\r\n  if b == 0, do: raise(&quot;Divide by Zero&quot;)\r\nend\r\n\r\ndef div(a, b) do\r\n  a \/ b\r\nend\r\n<\/pre>\n<p>I&#8217;m not saying this is better than writing something like the code below, but it&#8217;s another way of doing things<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndef div(a, b) do\r\n  if b == 0, do: raise(&quot;Divide by Zero&quot;)\r\n\r\n  a \/ b\r\nend\r\n<\/pre>\n<p>As we saw, we use <em>or<\/em> not <em>||<\/em>. <\/p>\n<ul>\n<li>The boolean operators allowed are <em>or<\/em>, <em>and<\/em>, <em>not<\/em> and <em>!<\/em>.<\/li>\n<li>Comparison operators include <em>==<\/em>, <em>!=<\/em>, <em>===<\/em>, <em>&gt;<\/em>, <em>&lt;<\/em>, <em>&gt;=<\/em> and <em>&lt;=<\/em>.<\/li>\n<li>Arithmetic operators <em>+<\/em>, <em>&#8211;<\/em>, <em>*<\/em>, <em>\/<\/em> can be used.<\/li>\n<li>Join operators <em>&lt;&gt;<\/em> and <em>++<\/em> can be used.<\/li>\n<li>The <em>in<\/em> operator can be used.<\/li>\n<li>Type check functions such as <em>is_integer<\/em> etc.<\/li>\n<\/ul>\n<p>For a more complete list which also include &#8220;guard-friendly functions&#8221; such as abs etc. can be viewed in the <a href=\"https:\/\/hexdocs.pm\/elixir\/1.6.4\/guards.html\" rel=\"noopener\" target=\"_blank\">Guards documentation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Guard clauses in Elixir allow us to extend the standard parameter pattern match to evaluating against a predicate. For example defmodule Guard do def check(value) when value == nil do IO.puts &quot;Value is nil&quot; end def check(value) when is_integer(value) or is_float(value) do IO.puts &quot;#{value} is number&quot; end def check(value) when is_atom(value) do IO.puts &quot;#{value} is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[730],"tags":[],"class_list":["post-10991","post","type-post","status-publish","format-standard","hentry","category-elixir"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=10991"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10991\/revisions"}],"predecessor-version":[{"id":10995,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10991\/revisions\/10995"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=10991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=10991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=10991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}