Elixir

2,095 views

Published on

An introduction into Elixir, a Ruby-like language built on Erlang. Demos at https://github.com/rob-brown/Elixir-Demos.

Published in: Technology, News & Politics
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,095
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
28
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Elixir

  1. 1. Elixir Robert Brown @robby_brown @robert_brown @rob-brown
  2. 2. What is Elixir? A Ruby-inspired language built on the Erlang Virtual Machine Extends Erlang with macros, pipelines, and sigils Your next programming language
  3. 3. What is Erlang? Created in 1986 by Ericsson Open sourced in 1998 Functional, concurrent language Based on Prolog, Smalltalk, CSP, and functional programming
  4. 4. Advantages of Erlang Fault tolerant Lightweight processes Hot code swapping “Let it crash” philosophy
  5. 5. Advantages of Erlang Battle-tested libraries Soft real time Trivial parallel processing Trivial network protocol processing
  6. 6. Advantages of Erlang Pattern matching Tail recursion Garbage Collected
  7. 7. Advantages of Erlang http://www.slideshare.net/JanHenryNystrom/productivity-gains-in-erlang
  8. 8. Who Uses Erlang? Amazon Yahoo! Facebook T-Mobile Motorola Ericsson WhatsApp Huffington Post CouchDB GitHub Basho RabbitMQ Call of Duty League of Legends Goldman Sachs http://en.wikipedia.org/wiki/Erlang_(programming_language)
  9. 9. Why Learn Functional Programming? The future is in parallel processing Easier to debug Many languages are adopting FP techniques
  10. 10. Actor Model Actors can be created/destroyed and send/receive messages All state is encapsulated In Elixir, each actor is its own process
  11. 11. Elixir Syntax: Numbers ! 42 123.456 1_000_000 ! 0b101010 (binary) 0xdeadc0de (hex) 034 (octal)
  12. 12. Elixir Syntax: Tuples { 1, 2, 3 } { 3.14, :hello, “world” }
  13. 13. Elixir Syntax: List [ ] [ 1, 2, 3 ] [ head | tail ] [ first, second | tail ]
  14. 14. Elixir Syntax: Atom :atom :“with spaces”
  15. 15. Elixir Syntax: Binary “Elixir” <<“Elixir”>> << 69, 108, 105, 120, 105, 114 >>
  16. 16. Elixir Syntax: Character List ‘Elixir’ [ ?E, ?l, ?i, ?x, ?i, ?r ] [ 69, 108, 105, 120, 105, 114 ]
  17. 17. Elixir Syntax: Range 1..100 10..0 -10..10
  18. 18. Elixir Syntax: Pipeline IO.puts(“Hello world!”) “Hello world!” |> IO.puts() ! IO.puts(String.upcase(“Elixir”)) “Elixir” |> String.upcase() |> IO.puts()
  19. 19. Elixir Syntax: Regex ~r“^[A-Z]$” “101010” =~ ~r“^[01]+$”
  20. 20. Elixir Syntax: Operators + - * / ! = == === != !== > >= < <= and or xor not && || [ 1, 2, 3 ] ++ [ 4, 5, 6 ] [ 1, 2, 3 ] -- [ 2 ] “Hello ” <> “World!”
  21. 21. Elixir Syntax: Fn fn (x) -> x * x end &(&1 * &1) ! fn (x, y) -> x + y * 2 end &(&1 + &2 * 2)
  22. 22. Elixir Syntax: Modules and Functions defmodule Demo do def say_hello() do IO.puts(“Hello”) end def say_goodbye(), do: IO.puts(“Goodbye”) defp private_function(), do: “Top Secret” end
  23. 23. Pattern Matching “=” operator does not mean “assign” It’s the matching operator Think of “=” in terms of math
  24. 24. Pattern Matching x = 42 [ a, b, c ] = [ 1, 2, 3 ] [ d, d, e ] = [ 4, 4, 5 ] { ^x, y } = { 42, 99 }
  25. 25. Pattern Matching { :ok, data } = File.read(“Demo.txt”) { :error, reason } = File.read(“Bogus.txt”) { a, b, _ } = Demo.do_something()
  26. 26. Pattern Matching def sum(list), do: _sum(list, 0) defp _sum([], total), do: total defp _sum([ head | tail ], total) do _sum(tail, head + total) end
  27. 27. Pattern Matching fn (x) when rem(x, 15) == 0 -> “FizzBuzz” (x) when rem(x, 3) == 0 -> “Fizz” (x) when rem(x, 5) == 0 -> “Buzz” (x) -> x end
  28. 28. Pattern Matching << number::[ bitstring, size(16) ], “ ”, word::[ bitstring, size(48) ] >> = “42 Elixir”
  29. 29. PID Process ID Returned from spawn and spawn_link Transfer messages with send and receive
  30. 30. PID: spawn pid = spawn(fn -> do_something() end) pid = spawn(Demo, :do_something, []) pid = spawn(&Demo.do_something/0) pid = spawn_link(fn -> 1 / 0 end)
  31. 31. PID: send send(pid, 42) send(pid, { self, :something }) send(pid, { self, fn (x) -> x * x end })
  32. 32. PID: receive receive do { from, :something } -> send(from, { self, do_something() } { :EXIT, from, reason } ->
 IO.puts(“#{from} died by #{reason}”) after 60 * 1000 -> :timeout end
  33. 33. Questions?
  34. 34. Demo
  35. 35. Want to Learn More? Elixir Lang Elixir Cheat Sheet Programming Elixir Programming Erlang
  36. 36. Want to Learn More? ! Productivity Gains In Erlang Joe Armstrong Evan Miller Russel Dillin

×