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
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.
1
acrolink
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.
1
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.
1
Popular in Questions
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
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
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
Student & New to elixir. Nice language.
I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
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
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
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
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
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







