All notes tagged with elixir.

Alias for fast testing in Elixir

In my previous note Shorter feedback loops with Elixir tests I already described how to run tests in Elixir, where it only runs the failed tests. Below is what I put in my mix.exs file to have faster feedback loops when testing:

defp aliases do
    [
      ...
      "test.failed": ["test --failed --max-failures 1"],
    ]
  end

  # Make sure the alias runs in the test MIX_ENV environment.
  def cli do
    [preferred_envs: ["test.failed": :test]]
  end

This will create a new alias test.failed that will run only the failed tests and stop after the first failure. This is useful when you are working on a specific test and you want to run it quickly without running all the tests.

April 20, 2024

elixir exercism

Piping arithmetic in Elixir

For an exercism exercise I wanted to do multiplication with the pipe operator, and simply using * does not work, neither does (*).

Now, it turns out the arithmetic operations are functions part of the Kernel module, and you can also call them as an actual function with Kernel.*(2, 2), which is equal to writing 2 * 2.

This enables you do do arithmetic as part of the pipe operation, see:

def monthly_rate(hourly_rate, discount) do
  hourly_rate
  |> daily_rate
  |> apply_discount(discount)
  |> Kernel.*(22)
  |> ceil()
end

April 20, 2024

testing elixir

Shorter feedback loops with Elixir tests

In Ruby, you have the --fail-fast flag, which stops running the test suite at the first failed test. Convenient to get shorter feedback loops, with a long running test suite.

In Elixir, you can achieve the same with:

mix test --max-failures 1

And then when a test fails, you fix it, and make sure that it works with:

mix test --failed

That makes for shorter feedback loops!

April 14, 2024

elixir asdf

Install Elixir with asdf

The following is what I use to setup Elixir on my machines, where I want to make sure that I can jump to all definitions of Elixir library functions.

First, I install asdf. I go the Homebrew and Fish route, but you can also do it from the source and use your shell of choice.

Before I install Erlang, I make sure to set the right flags, because I don’t want Java or build any GUI stuff:

KERL_BUILD_DOCS="yes"
KERL_CONFIGURE_OPTIONS="--disable-debug --without-javac --without-wx"

After that, I go to the asdf-erlang repository, check that I have all the build requirements and install Erlang with:

asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git

# check available Erlang versions
asdf list all erlang

# install the latest version
asdf install erlang 26.2.4

# enable it globally
asdf global erlang 26.2.4

Now, let’s install Elixir. Go to the asdf-elixir repository, make sure you have unzip installed, and do the following.

asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git

# check all available versions
asdf list all elixir

# install the latest version, but! Install by source, so we can jump to the definition of Elixir functions
asdf install elixir ref:v1.16.2

# enable elixir globally
asdf global elixir ref:v1.16.2

Now, it says it in the comments, but the important difference is that I install Elixir by reference instead of the pre-compiled versions because I also like to be able to jump to definitions of Elixir library functions.