ELIXIR
LEARNING DAY
INTRO TO ELIXIR
WHY ELIXIR?
▸ Rails core dev José Valim found problems in Rails which
could not be solved by Ruby.
▸ Scalability, concurrency, real-time communication
▸ Looked for other languages that provided it.
▸ Didn’t find one that had both a nice syntax (developer
happiness) and solved the problems features.
▸ He did find Erlang.
ERLANG
ERLANG FEATURES
▸ Created in the 80s by Ericsson, to
support telephony systems.
▸ Scalable, consumes all cores, fault
tolerant, functional programming
▸ When was the last time your cell phone
network went down to apply an
update? Hot code swapping.
▸ Fault tolerant. Let it fail! Restart a
process when it fails. Supervisor
processes watch other processes. Kill
and restart failing processes.
▸ WhatsApp: 900 million users, 50
engineers
ERLANG FEATURES
FUNCTIONAL PROGRAMMING
▸ “Functional” refers to a mathematical function: given
certain inputs, it will always return the same output.
▸ Functions are first-class citizens.
▸ State is immutable.
▸ Lends itself to concurrent programming.
▸ More cores.
%%	Loop	uses	a	list	for	times	in	order	to	go	around	
%%	the	~49	days	limit	on	timeouts.	
loop(S	=	#state{server=Server,	to_go=[T|Next]})	->	
				receive	
								{Server,	Ref,	cancel}	->	
												Server	!	{Ref,	ok}	
				after	T*1000	->	
								if	Next	=:=	[]	->	
												Server	!	{done,	S#state.name};	
											Next	=/=	[]	->	
												loop(S#state{to_go=Next})	
								end	
				end.
ERLANG SYNTAX…
THE BEST OF BOTH WORLDS
}
$	iex	
Erlang/OTP	18	[erts-7.1]	[source]	[64-bit]	
[smp:8:8]	[async-threads:10]	[hipe]	[kernel-
poll:false]	[dtrace]	
Interactive	Elixir	(1.1.1)	-	press	Ctrl+C	to	
exit	(type	h()	ENTER	for	help)	
iex(1)>	1+1	
2	
iex(2)>
REPL
x	=	[1,	2]	
//	Ruby	
x.push(3)	
=>	[1,	2,	3]	
x.push(4)	
=>	[1,	2,	3,	4]	
//	Elixir	
List.insert_at(x,	-1,	3)	
=>	[1,	2,	3]	
List.insert_at(x,	-1,	4)	
=>	[1,	2,	4]
IMMUTABLE STATE
add	=	fn	num1,	num2	->	
		num1	+	num2	
end	
subtract	=	fn	num1,	num2	->	
	num1	-	num2	
end	
calculate	=	fn	num1,	num2,	operation	->	
		operation.(num1,	num2)	
end	
calculate.(17,	4,	add)	
calculate.(17,	4,	subtract)
FUNCTIONS: FIRST-CLASS CITIZENS
defmodule	Convert	do	
		def	kph_to_mph(kph)	do	
				kph/1.60934	
		end	
		def	mph_to_mps(mph)	do	
				mph	/	2.23693629	
		end	
end	
Convert.kph_to_mph(100)	
=>	62.137273664980675
MODULES
mph	=	Convert.kph_to_mph(100)	
=>	62.137273664980675	
Convert.mph_to_mps(mph)	
=>	27.777846844704136	
Convert.kph_to_mph(100)	|>	Convert.mph_to_mps	
=>	27.777846844704136	
PIPE OPERATOR
defmodule	Calculator	do	
		def	sqrt(x)	do	
				:math.sqrt(x)	
		end	
end	
Calculator.sqrt(-1)	
**	(ArithmeticError)	bad	argument	in	
arithmetic	expression	
				(stdlib)	:math.sqrt(-1)	
													iex:47:	Calculator.sqrt/1
FUNCTION GUARDS (1)
defmodule	Calculator	do	
		def	sqrt(x)	when	x	>=	0	do	
				:math.sqrt(x)	
		end	
end	
Calculator.sqrt(-1)	
**	(FunctionClauseError)	no	function	clause	
matching	in	Calculator.sqrt/1	
				iex:47:	Calculator.sqrt(-1)
FUNCTION GUARDS (2)
defmodule	Calculator	do	
		def	add(x,	y)	do	
				x	+	y	
		end	
		def	add(x,	y,	z)	do	
				x	+	y	+	z	
		end	
end	
Calculator.add(1,	2)	
=>	3	
Calculator.add(1,	2,	3)	
=>	6
FUNCTION OVERLOADING
[x,	y]	=	[1,	2]	
=>	[1,	2]	
x	
=>	1	
y	
=>	2
PATTERN MATCHING (1)
defmodule	GPS	do	
		def	location_name(lat,	long)	do	
				#	lots	of	processing	here	
				%{city:	"Santiago",	country:	"Chile"}	
		end	
end	
%{city:	mycity}	=	GPS.location_name(0.1,	0.2)	
=>	%{city:	"Santiago",	country:	"Chile"}	
mycity	
=>	"Santiago"
PATTERN MATCHING (2)
defmodule	Factorial	do	
		def	fact(0),	do:	1	
		def	fact(n)	do	
				n*fact(n-1)	
		end	
end	
Factorial.fact(0)	
=>	1	
Factorial.fact(1)	
=>	1	
Factorial.fact(4)	
=>	24
PATTERN MATCHING (3)
defmodule	Factorial	do	
		@moduledoc	"""	
		Contains	a	bunch	of	functions	to	calculate	factorials	
		"""	
		@doc	"""	
		Calculates	the	factorial	of	the	given	number	
		"""	
		def	fact(0)	do	
				1	
		end	
		def	fact(num)	do	
				num	*	fact(num-1)	
		end	
end	
INLINE DOCUMENTATION (1)
h	Factorial	
=>	
		Factorial	
		Contains	a	bunch	of	functions	to	calculate	
		factorials	
h	Factorial.fact	
=>	
		def	fact(num)	
		Calculates	the	factorial	of	the	given	
		number
INLINE DOCUMENTATION (2)
MOTIVATION
AKA ELIXIR ON RAILS
MOTIVATION
PHOENIX PERFOMANCE
Source: https://github.com/mroth/phoenix-showdown

Intro to Elixir