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
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lists...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Hi,
I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
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
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
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
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








