The problem with the cond expression is that it is only necessary if you have at least 2 different input variables and 3 different output values. If there is a single input, it's easier to use case. If there are only 2 output values, it's easier to use if. Then I solved the exercise in its original form spontaneously, I did it like this, without cond.
defmodule LogLevel do
def to_label(level) do
case level do
0 -> :trace
1 -> :debug
4 -> :info
5 -> :warning
6 -> :error
7 -> :fatal
_ -> :unknown
end
end
def send_alert?(level) do
label = to_label(level)
label in [:fatal, :error, :unknown]
end
end
To steer people into using cond, I'm proposing a change to the exercise that introduces the concept of a "legacy app" that doesn't support all log codes, and changing send_alert? to alert_recipient to have more than 2 different return values.
PS: This PR includes unrelated reformatting because I saw on Erik's big track v2 <-> v3 merge branch (https://github.com/ErikSchierboom/elixir/pull/1) that some of the concept exercises fail the mix format check script.
The problem with the
cond
expression is that it is only necessary if you have at least 2 different input variables and 3 different output values. If there is a single input, it's easier to usecase
. If there are only 2 output values, it's easier to useif
. Then I solved the exercise in its original form spontaneously, I did it like this, withoutcond
.To steer people into using
cond
, I'm proposing a change to the exercise that introduces the concept of a "legacy app" that doesn't support all log codes, and changingsend_alert?
toalert_recipient
to have more than 2 different return values.PS: This PR includes unrelated reformatting because I saw on Erik's big track v2 <-> v3 merge branch (https://github.com/ErikSchierboom/elixir/pull/1) that some of the concept exercises fail the mix format check script.