Refactoring Elixir

One of my colleagues pointed me at this blog post:

https://evuez.github.io/posts/cursed-elixir.html

This is a refactoring exercise, starting with this:

defmodule FooBar do
  def foo(a) do
    if a < 0 do
      bar(a, -1)
    else
      bar(a, 1)
    end
  end

  defp bar(a, b) do
    IO.inspect(a * b)
  end
end

Transform it into canonical Elixir.

The article linked to moves heavily into pipelines but there are alternatives, here this notes that if is an expression:

defmodule FooBar do
  def foo(a) do
    bar(a, (if a < 0, do: -1, else: 1))
  end

  defp bar(a, b) do
    IO.inspect(a * b)
  end
end

Also bar is symmetrical:

defmodule FooBar do
  def foo(a) do
    if a < 0, do: -1, else: 1 |> bar(a)
  end

  defp bar(a, b) do
    IO.inspect(a * b)
  end
end

Pattern matching and guards is also typical Elixir, more so than an if:

defmodule FooBar do
  def foo(a) do
    a 
    |> calculate() 
    |> bar(a)
  end

  defp calculate(a) when a < 0, do: -1
  defp calculate(a), do: 1

  defp bar(a, b) do
    IO.inspect(a * b)
  end
end

This is far more typical.

Building a Domain Model in Elixir part 5

https://github.com/chriseyre2000/pandemic

Only one role out of the basic game to implement before I start on a UI. The domain model is still working despite the essential complexity of the domain.

The operations expert has an ability that can only be used once per turn. This required recording introducing a concept of setting up a player for the start of their turn.

The only complex item that I am concerned about are the actions that can be played at any time. This may mean that some of the automated actions may need to be slowed down (airlift during infect cities?)

Code Golf in Elixir

Elixir has some great short forms of functions:

This is the identity function:

& &1

You can use this to turn a list into a map of counts:

~w[a b c]a |> Map.new(&{&1, 0})

The if statement is an expression and will return nil for the else clause

a = if foo == "hello", do: "yes", else: nil end

There is also an unless that is the reverse of if

Building a Domain Model in Elixir part 4

The domain model is still developing well. https://github.com/chriseyre2000/pandemic.

The technique of using one line composable functions works well. It also makes the code really readable.

I was surprised how easy it was to enable the “quiet night” card. This can be used at any time and will prevent the next infect city phase from doing anything. It was simple to add a new property to the model, set and unset it. I had expected to have had to build this into the UI (which does not exist yet).

Building a Domain Model in Elixir Part 2

I have now got a bit further with my domain model of the Pandemic game. There are very few complex examples of functional domain models. The problem is that problems that benefit from this approach are normally too complex to make a good example.

Pandemic is a complex cooperative board game. Players race to find cures for 4 diseases. There are lots of ways to lose. It’s a Euro game so it has built in termination options. You lose if:

– You can’t draw from the player deck

– On the 8th outbreak

– If you run out of any one of the 4 disease counters and are unable to place one.

The team wins if all 4 diseases have a cure.

https://github.com/chriseyre2000/pandemic

This now has a model of the cities with the links between them. The this was test driven with some useful checks. I used exhaustive property tests to check the link validity. This includes checks that all links are reversable, that no city links to itself and that there are no orphan cities. These caught a number of cut and paste errors.

The next part is a model of the board state. This includes the initial draw of cards, the infection of new cities and the cascade of an outbreak.

This follows an Elixir pattern of using functions in modules using struts. Every non-query function takes and returns the struct defined in the module. This makes composition much easier.