A Drop of Elixir
A short introduction to Elixir
Paweł Duda (@paweldude)
http://slides.com/pawelduda/a-drop-of-elixir#
Erlang
- Made by Ericsson to improve telephony applications (1986)
- Runs on BEAM (Bogdan/Björn's Erlang Abstract Machine) VM
- Open-sourced in 1998
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991910/58398235.jpg)
Who even uses it?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991915/3-17-11-ericsson-logo-1300463334.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991916/Motorola-logo-original-blue.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991917/tmobile.jpg)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991918/Whatsapp-logo-vector-1024x727.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991919/Yahoo.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991920/download.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991921/amazon-com-logo.jpg)
http://stackoverflow.com/questions/1636455/where-is-erlang-used-and-why
TL;DR
- Facebook: chat backend, 100m active users
- WhatsApp: up to 2m connected users per server
- Yahoo!: bookmarking service, 5m users and 150m bookmarked URLs
http://stackoverflow.com/questions/1636455/where-is-erlang-used-and-why
Let's talk about Elixir
- First released in 2012
- Superset of the battle-tested Erlang
- Compiled to bytecode for BEAM
- Functional
- Immutable data structures
- Declarative
- Dynamic
- Everything is an expression
- Processes share nothing by default - they exchange messages (actor model)
- It stands still faster than Ruby sprints (and eats way less resources)
- Judging by the syntax it is very Ruby-like but the truth is far from that. Code written in Elixir tends to be more verbose and uses many different concepts.
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2999363/Screenshot_from_2016-09-13_11-28-03.png)
http://techbeacon.com/5-emerging-programming-languages-bright-future
Somewhat off-topic quiz time!
Imperative vs declarative code
Which way to double this array is imperative and which is declarative?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997394/Screenshot_from_2016-09-12_19-42-48.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997395/Screenshot_from_2016-09-12_19-43-04.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997396/Screenshot_from_2016-09-12_19-43-15.png)
a)
b)
Which way to double this array is imperative and which is declarative?
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997396/Screenshot_from_2016-09-12_19-43-15.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997402/Screenshot_from_2016-09-12_19-45-17.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997403/Screenshot_from_2016-09-12_19-45-26.png)
a) imperative
b) declarative
Regardless of how I get to your house, what really matters is the car I drive. Am I going to drive
an imperative stick shift car
or
a declarative automatic car.
- Tyler McGinnis
https://medium.freecodecamp.com/imperative-vs-declarative-programming-283e96bf8aea
Now that we know what imperative and declarative really means, let's get back to Elixir :)
Syntax and things I like about the language
so far
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991970/Screenshot_from_2016-09-10_23-24-35.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991976/Screenshot_from_2016-09-10_23-27-41.png)
Anonymous functions
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2991982/Screenshot_from_2016-09-10_23-32-05.png)
Inspect stuff
Modules + pattern matching
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2992014/Screenshot_from_2016-09-11_00-06-03.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2992036/Screenshot_from_2016-09-11_00-11-42.png)
=
Modules + pattern matching
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2992035/Screenshot_from_2016-09-11_00-16-52.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2992016/Screenshot_from_2016-09-11_00-07-17.png)
Modules + pattern matching
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2993886/Screenshot_from_2016-09-11_22-24-48.png)
Let's go back to our TestPatternMatching.two_plus_two/1 and add some documentation
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2993935/Screenshot_from_2016-09-11_22-43-58.png)
We now have documentation in Markdown
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2993939/Screenshot_from_2016-09-11_22-47-11.png)
Let's add a test file...
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2993958/Screenshot_from_2016-09-11_22-51-00.png)
Time to write some actual tests?
Nope, we already wrote them :)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2993964/Screenshot_from_2016-09-11_22-54-28.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2993967/Screenshot_from_2016-09-11_22-56-56.png)
The Markdown documentation has actual, runnable tests!
A good way to solve problems with an outdated documentation :)
mindblown.gif
The pipe operator |>
The task:
- Generate a list of numbers from 1 to 100
- Multiply each of these numbers by 2
- Get the sum of all odd numbers after multiplying them
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997325/Screenshot_from_2016-09-12_19-19-52.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997327/Screenshot_from_2016-09-12_19-20-55.png)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997346/Screenshot_from_2016-09-12_19-22-44.png)
The capture operator makes this even sexier :)
![](https://s3.amazonaws.com/media-p.slid.es/uploads/463494/images/2997362/Screenshot_from_2016-09-12_19-31-53.png)
Supervisor
Live demo
Where to start
http://elixir-lang.org/getting-started/introduction.html
A Drop of Elixir
By Paweł Duda
A Drop of Elixir
- 1,182