Elixir notes part III

Hasan Tayyar Beşik
3 min readSep 24, 2021

I realised that I did skip the getting started pages at elixir official page. So I stopped reading the books that I selected initially and started reading getting started guides at https://elixir-lang.org/getting-started/introduction.html

Even the types are quite different in Elixir. So it’s important to read the getting started pages before diving into seriously.

Some reading on Elixir: https://dorgan.netlify.app/ — I bookmarked for the future.

Notes

  • OTP is what makes Elixir so efficient. “OTP (Open Telecom Platform) is a set of libraries that ships with Erlang. Erlang developers use OTP to build robust, fault-tolerant applications. In this chapter we will explore how many aspects from OTP integrate with Elixir, including supervision trees, event managers and more;” — Excerpt From: elixir-lang.org. “Mix and OTP Guide”.
  • All documentation about Elixir internals are available online via https://hexdocs.pm/elixir/Kernel.htm
  • GenServer is the most famous and important library part of OTP. “A behaviour module for implementing the server of a client-server relation. A GenServer is a process like any other Elixir process and it can be used to keep state, execute code asynchronously and so on. The advantage of using a generic server process (GenServer) implemented using this module is that it will have a standard set of interface functions and include functionality for tracing and error reporting. It will also fit into a supervision tree.” — Excerpt From: hexdocs.pmGenServer Documentation”.
  • In Elixir, all code runs inside processes. Processes are isolated from each other, run concurrent to one another and communicate via message passing. And this is quite different comparing many popular programming languages including NodeJs.
  • Elixir allows you to drop the parentheses when invoking named functions with at least one argument. This makes the code cleaner. Instead of div(10,2) you can use div 10,2
  • Functions in Elixir are identified by both their name and their arity. The arity of a function describes the number of arguments that the function takes.
  • Inside iex documentation can be accessed via h function. All functions in the Kernel module are automatically imported into our namespace. So you can just type h trunc/1 (1 is the arity) to get documentation about trunc function. You may need to include the module name like h Kernel.trunc/1 . You can also get documentations about the operators too, like + operator: h Kernel.+/2 or h Kernel.+/1
  • Elixir has the basic types of integer, float, boolean, string, list, tuple and atom. An atom is a constant whose value is its own name. Atoms are like enums. Atoms are defined with : . There are also aliases to alias modules. They are also considered as atoms but they are used without : . is_atom(AnythingStartsWithCapitalCaseIsAnAlias) returns true.
  • There are many different cases for atoms. Like boolean types are already atoms true == :true and is_atom(true)will return true. But is_boolean(true) will also return true. Boolean types are both boolean and atoms. So atoms are constants whose values are their own name. IO.puts will write :ok to the output device after printing /putting the data successfully. Here the :ok is an atom.
  • Lists are stored to memory as linked lists. There is a concept of proper and improper lists. Read more : https://dorgan.netlify.app/posts/2021/03/making-sense-of-elixir-(improper)-lists/
  • Elixir uses UTF-8 by default. Strings in Elixir are UTF-8 encoded binaries. is_binary("test") will return true. byte_size("test") will return 4.
  • Single quoted and double quoted strings are handled different. Documentation states clearly https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html.
  • Single quoted string, is actually a charlist, aka a list of chars. If you want to concatenate them, you need to use list concatenation operator ‘++’. 'test' ++ ' ' ++ '1234'
  • Double quoted string, is a UTF-8 encoded ‘binary’, aka a series of bytes. Their concatenation is done via ‘<>’. "test" <> " " <> "1234" . Or can be mixed like "test" <> " " ++ '1234'
  • Strings docs must read : https://hexdocs.pm/elixir/1.12/String.html

--

--

Hasan Tayyar Beşik

GCloud, AWS, Security, NodeJs, Berlin — This blog is mostly technical and multilingual. Be aware of possible and multiple typos!