GDI

GDI

Cond evaluation failing on LogLevel exercise

Hi, I’m learning Elixir by doing Exercism courses. I’m on the LogLevel exercise that has to do with Atoms and Cond, I’ve been pulling my hair out over the failing test for a cond evaluation.

Here’s the issue, in the code snippet below, if the log level is 0 and the legacy? is false, it should return an atom of :trace, otherwise it should return an atom of :unknown. The tests for the two lines that have an and operator in the cond evaluation are failing and i don’t know why. I pasted the code in my local editor and there were no errors, I’m confused. Is there something I’m doing wrong?

defmodule LogLevel do
  def to_label(level, legacy?) do
    # Please implement the to_label/2 function
  cond do 
    (level === 0 and legacy? == false) -> :trace 
    level === 1 -> :debug
    level === 2 -> :info
    level === 3 -> :warning
    level === 4 -> :error
    (level === 5 and legacy? == false) -> :fatal
    true -> :unkown
  end    
  end

  def alert_recipient(level, legacy?) do
    # Please implement the alert_recipient/2 function

  end
end

Marked As Solved

dimitarvp

dimitarvp

Not sure about the rest because I don’t remember the exercise but you have a typo here.

:unkown:unknown

Also Liked

al2o3cr

al2o3cr

Static typing still gives you error messages you have to correctly understand to find the error; for instance, the failing test for this exercise:

will fail on the assert LogLevel.to_label(0, true) == :unknown line with a message:

  1) test LogLevel.to_label/1 level 0 has label trace only in a non-legacy app (LogLevelTest)
     test/log_level_test.exs:6
     Assertion with == failed
     code:  assert LogLevel.to_label(0, true) == :unknown
     left:  :unkown
     right: :unknown
     stacktrace:
       test/log_level_test.exs:8: (test)
dimitarvp

dimitarvp

True, but let’s not forget that somebody has to make this proper test. This is a fact in this exercise but it’s not always a fact in the commercial projects.

dimitarvp

dimitarvp

The woes of dynamic languages. :man_shrugging: Elixir is fantastic but Rust would have yelled at you on the first compilation.

Glad that you are unblocked!

GDI

GDI

Now I see why programmers will live and die for statically typed languages :smile:

dimitarvp

dimitarvp

Yep, that’s the reason. In Rust / OCaml / Haskell you just define one sum type (enum in Rust) and if you have a typo the program doesn’t compile.

Still though, for Elixir you can just start doing Ctrl-F (namely find) occurrences of what you think should be in the code and check for yourself. Or you can use grep with counting matches and you can manually verify that. There are many ways to reduce the blind spots of the dynamic languages. :slight_smile:

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
script
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
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
sergio_101
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lucidguppy
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
joeerl
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

We're in Beta

About us Mission Statement