exercism / v3

The work-in-progress project for developing v3 tracks
https://v3.exercism.io
Other
170 stars 164 forks source link

[Elixir] Extend the `log-level` exercise to justify using `cond` over `if` or `case` #3067

Closed angelikatyborska closed 3 years ago

angelikatyborska commented 3 years ago

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.