Again working though Programming Phoenix

I am now on my third pass through this book.

The first time through it was still in beta.

The second was at the first release.

Now I am working on the >= 1.4 Version

Here is the repo that I am working on (now at Chapter 6):

https://github.com/chriseyre2000/rumbl

You need to note that sometime you need to edit the code *before* running the ecto.migrate command that prompts you in the console.

The new structure of a Phoenix app is much cleaner. Keeping the domain and the site in the same project helps. The scaffolding is amazing.

For those that don’t know, Phoenix is Elixir’s answer to Ruby On Rails.

That means that it is both an MVC Framework and an associated set of code generators that allow scaffolding of the site. It defaults to using Postgres as a database, but given that the data access uses the wonderful Ecto framework you get migrations and a data access layer.

Processes in Iex

The Elixir iex shell is a very powerful tool in itself.

I have recently found a trick that makes it much easier to use. If you get stuck in a line that you just can’t complete just type :break + return and it will cancel that line.

This is an investigation of what you get out of the box.

Lets start with the processes that exist in an empty iex session:

iex > Process.list |> Enum.count
56 
Process.registered |> Enum.count
33

That means that there are 33 named processes started with iex and 23 anonymous ones.

Here are the named ones:

Elixir.IEx.Broker - GenServer, communicates with other components.
Elixir.IEx.Config - Agent, handles .iex.config file
Elixir.IEx.Pry - GenServer, Handles Pry debugging
Elixir.IEx.Supervisor - Supervisor for the above three Services.
Elixir.Logger, Service for logging, works with logger.
Elixir.Logger.BackendSupervisor, Supervises the backend for the logger
Elixir.Logger.Supervisor - Supervisor for the logger components
application_controller - GenServer, handles loaded applications
code_server - Erlang code server
elixir_code_server - GenServer that manages loaded code in ets
elixir_config - GenServer that stores config in ets
elixir_sup - Supervisor for the above two GenServers
erl_prim_loader
erl_signal_server
erts_code_purger
file_server_2
global_group
global_name_server
inet_db
init
kernel_refc
kernel_safe_sup
kernel_sup
logger - Erlang logger service, used by Elixir.Logger
logger_handler_watcher
logger_proxy
logger_sup
rex
socket_registry
standard_error
standard_error_sup
user
user_drv

We have the following applications loaded:

iex > :application_controller.loaded_applications
[
  {:logger, 'logger', '1.10.4'},
  {:iex, 'iex', '1.10.4'},
  {:compiler, 'ERTS  CXC 138 10', '7.6.2'},
  {:elixir, 'elixir', '1.10.4'},
  {:stdlib, 'ERTS  CXC 138 10', '3.13'},
  {:kernel, 'ERTS  CXC 138 10', '7.0'}
]

You can find out more using :observer.start
This is the Elixir Application Tree
Here is the IEx Application Tree
This is the Elixir Logger Supervisor tree
Here is the Erlang Kernel supervisor tree

These cover 43 of the Processes

I have 13 to track down.

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