GDI

GDI

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

Showing Posts 1 to 9

dimitarvp

dimitarvp

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

:unkown:unknown

GDI

GDI OP

Oh my God! I feel so dumb! Thank you so much! That solved it. I’d been going at it all day, who knew all I needed was a second pair of eyes. Thanks!

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 OP

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:

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:

https://github.com/exercism/elixir/blob/3231f6ce81662a3e03e6173195390978c71b417d/exercises/concept/log-level/test/log_level_test.exs#L8

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.

GDI

GDI OP

I definitely have to get better at reading Elixir error messages, thanks for pointing this out. I honestly don’t know how my brain could not parse the typo for hours!

GDI

GDI OP

Thank you! I’ll take note and add that to my debugging toolkit :pray:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews