Notes on Elixir.

antonrich profile image Anton ・3 min read

Error messages in Elixir are mostly delightful:

warning: parentheses are required when piping into a function call. For example:

foo 1 |> bar 2 |> baz 3

is ambiguous and should be written as

foo(1) |> bar(2) |> baz(3)


:math.pow(2,3) |> round


Haven't yet tried that tutorial. When I try it out I will write a feedback post about it.


I really like trying to use the language as much as possible, even in the domains that it was not designed to handle. Obviously Ruby and Python are better choices for writing quick and dirty scripts. But why not try it out with Elixir?

Here's another tutorial on scripting in Elixir:


iex(1)> Is there a way to change a setting for IEX to be just iex>

It will go nice with copying right from iex to doctests. Otherwise you just need to delete all these (1)s.


I tried this function in iex

add_one = fn x -> x + 1 end

The mistake that I made was I called the function without the dot.


Correct with anonymous functions:

warning: Enum.chunk/2 is deprecated. Use Enum.chunk_every/2 instead

[[1, 2, 3], [4, 5, 6], 

In my case I had to use Enum.chunk_every(list, 3, 3, :discard)

The strange characters '\a\b\t'] are nothing to worry about. It's just some iex stuff.


I just had some silly bug.

    Enum.each pixel_map, fn {start, stop} -> do
      :egd.filledRectangle(image, start, stop, fill)

Can you spot a bug here?

Here's what I get when I run iex -S mix:

== Compilation error in file lib/identicon.ex ==
** (TokenMissingError) lib/identicon.ex:80: missing terminator: end (for "do" starting at line 1)

    HINT: it looks like the "do" on line 17 does not have a matching "end"

    (elixir) lib/kernel/parallel_compiler.ex:206: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

I read this and look at line 17. For like 5-10 minutes I couldn't figure out what the hell is wrong, because I'm sure I have all the ends I need, and it's true. But I have a superfluous do after the anonymous function.

After writing out functions in the "def do end" style the "do" became automatic.

Installing egd in your project

{:egd, github: "erlang/egd"}

run mix deps.get

Understanding the problem.



Here I have an idea. To basically do the opposite exercise.

Reverse the problem. Take a word and generate the exercises out of a word.

defmodule Garden do
  def rainAmmount(mm) do
      case mm do
        mm < 40 -> "You need to give your plant #{40 - mm}ml of water."
        _ -> "Your plant has had more than enough water for today."

Will give you:

** (CompileError) before_the_garden_dies.exs:4: cannot invoke remote function :erlang.</2 inside match
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) lists.erl:1354: :lists.mapfoldl/3

to fix this write
mm when mm < 40


There is no partial application in Elixir or currying.

But that looks like partial application.

iex> upcase = &String.upcase/1

iex> total_cost = &(&1 + &2)
iex> total_cost.(1, 2)

iex> check = &(true)
Will give an error. Capture operator cannot have arity of zero.

Understanding the ampersand (the capture operator):



iex> check = fn -> true end
iex> check.()

The parentheses are optional. But I should be careful with that.
iex> add = & &1 + &2
iex> add.(3, 3)

Elixir has a pin operator.
It's just something I need to dig into more a little bit later.

That's it for now, I'm sure I will have more notes to save/share in the future.


Editor guide