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.

Nodes 2020 Extended

I was the first speaker at Nodes 2020 Extended.

My Talk at Nodes 2020 Extended

This is the first time I have done an event that was live streamed. It was a bit odd having two different streams being shared at the same time (video + audio on one) and the screen on the other.

Thanks to @ElLazal and @nsmith_piano for hosting.

I almost got caught out by the demo gods: I had installed Neo4j 4 at the conference a couple of weeks ago and found that my utility will need some updates before it will work with that version. Thankfully I was able to quickly debug this using a docker image. It’s possible to create a Neo4j 3.5 db in Neoj4j which saved the day.

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