-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_return.ex
More file actions
39 lines (31 loc) · 767 Bytes
/
error_return.ex
File metadata and controls
39 lines (31 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
defmodule ErrorReturn do
def sqrt_good(x) when is_float(x) and x >= 0 do
{:ok, :math.sqrt(x)}
end
def sqrt_good(x) when is_float(x) do
:error
end
def sqrt_bad(x) when is_float(x) and x >= 0 do
:math.sqrt(x)
end
def sqrt_bad(x) when is_float(x) do
:error
end
@doc """
Log of sqrt of the last number in a string
## Examples
iex> log_sqrt_last("a b 10000")
2.0
"""
def log_sqrt_last(str) do
with words <- String.split(str, ~r/\s+/),
last_word <- List.last(words),
{last_number, ""} <- Float.parse(last_word),
{:ok, sqrt} <- sqrt_good(last_number),
# sqrt when sqrt != :error <- sqrt_bad(last_number), # :((
log <- :math.log10(sqrt)
do
{:ok, log}
end
end
end