DEV Community

Anton
Anton

Posted on

Notes on Elixir.

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)

https://stackoverflow.com/questions/32024156/how-do-i-raise-a-number-to-a-power-in-elixir

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

https://blog.usejournal.com/elixir-scenic-snake-game-b8616b1d7ee0

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

https://robots.thoughtbot.com/is-elixir-a-scripting-language

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:

https://www.grok-interactive.com/blog/unix-scripting-with-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.


https://prograils.com/courses/elixir/ch/anonymous-functions-overview

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.

Incorrect
add_one(1)

Correct with anonymous functions:
add_one.(1)


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

[[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.

https://stackoverflow.com/questions/30037914/elixir-lists-interpreted-as-char-lists/30039460#30039460


I just had some silly bug.

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

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.

https://www.codewars.com/kata/price-of-mangoes/train/elixir


https://www.codewars.com/kata/triple-trouble-2/train/elixir

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."
      end
  end
end

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

https://elixirforum.com/t/cannot-invoke-remote-function-erlang-length-1-inside-match/9571/2


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):

https://dockyard.com/blog/2016/08/05/understand-capture-operator-in-elixir

https://toranbillups.com/blog/archive/2018/09/28/ampersand-and-functions-in-elixir/

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

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.

Top comments (0)