Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Intro to Elixir
Drew Olson
Braintree
brew install elixir
Functional
Concurrent
Immutable
Erlang?
Kind of.
It’s on the Erlang VM.
Metaprogramming
Polymorphism
Tooling
IO.puts "Hello, world!"
Types
1         #
1.0       #
:atom     #
{1, 2, 3} #
[1, 2, 3] #

integer"
float"
atom / symbol"
tuple"
list
[name: "Drew", age: 31]             # keyword"
[{:name, "Drew"}, {:age, 31}]       # keyword
HashDict.new(name: "Drew", age: 31) # HashDict
Modules & Functions
defmodule Greeter do"
  def greet(thing) do"
    IO.puts "Hello, #{thing}!""
  end"
end"
!

Greeter.greet("World")
Anonymous Functions
subject = "World""
!

greeter = fn(greeting) ->"
  IO.puts("#{greeting}, #{subject}!")"
end"
!

greeter.("Hello")
Records
defrecord Person,"
  name: nil,"
  age: nil"
!

drew = Person.new(name: "Drew", age: 31)"
!

IO.puts(drew.name)"
IO.puts(d...
Protocols
defprotocol Checker do"
  @fallback_to_any true"
  def person?(data)"
end"
!

defimpl Checker, for: Person do"
  def perso...
Enum
list = [1, 2, 3]"
keyword = [a: 1, b: 2, c: 3]"
!

Enum.map list, fn(item) ->"
  item * 2"
end"
!

Enum.map keyword, fn({k...
Pattern Matching
list = [1, 2, 3]"
!

case list do"
  [head|_] ->"
    IO.puts(head)"
  [] ->"
    IO.puts("empty!")"
end
defmodule MyList do"
  def sum(list) do"
    sum(list, 0)"
  end"
!

  defp sum([head|rest], acc) do"
    sum(rest, acc + ...
but really
Enum.reduce [1, 2, 3], fn(item, acc) ->"
  acc + item"
end
Macros
not today :)
Concurrency
not today ;(
Tooling
mix
Bundler + Rake
defmodule Foo.Mixfile do"
  use Mix.Project"
!
  def project do"
    [ app: :foo,"
      version: "0.0.1","
      elixir: ...
ExUnit
defmodule MyListTest do"
  use ExUnit.Case"
!

  test "it sums" do"
    assert MyList.sum([1, 2, 3]) == 6"
  end"
end
Demos
Thank you.
Questions?
Nächste SlideShare
Wird geladen in …5
×

Chicago Elixir - Elixir Intro

898 Aufrufe

Veröffentlicht am

Veröffentlicht in: Technologie, Design

Chicago Elixir - Elixir Intro

  1. 1. Intro to Elixir
  2. 2. Drew Olson Braintree
  3. 3. brew install elixir
  4. 4. Functional Concurrent Immutable
  5. 5. Erlang?
  6. 6. Kind of. It’s on the Erlang VM.
  7. 7. Metaprogramming Polymorphism Tooling
  8. 8. IO.puts "Hello, world!"
  9. 9. Types
  10. 10. 1         # 1.0       # :atom     # {1, 2, 3} # [1, 2, 3] # integer" float" atom / symbol" tuple" list
  11. 11. [name: "Drew", age: 31]             # keyword" [{:name, "Drew"}, {:age, 31}]       # keyword
  12. 12. HashDict.new(name: "Drew", age: 31) # HashDict
  13. 13. Modules & Functions
  14. 14. defmodule Greeter do"   def greet(thing) do"     IO.puts "Hello, #{thing}!""   end" end" ! Greeter.greet("World")
  15. 15. Anonymous Functions
  16. 16. subject = "World"" ! greeter = fn(greeting) ->"   IO.puts("#{greeting}, #{subject}!")" end" ! greeter.("Hello")
  17. 17. Records
  18. 18. defrecord Person,"   name: nil,"   age: nil" ! drew = Person.new(name: "Drew", age: 31)" ! IO.puts(drew.name)" IO.puts(drew.age)
  19. 19. Protocols
  20. 20. defprotocol Checker do"   @fallback_to_any true"   def person?(data)" end" ! defimpl Checker, for: Person do"   def person?(_) do"     true"   end" end" ! defimpl Checker, for: Any do"   def person?(_) do"     false"   end" end" ! Checker.person?(Person.new)" Checker.person?(HashDict.new)
  21. 21. Enum
  22. 22. list = [1, 2, 3]" keyword = [a: 1, b: 2, c: 3]" ! Enum.map list, fn(item) ->"   item * 2" end" ! Enum.map keyword, fn({key, val}) ->"   "#{key} -> #{val}"" end
  23. 23. Pattern Matching
  24. 24. list = [1, 2, 3]" ! case list do"   [head|_] ->"     IO.puts(head)"   [] ->"     IO.puts("empty!")" end
  25. 25. defmodule MyList do"   def sum(list) do"     sum(list, 0)"   end" !   defp sum([head|rest], acc) do"     sum(rest, acc + head)"   end" !   defp sum([], acc) do"     acc"   end" end" ! MyList.sum([1, 2, 3])
  26. 26. but really
  27. 27. Enum.reduce [1, 2, 3], fn(item, acc) ->"   acc + item" end
  28. 28. Macros
  29. 29. not today :)
  30. 30. Concurrency
  31. 31. not today ;(
  32. 32. Tooling
  33. 33. mix
  34. 34. Bundler + Rake
  35. 35. defmodule Foo.Mixfile do"   use Mix.Project" !   def project do"     [ app: :foo,"       version: "0.0.1","       elixir: "~> 0.12.2","       deps: deps ]"   end" !   # Configuration for the OTP application"   def application do"     [mod: { Foo, [] }]"   end" !   # Returns the list of dependencies in the format:"   # { :foobar, git: "https://github.com/elixir-lang/foobar.git", tag: "0.1" }"   #"   # To specify particular versions, regardless of the tag, do:"   # { :barbat, "~> 0.1", github: "elixir-lang/barbat" }"   defp deps do"     []"   end" end
  36. 36. ExUnit
  37. 37. defmodule MyListTest do"   use ExUnit.Case" !   test "it sums" do"     assert MyList.sum([1, 2, 3]) == 6"   end" end
  38. 38. Demos
  39. 39. Thank you. Questions?

×