- dynamic/functional lang
- runs on Erlang VM
- Actor model - each actor is a separate process in the VM - allows concurrency
Processes are lightweight, and exchange info via messages. This isolates processes, which allows independent GCs and prevents system-wide pauses.
- Mix is the build tool
- Hex is the package manager
- IEx is the repl
- can invoke any Erlang function with no runtime cost
Basic types and operations
- Booleans:
-
Functions are referred by name and arity, eg.
is_boolean/1
. -
Strings are UTF-8 encoded
- When using binary operations,
use
and
,or
,not
when first argument is boolean. - With
&&
,||
,!
the args can be of any type. (Everything exceptfalse
andnil
eval to true, just like in Ruby)
- Comparison operators:
===
is more strict compared to==
.
- Different data types can be compared. This allows sorting algorithms to not worry about data types.
Pattern matching
=
is the pattern match operator
x = 42
assigns 42 to the variable x.42 = x
is ok because the operator only checks if both sides match1 = x
fails because the right side has the value 42 and left side has the value 1, and so the match fails- Variables can only be assigned if they are on the left side
- When we try matching an unknown variable
on the right side,
Elixir thinks it is a function call to
foo
- You can re-bind variables
^
(pin operator) lets you access previously bound values- it can be used when you need to match against a variable’s value prior to the match
Strings, binaries, char lists
- Char lists are mainly used for interfacing with old Erlang libs
Keyword lists
- Keyword lists are similar to hashes/dictionaries in other languages
- They map to arrays of key-value tuples
- have 3 characteristics
- keys must be atoms
- keys are ordered
- duplicate keys can exist
Maps
- Key value store
Modules and functions
- The
defmodule
macro defines a module def
defines a function- Functions have implicit returns
Fibonacci example:
This version, using case, maps to the pattern matching version at the VM level.
Ruby-like version:
Length of a list:
Map:
Run length encoding:
Default args:
Processes
- Achieves concurrency by making use of actors.
- Actors are processes that can perform a specific task.
- We can send messages to an actor to ask it to do something, and it can respond by sending back a message.
Flow example
From Jose’s Elixirconf 2016 Keynote:
Umbrella apps
Ecto
- An Introduction to Elixir’s Ecto Library
- Meet Ecto, The No-Compromise Database Wrapper For Concurrent Elixir Apps
- Building Many-To-Many Associations with Embedded Schemas in Ecto and Phoenix
Plug
Links
- Elixir lang - website
- Phoenix guides - Guides for the Phoenix web framework
- Elixir Quick Reference
- Jose Valim - How I Start
- Elixir Resources
- Elixir Style Guide
- Learning Elixir and Erlang - more resources
OTP
Videos
- Elixir - A modern approach to programming for the Erlang VM on Vimeo, Jose Valim
- Introduction to Elixir, Dave Thomas
- Think Different, Dave Thomas keynote at Elixir Conf 2014
Books
- Études for Elixir by J. David Eisenberg