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

Functional Programming Essentials with Elixir

Functional Programming Essentials with Elixir

The world is changing, new languages comes and go, however, a paradigm change is something rare. We need to understand this change to be ready to develop software for the next decades. In this talk, you’ll learn why functional programming matters. You’ll see 3 essential concepts of functional programming: immutability, declarative code and pure functions. We’ll explore how functional programming concepts will help you create software with high parallelism for better use of modern hardware.

50e713934ed341675bf1fa73127ec260?s=128

Ulisses Almeida

November 08, 2018
Tweet

Transcript

  1. FP Essentials with Elixir Elixir Meetup, Tallinn November/2018

  2. 3 concepts Functional Programming Essentials with Elixir Immutability Functions Declarative

    programming 3 concepts that shapes everything you do while programming in Elixir.
  3. Ulisses Almeida Functional Programming Essentials with Elixir @ulissesalmeida

  4. Ulisses Almeida Functional Programming Essentials with Elixir Author of Learn

    Functional Programming with Elixir The Pragmatic Bookshelf
  5. Who We Are Functional Programming Essentials with Elixir The Coingaming

    Group are the crypto gaming pioneers who operate the world’s leading bitcoin gaming and betting brands.
  6. 3 concepts Functional Programming Essentials with Elixir Immutability Functions Declarative

    programming 3 concepts that shapes everything you do while programming in Elixir.
  7. Why?

  8. Parallelism Functional Programming Essentials with Elixir

  9. Parallelism Functional Programming Essentials with Elixir Transistors number are increasing,

    Frequency and Performance are stable, Number of cores are increasing
  10. Parallelism Functional Programming Essentials with Elixir Transistors number are increasing,

    Frequency and Performance are stable, Number of cores are increasing
  11. Parallelism Functional Programming Essentials with Elixir Transistors number are increasing,

    Frequency and Performance are stable, Number of cores are increasing
  12. AMD Ryzen Functional Programming Essentials with Elixir Up to 32

    cores
  13. Joe Armstrong Functional Programming Essentials with Elixir Your Erlang program

    should just run N times faster on an N core processor https://pragprog.com/articles/erlan g
  14. Amdahl's Law Functional Programming Essentials with Elixir A large portion

    of your task/program must run in parallel to take advantage of multi-core architecture
  15. Amdahl's Law Functional Programming Essentials with Elixir A large portion

    of your task/program must run in parallel to take advantage of multi-core architecture
  16. Amdahl's Law Functional Programming Essentials with Elixir If we apply

    2 cores, we can speed up twice the parallel portion 4 minutes 4 minutes 4 minutes 2 2 cores
  17. Amdahl's Law Functional Programming Essentials with Elixir If we double

    the number of cores, we can double the speed of the parallel portion 4 minutes 4 minutes 4 minutes 2 4 minutes 1 2 cores 4 cores
  18. Amdahl's Law Functional Programming Essentials with Elixir Double it again...

    4 minutes 4 minutes 4 minutes 2 4 minutes 1 4 minutes 2 cores 4 cores 8 cores
  19. Amdahl's Law Functional Programming Essentials with Elixir ...and again… It

    doesn't matter anymore. We can have at maximum 50% time improvement. 4 minutes 4 minutes 4 minutes 2 4 minutes 1 4 minutes 4 minutes 2 cores 4 cores 8 cores 16 cores
  20. Amdahl's Law Functional Programming Essentials with Elixir A large portion

    of your task/program must be parallelable to take advantage of multi-core architecture
  21. Functional Programming Essentials with Elixir .4 7,6 minutes .4 .4

    .4 2 cores 4 cores 8 cores 16 cores 4 minutes 4 minutes 4 minutes 2 4 minutes 1 4 minutes 4 minutes 2 cores 4 cores 8 cores 16 cores 50% 95% 3,8 minutes 1,9 minutes .4 10x faster 2x faster
  22. Threads Functional Programming Essentials with Elixir Is parallel programming hard

    or is something wrong in our way of thinking?
  23. Complexity Functional Programming Essentials with Elixir How reduce complexity in

    a parallel world?
  24. Functional Programming

  25. None
  26. 3 concepts Functional Programming Essentials with Elixir Immutability Functions Declarative

    programming 3 concepts that shapes everything you do while programming in Elixir.
  27. Functions

  28. Pure Functions Functional Programming Essentials with Elixir Limited to arguments

    Immutable values add2 = fn (n) -> n + 2 end add2.(2) # => 4 code = 10 add2.(code) # => 12
  29. Explicit arguments Functional Programming Essentials with Elixir Is it good

    or bad? # Ruby cart .add_product(product) .create_order .notify # Elixir cart = Cart.add(cart, product) order = Order.create(cart, order) Mailer.notify(order, to: user)
  30. First-class citizens Functional Programming Essentials with Elixir Functions are like

    any other data type. You can return as functions results or use them as arguments. Enum.map( ["dogs", "cats", "flowers"], &String.upcase/1 ) # => ["DOGS", "CATS", "FLOWERS"]
  31. |> Data transformation Functional Programming Essentials with Elixir Chaining functions

    can be hard to read. def capitalize_words(title) do join_with_whitespace( capitalize_all( String.split(title) ) ) end capitalize_words("the dark tower") # => "The Dark Tower"
  32. |> Data transformation Functional Programming Essentials with Elixir Pipe operator

    clarify the order of the execution and the data transformation flow. def capitalize_words(title) do title |> String.split() |> capitalize_all() |> join_with_whitespace() end capitalize_words("the dark tower") # => "The Dark Tower"
  33. Declarative Programming

  34. Imperative programming Functional Programming Essentials with Elixir Focus on how

    things need to be done... // Javascript function capitalizeAll(list) { var newList = []; for (var i = 0; i < list.length; i++) { newList.push( capitalize(list[i]) ); } return newList; }
  35. Imperative programming Functional Programming Essentials with Elixir ...and it works

    // Javascript var list = [ "dogs", "hot dogs", "Bananas" ]; capitalizeAll(list); // => ["Dogs", "Hot dogs", "Bananas"]
  36. Declarative programming Functional Programming Essentials with Elixir Focus on what

    needs to be done... # Elixir defmodule StringList do def capitalize_all([]), do: [] def capitalize_all([first | rest]) do [ String.capitalize(first) | capitalize_all(rest) ] end end
  37. Declarative programming Functional Programming Essentials with Elixir Many ways of

    being declarative # Elixir list = [ "dogs", "hot dogs", "Bananas" ]; Enum.map( list, &String.capitalize/1 ); # => ["Dogs", "Hot dogs", "Bananas"]
  38. Declarative programming Functional Programming Essentials with Elixir Many ways of

    being declarative in imperative languages // Javascript var list = [ "dogs", "hot dogs", "Bananas" ]; list.map((i) => i.toUpperCase()) // => ["DOGS", "HOT DOGS", "BANANAS"] # Ruby list.map(&:upcase) # => ["DOGS", "HOT DOGS", "BANANAS"]
  39. Immutability

  40. Mutability Functional Programming Essentials with Elixir Values has a tendency

    to change after their creation. # Ruby list = [1, 2, 3, 4] list.pop puts list.inspect # => [1, 2, 3] list.push(1) puts list.inspect # => [1, 2, 3, 1]
  41. Immutability Functional Programming Essentials with Elixir Values can't change after

    their creation. # Elixir list = [1, 2, 3, 4] new_list = List.delete_at(list, -1) IO.inspect(list) # => [1, 2, 3, 4] IO.inspect(new_list) # => [1, 2, 3] new_list = list ++ [5] IO.inspect(list) # => [1, 2, 3, 4] IO.inspect(new_list) # => [1, 2, 3, 4, 5]
  42. Immutability Functional Programming Essentials with Elixir Immutability in other languages

    sometimes doesn't work as expected. # Ruby User = Struct.new(:name) list = [ User.new("Bob"), User.new("Anna") ] list.freeze list.push(User.new("July")) # => RuntimeError: can't modify frozen Array list[0].name = "Ted" puts list[0].inspect # => <struct User name="Ted">
  43. Sharing values with mutability Functional Programming Essentials with Elixir Let's

    share an account to multiple simultaneously operations. # Ruby Account = Struct.new(:amount) do def deposit(value) new_amount = amount + value sleep(1) self.amount = new_amount end def withdraw(value) new_amount = amount - value sleep(1) self.amount = new_amount end end
  44. Sharing values with mutability Functional Programming Essentials with Elixir Doing

    simple operations it handles well. # Ruby ulisses = Account.new(100) ulisses.deposit(20) ulisses.withdraw(20) ulisses.amount # => 100
  45. Sharing values with mutability Functional Programming Essentials with Elixir Let's

    increase a little bit the complexity # Ruby Thread.new { ulisses.deposit(20) } Thread.new { ulisses.withdraw(20) }
  46. Sharing values with mutability Functional Programming Essentials with Elixir Little

    bit more... # Ruby ulisses = Account.new(100) threads = 5.times.map { Thread.new { ulisses.deposit(20) } } + 5.times.map { Thread.new { ulisses.withdraw(20) } } threads.each(&:join) puts ulisses.amount
  47. Sharing values with mutability Functional Programming Essentials with Elixir The

    results are not consistent. Using threads with mutability are not safe by default. You need abstractions! # Ruby 5.times { # do all that stuff } # => 120 # => 80 # => 100 # => 80 # => 120
  48. Sharing values with mutability Functional Programming Essentials with Elixir Of

    course. That wasn't a safe implementation using threads. Always look for a abstractions: Actor Model, CSP, Promises, Semaphore... concurrent-ruby java.util.concurrent PHP: spawn Python: concurrent.futures
  49. Sharing values with immutability Functional Programming Essentials with Elixir What

    to do when you can't change a reference directly? # Elixir amount = 100 Task.async(fn -> amount = amount + 20 end) IO.inspect amount # => 100
  50. Sharing values with immutability Functional Programming Essentials with Elixir Elixir

    forces you to use a safe abstraction. Agent allows you to store a state and serialize changes using a process. # Elixir {:ok, ulisses} = Agent.start_link(fn -> 100 end)
  51. Sharing values with immutability Functional Programming Essentials with Elixir You

    can communicate to Erlang process using messages. The process consumes each message in their arrival order. Process Msg 1 Msg 2 Msg 3 Msg ... Another process State
  52. Sharing values with immutability Functional Programming Essentials with Elixir We

    can create functions that update and check the process state. # Elixir deposit = fn pid, value -> Agent.update(pid, fn total -> Process.sleep(100) total + value end) end withdraw = fn pid, value -> Agent.update(pid, fn total -> Process.sleep(100) total - value end) End balance = fn p -> Agent.get(p, &(&1)) end
  53. Sharing values with immutability Functional Programming Essentials with Elixir Now,

    we can update that account and check their balance. # Elixir deposit.(ulisses, 10) IO.inspect balance.(ulisses) # => 110
  54. Sharing values with immutability Functional Programming Essentials with Elixir Let's

    do a similar thing as the Ruby example. {:ok, ulisses} = Agent.start_link(fn -> 100 end) withdraws = for _i <- 1..5, do Task.async(fn -> deposit.(ulisses, 20) end) end deposits = for _i <- 1..5, do Task.async(fn -> withdraw.(ulisses, 20) end) end tasks = withdraws ++ deposits Enum.each(tasks, &Task.await/1) IO.inspect balance.(ulisses) Agent.stop(ulisses)
  55. Sharing values with immutability Functional Programming Essentials with Elixir We

    can do asynchronously. # Elixir Task.async(fn -> deposit.(ulisses, 20) end) Task.async(fn -> withdraw.(ulisses, 20) end) # ... IO.inspect balance.(ulisses) # => 100
  56. Sharing values with immutability Functional Programming Essentials with Elixir The

    result is consistent since the messages are serialized for _i <- 1..5 do # do all that 5 times end # 100 # 100 # 100 # 100 # 100
  57. Recap

  58. Parallelism Functional Programming Essentials with Elixir Transistor number are increasing,

    Frequency and Performance are stable, Number of cores are increasing
  59. Amdahl's Law Functional Programming Essentials with Elixir A large portion

    of your task/program must be parallelable to take advantage of multi-core architecture
  60. Complexity Functional Programming Essentials with Elixir How reduce complexity in

    a parallel world?
  61. 3 concepts Functional Programming Essentials with Elixir Immutability Functions Declarative

    programming 3 concepts that shapes everything you do while programming in Elixir.
  62. Be Part of a Revolution Functional Programming Essentials with Elixir

    . Facebook /coingaming.io LinkedIn company/coingaming Instagram /coingaming