Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir - a brief intro for pythonistas

Elixir - a brief intro for pythonistas

Lightning talk given at Python DBBUG.
Credit on last slide.

Eleni Lixourioti

March 25, 2019

Transcript

26 slides
  1. Elixir • General Purpose • Dynamic • Functional • "Expressive"

    • OTP (Erlang libraries and VM) • interoperability • ! Concurrency !
  2. ! Concurrency • EVM processes (not OS processes) • Like

    super tiny threads... • ...but no shared state! • Easy to spawn • Actor Model • Supervisors (it's processes all the way down...)
  3. Python def hello(name): print(f"Hello, {name}") Elixir defmodule MyApp.Greetings do def

    hello(name) do IO.puts("Hello, #{name}") end end hello("Linda") # Both hello "Linda" # Elixir only ;)
  4. Other familiar things: • Similar data structures (the immutable version)

    • Operations, comparisons... • Docstrings • Interactive interpreter • Exception handling (kind of...) • Comprehensions • Unicode support
  5. def update_profile_from_api(url): response = requests.get(url) raw_profile = response.json() profile =

    parse_profile(raw_profile) return store(profile) vs def update_profile_from_api(url) do response = HTTPoison.get!(url) raw_profile = Poison.decode!(response.body) profile = parse_profile(raw_profile) store(profile) end
  6. Atoms # Standalone: :ok :error # Map keys responses =

    %{ :ok => 200 } # instead of %{ "ok" => 200 } responses = %{ ok: 200 } IO.puts(responses.ok)
  7. Many more shiny things • Processes as a core feature

    • Macros (make your own DSLs!) • Increasingly evolving package ecosystem (Phoenix, Ecto, Nerves...) • Erlang's mature, battle tested, ecosystem
  8. Real time? • I want to use all my cores

    • Sockets please • The real world is concurrent • The real world has no GIL
  9. Second class citizen? Preventing mutation... def update_a_thing(thing): new_thing = thing.copy()

    new_thing.update("foo": "should I use deepcopy instead?") return new_thing
  10. Impromptu pattern-matching-single-dispatch- thing... def render(response): return { 200: _render_succesful, 400:

    _render_bad_request, 403: _render_forbidden, 404: _render_not_found, }[response.status](response) def _render_successful(response): return "OK" def _render_bad_request(response): return "Such and such things are wrong: " + response.errors
  11. Elixir def render(%{status: 200}) do "OK" end def render(%{status: 400}

    = response) do "Such and such things are wrong: " <> response.error_msg end def render(%{status: 403}) do "The FBI has been dispatched" end def render(%{status: 404}) do "Not Found" end
  12. Python is still great ❤ • But maybe you are

    curious about other tools • BONUS: You can use it with Elixir using Erlport
  13. Credits Icons made by - Smashicons - Flat Icons and

    - Freepik from www.flaticon.com, and are licensed CC 3.0 BY

More Decks by Eleni Lixourioti

Other Decks in Programming