origins of elixir programming language
TRANSCRIPT
# my_script.exs
defmodule MyModule do def my_func do IO.puts("Hello, world!") endend
MyModule.my_func()
gmile@macbook ~> elixir my_scriptHello, world!
gmile@macbook ~> iexiex(1)> c “my_script.exs”[MyModule]iex(1)> MyModule.my_func"Hello, world!”:ok
defmodule MyModule do def my_func(1), do: "Got one!" def my_func(2), do: "Got two!" def my_func(n), do: "Got #{n}!"end
IO.puts(MyModule.my_func(10))IO.puts(MyModule.my_func(5))IO.puts(MyModule.my_func(2))IO.puts(MyModule.my_func(1))
defmodule MyModule do def my_func(1), do: "Got one!" def my_func(2), do: "Got two!" def my_func(n), do: "Got #{n}!"end
MyModule.my_func(10) |> IO.putsMyModule.my_func(5) |> IO.putsMyModule.my_func(2) |> IO.putsMyModule.my_func(1) |> IO.puts
defmodule MyModule do def my_func(a_thing) when is_list(a_thing) do "Got a list!” end
def my_func(a_thing) when is_integer(a_thing) do "Got a number!” end
def my_func(_) "Got something else!” endend
MyModule.my_func([1,2,3]) # => “Got a list!”MyModule.my_func(369) # => “Got a number!”MyModule.my_func(“yo”) # => “Got something else!”
defmodule MyModule do def match_a_list([head|tail]) do IO.puts(“First element is #{head}!”) endend
defmodule MyModule do def match_a_list([one, two|tail]) do IO.puts(“First element is #{one}!”) IO.puts(“Second element is #{two}!”) match_a_list(tail) endend
defmodule MyModule do def match_a_list([]) do IO.puts(“A list is empty!”) end
def match_a_list([one, two|tail]) do IO.puts(“First element is #{one}!”) IO.puts(“Second element is #{two}!”) match_a_list(tail) endend
defmodule MyModule do def match_a_list([one, two|tail]) do IO.puts(“First element is #{one}!”) IO.puts(“Second element is #{two}!”) endend
defmodule MyModule do def match_a_list([{_,b,_}|tail]) do IO.puts(“First element is a tuple”) IO.puts(“Tuple’s second element is #{b}”) endend
defmodule MyModule do def match_a_list([t = {_,b,_}|tail]) do IO.puts(“First element is a tuple of size 3”) IO.puts(“Tuple’ss second element is #{b}”) endend
defmodule Anagram do def match(base, candidates) do base_ = String.downcase(base) fingerprint = fingerprint(base_)
Enum.filter(candidates, fn(candidate) -> cand_ = String.downcase(candidate) cand_ != base_ && fingerprint(cand_) == fingerprint end) end
def fingerprint(string) do string |> String.codepoints |> Enum.sort endend
Kyiv, July 2016
Introduction toElixir & Erlang
VM
Kiev, 2016
Erlang.Brief history and facts
Erlang VM.An overview of languages on BEAM
Elixir.Brief history and ecosystem overview
Who created Erlang?
Who uses Erlang in production?
How old is erlang?
version 1.03 “lost in
the mists of time”
version 1.06 dated 1986-12-18
Who supports erlang today?
How many packages are there?
470!
What are some of the most popular
libs/projects?
How many popular projects
are there, per GitHub?
What exactly erlang is?
erlang a mathematician
a measurement unit
a programming language
erlang the language itself (syntax)
the OTP framework
the virtual machine
-module(hello).-export([hello_world/0]).
hello_world() -> io:fwrite("hello, world\n").
Eshell V8.0.1 (abort with ^G)1> c(hello).{ok,hello}2> hello:hello_world().hello, worldok
-module(count_to_ten).-export([count_to_ten/0]). count_to_ten() -> do_count(0). do_count(10) -> 10;do_count(Value) -> do_count(Value + 1).
Open Telecom Platform
OTP ~~~ It’s a framework ~~~
a set of design principles
a set of tools
BEAM
What is BEAM?
BEAM a name for virtual machine
binary file format
file extension, e.g. .beam
Before BEAM Warren Abstract Machine, WAM
Joe’s Abstract Machine, JAM
Bogdan's Erlang Abstract Machine
Bogdan/Björn's Erlang Abstract
Machine
Current version is R19
Line numbersin exceptions,
woohoo!!!
What kind of virtual machine
BEAM is?
What is M in VM?
{module, add}. %% version = 0{exports, [{add,2},{module_info,0},{module_info,1}]}.{attributes, []}.{labels, 9}.
{function, add, 2, 2}. {label,1}. {line,[{location,"add.erl",6}]}. {func_info,{atom,add},{atom,add},2}. {label,2}. {allocate,1,2}. {move,{x,1},{y,0}}. {line,[{location,"add.erl",7}]}. {call,1,{f,4}}. {move,{x,0},{x,1}}. {move,{y,0},{x,0}}. {move,{x,1},{y,0}}. {line,[{location,"add.erl",7}]}. {call,1,{f,4}}. {line,[{location,"add.erl",7}]}. {gc_bif,'+',{f,0},1,[{y,0},{x,0}],{x,0}}. {deallocate,1}. return.
{function, id, 1, 4}. {label,3}. {line,[{location,"add.erl",9}]}. {func_info,{atom,add},{atom,id},1}. {label,4}. return.
{function, module_info, 0, 6}. {label,5}. {line,[]}. {func_info,{atom,add},{atom,module_info},0}. {label,6}. {move,{atom,add},{x,0}}. {line,[]}. {call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
{function, module_info, 1, 8}. {label,7}. {line,[]}. {func_info,{atom,add},{atom,module_info},1}. {label,8}. {move,{x,0},{x,1}}. {move,{atom,add},{x,0}}. {line,[]}. {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
-module(add).
-export([add/2]).
add(A, B) -> id(A) + id(B).
id(I) -> I.
158 instructions
Write your own language that compiles to
BEAM!
So: what languages run on BEAM?
But before Elixir…
Jose Valim
Who created Elixir?
How old is Elixir?
Who supports Elixir today?
What is Elixir?
Functional Concurrent
General-purpose programming
language
Supports:metaprogramming
via macrospolymorphism via
protocols
How many packages are there?
How many popular projects
are there, per GitHub?
What are some of the most popular
libs?
Who uses Elixir in production?
What Joe Armstrong thinks of Elixir?
> Elixir has a non-scary syntax and combines the
good features of Ruby and Erlang.
> It's not Erlang and it's not Ruby and it has ideas of its
own.
What Elixir offers?
defmodule MathTest do use ExUnit.Case, async: true
test "can add two numbers" do assert 1 + 1 == 2 endend
import Supervisor.Spec
children = [ supervisor(TCP.Pool, []), worker(TCP.Acceptor, [4040])]
Supervisor.start_link(children, strategy: :one_for_one)
parent = self()
# Spawns an Elixir process (not an OS process!)spawn_link(fn -> send parent, {:msg, "hello world"}end)
# Block until the message is receivedreceive do {:msg, contents} -> IO.puts contentsend