acrolink

acrolink

How to handle "bad argument in arithmetic expression" error by using guards

I want to learn some basics of this great langauge. I have put this code together:

defmodule Xe do

  def call(input) do
    with {:ok, first} when is_integer(input) <- by_ten(input),
         {:ok, second} <- by_five(first)
    do
      IO.puts second
    else
      _ ->
        IO.puts "error"
    end
  end

  def by_ten(input) do
    {:ok, input * 10} 
  end

  def by_five(input) do
    {:ok, input * 5} 
  end

end

Xe.call("abc")

Here I am getting:

(ArithmeticError) bad argument in arithmetic expression
    a.ex:16: Xe.by_ten/1
    a.ex:5: Xe.call/1
    (elixir) lib/code.ex:677: Code.require_file/2

How to avoid this using guards? Thank you.

Most Liked

jordiee

jordiee

guards in with match against the result not the input. So in this case it checks that the result of by_ten first matches {:ok,first} and then runs that against the guard. Put your guard on the function itself to guard against input params. Also make sure to have a by_ten without the guard to return an error.

acrolink

acrolink

@jordiee

Thank you. Here is the refined code which gives the desired result:

defmodule Xe do

  def call(input) do
    with {:ok, first} <- by_ten(input),
         {:ok, second} <- by_five(first)
    do
      IO.puts second
    else
      _ ->
        IO.puts "error"
    end
  end

  def by_ten(input) when is_integer(input) do
    {:ok, input * 10} 
  end

  def by_ten(_) do
    {:error} 
  end

  def by_five(input) do
    {:ok, input * 5} 
  end

end

Xe.call("abc")

Any remarks are welcome.

jordiee

jordiee

Yep looks good. The only thing I would change and its really preference is when my function is only one line I change it to shorthand syntax…so

def by_ten(_), do: {:error}

but again that is all preference.

Where Next?

Popular in Questions Top

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
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
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 Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement