Notes on learning Elixir - Part 2

Notes on learning Elixir - Part 2

Disclaimer: I have been programming for a while, but I am a complete Elixir n00b. These are my unfiltered notes while learning the Elixir language. Official Getting Started documentation is where I started the learning process. I tried to write as many tiny code snippets as I could with already learned syntax. If you think I have misunderstood a concept, then please let me know and I can add corrected information next to my original misunderstanding.

Notes on learning Elixir - Part 1

Keywords and maps

  • associative data structures => data structures that are able to associate a particular value (or multiple values) to a key. Commonly known names and implementations: dictionary, map, key-value pair, etc.
  • Elixir has two types: associative keyword lists and maps
  • associative keyword lists can be created using an array of two item tuples
  • the key is defined using an atom (thing with colon in front)
iex> list = [{:a, 1}, {:b, 2}]
[a: 1, b: 2]

Shorter form:

iex> list = [a: 1, b: 2]
[a: 1, b: 2]
  • I like the shorthand form!
  • you can access the item using: list[:a]
  • it is useful that they are just lists, as you can merge them together using list ++ [c: 3]
  • in (modern) JavaScript you would do Object.assign({}, { a: 1, b: 2 }, { c: 3 })
  • The C# version would be a bit longer..
Keyword lists are important because they have three special characteristics:
Keys must be atoms.
Keys are ordered, as specified by the developer.
Keys can be given more than once.
A keyword may have duplicated keys so it is not strictly a dictionary. However most of the functions in this module behave exactly as a dictionary so they work similarly to the functions you would find in the Map module.
  • maps have following syntax
map = %{:a => 1, 2 => :b}
  • Maps allow any value as a key.
    Maps’ keys do not follow any ordering.
  • you can do pattern matching on maps. For example, if you want to pick certain values by key:
%{:a => picked_value} = %{:a => 1, 2 => :b}
  • It will place value of atom a to the variable picked_value.
  • updating values I find similar to Immutable.js API, so it doesn't look all that unfamiliar.
put_in users[:john].age, 31

Modules, Files & Project structure

  • defmodule is used to create own modules
  • functions are defined with def and private functions defp
  • my initial thought is that the names are a little bit too close to each other.
  • the documentation explicitly says that defmodule and def are macros. It is interesting that even very fundamental commands in the syntax are made using macros.
  • to create reusable modules, the module needs to be placed on a separate file.
  • .ex is the file extension for Elixir files

math.ex

defmodule Math do
  def sum(a, b) do
    a + b
  end
end
ebin - contains the compiled bytecode
lib - contains elixir code (usually .ex files)
test - contains tests (usually .exs files)
  • the project build tool is called mix
  • there is also a scripted mode which doesn't require compiling and therefore, doesn't produce build artifacts
  • reminds me about scriptcs which allows you to run C# code without running compilation and build
  • extension for script files is .exs
  • guards can be used in the module context to create something like overrides in the object oriented languages

defmodule Math do
def zero?(0) do
true
end

def zero?(x) when is_number(x) do
false
end
end

  • function capturing seems to do same thing as in JavaScript putting a function into a variable
  • after talking in the Elixir Slack channel about function capturing, it seems that function capturing can be thought differently:
It depends on what part you are thinking makes it 'the same'. Generally, I would think of function capturing in Elixir as a shortcut for writing an anonymous function. You could write fn a, b -> sum(a, b) end, you could write &sum(&1, &2), or you could write &sum/2.
  • with \, you can default arguments which can be any expression

In the part, I'll check Phoenix and try to implement RSS feed fetching and mapping it to JSON api. Stay tuned!