$30 off During Our Annual Pro Sale. View Details »

Functional Programming for Fun and Profit!!

Jenny Shih
November 18, 2022

Functional Programming for Fun and Profit!!

Presented at RubyConf Mini 2022

Jenny Shih

November 18, 2022
Tweet

More Decks by Jenny Shih

Other Decks in Technology

Transcript

  1. @jenny-codes Functional programming for fun and profit!!

  2. @jenny_codes https://github.com/readme/featured/functional-programming

  3. @jenny_codes Source: https://youtu.be/ADqLBc1vFwI

  4. @jenny_codes Source: https://giphy.com/gifs/emoji-idk-thinking-3o7buirYcmV5nSwIRW What is functional programming?

  5. @jenny_codes Source: https://giphy.com/gifs/emoji-idk-thinking-3o7buirYcmV5nSwIRW What is functional programming? Should I care

    about it if I just want to write Ruby code?
  6. Functional programming helps us write better Ruby code

  7. @jenny_codes Jenny Shih / 施靜樺

  8. @jenny_codes Jenny Shih / 施靜樺 🇹🇼 Taiwan -> 🇨🇦 Canada

    💎 💧 https://codecharms.me
  9. Programming paradigms A way to classify programming languages based on

    their features
  10. @jenny_codes Programming paradigms • Object Oriented Programming (OO) • Functional

    Programming (FP)
  11. Mental model

  12. @jenny_codes Value Time Behavior A program

  13. @jenny_codes

  14. @jenny_codes Value Behavior

  15. @jenny_codes

  16. @jenny_codes Time

  17. @jenny_codes Value Time Behavior A program State

  18. @jenny_codes Object An OO program Value Behavior Time Time

  19. @jenny_codes An OO program Object Value Behavior Object Value Behavior

    Object Value Behavior Time Time Time Time Time Time
  20. @jenny_codes A FP program Value Time Behavior

  21. @jenny_codes A FP program Value Time Behavior

  22. Immutability

  23. Immutability Don’t change a value once you initialize it.

  24. @jenny_codes Immutability

  25. @jenny_codes Immutability

  26. @jenny_codes Immutability

  27. @jenny_codes Immutability

  28. @jenny_codes Source: https://giphy.com/gifs/emoji-idk-thinking-3o7buirYcmV5nSwIRW Why?

  29. @jenny_codes Immutability: Why?? • Peace of mind

  30. @jenny_codes Immutability: Why??

  31. @jenny_codes Immutability: Why?? • Peace of mind

  32. @jenny_codes Immutability: Why?? • Peace of mind • Eliminate shared

    state
  33. @jenny_codes Immutability: Why?? • Peace of mind • Eliminate shared

    state
  34. @jenny_codes Immutability: How???

  35. @jenny_codes Immutability: How??? • Provide a good reason for every

    value mutation.
  36. @jenny_codes Immutability: How??? • Provide a good reason for every

    value mutation. • Use tools
  37. @jenny_codes

  38. @jenny_codes https://dry-rb.org/

  39. @jenny_codes Immutability: How??? • Provide a good reason for every

    value mutation. • Use tools • `dry-struct`: https://dry-rb.org/gems/dry-struct/main/
  40. @jenny_codes Immutability: How??? • Provide a good reason for every

    value mutation. • Use tools • `dry-struct`: https://dry-rb.org/gems/dry-struct/main/ • `Data.de fi ne` in Ruby
  41. @jenny_codes

  42. @jenny_codes

  43. @jenny_codes https://dev.to/baweaver/new-in-ruby-32-datade fi ne-2819

  44. @jenny_codes Immutability: How??? • Provide a good reason for every

    value mutation. • Use tools • `dry-struct`: https://dry-rb.org/gems/dry-struct/main/ • `Data.de fi ne` in Ruby
  45. @jenny_codes A FP program Value Time Behavior

  46. @jenny_codes A FP program Value Time Behavior Immutable data

  47. @jenny_codes A FP program Value Time Behavior Immutable data

  48. Pure functions

  49. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  50. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  51. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  52. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  53. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  54. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  55. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  56. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  57. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  58. @jenny_codes Source: https://giphy.com/gifs/emoji-idk-thinking-3o7buirYcmV5nSwIRW Why?

  59. @jenny_codes “The problem with OO languages is they’ve got all

    this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.” – Joe Armstrong, Creator of Erlang Pure functions: Why??
  60. @jenny_codes “The problem with OO languages is they’ve got all

    this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.” – Joe Armstrong, Creator of Erlang Pure functions: Why??
  61. @jenny_codes Pure functions: Why?? No implicit environment!! Source: https://thenounproject.com/icon/forest-5299391/ Source:

    https://thenounproject.com/icon/monkey-298282/
  62. @jenny_codes Pure functions: Why?? No implicit environment!! Source: https://thenounproject.com/icon/forest-5299391/ Source:

    https://thenounproject.com/icon/monkey-298282/
  63. @jenny_codes Pure functions: Why?? No implicit environment!! 🤯

  64. @jenny_codes Pure functions: Why?? No implicit environment!!

  65. @jenny_codes Pure functions: Why?? No implicit environment!! 🤘

  66. @jenny_codes Pure functions: Why?? No implicit environment!! 🤘🐛 🔪

  67. @jenny_codes Pure functions: Why?? No implicit environment!! 🤘🐛 🔪 🧪

  68. @jenny_codes Pure functions: Why?? No implicit environment!! 🤘🐛 🔪 🧪

  69. @jenny_codes Pure functions: Why?? No implicit environment!! 🤘🐛 🔪 🧪

    ✅ ✌
  70. @jenny_codes Pure functions: Why?? No implicit environment!! 🤘🐛 🔪 🧪

    ✅ ✌ Source: https://www.ktiv.com/2022/10/07/nobel-peace-prize-awarded-human-rights-advocates/
  71. @jenny_codes Pure functions: How???

  72. @jenny_codes Pure functions: How??? • Parameterization/Dependency Injections

  73. @jenny_codes Pure functions: Parameterization

  74. @jenny_codes Pure functions: Parameterization

  75. @jenny_codes Pure functions: Parameterization

  76. @jenny_codes Pure functions: Parameterization

  77. @jenny_codes Pure functions: Parameterization

  78. @jenny_codes Pure functions: Parameterization

  79. @jenny_codes Pure functions: Parameterization

  80. @jenny_codes Pure functions: Parameterization

  81. @jenny_codes Pure functions: Parameterization

  82. @jenny_codes Pure functions: Parameterization

  83. @jenny_codes Pure functions: How??? • Parameterization/Dependency Injections • Transformation instead

    of mutation
  84. @jenny_codes Pure functions: Transformation over Mutation

  85. @jenny_codes Pure functions: Transformation over Mutation

  86. @jenny_codes Pure functions: Transformation over Mutation

  87. @jenny_codes 1. Only depends on its input argument 2. Does

    not mutate states (no side effects) A pure function
  88. @jenny_codes A pure function 1. Only depends on its input

    argument ✅ 2. Does not mutate states (no side effects)
  89. @jenny_codes A pure function 1. Only depends on its input

    argument ✅ 2. Does not mutate states ✅ (no side effects)
  90. @jenny_codes A FP program Value Time Behavior Immutable data

  91. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions
  92. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions
  93. Concurrency

  94. @jenny_codes An OO program Object Value Behavior Object Value Behavior

    Object Value Behavior Time Time Time Time Time Time
  95. @jenny_codes An OO program Object Value Behavior Object Value Behavior

    Object Value Behavior Time Time Time Time Time Time Global Interpreter Lock 🔐
  96. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions Concurrency 💯
  97. @jenny_codes Elixir’s concurrency model: Actors Actor / Process Actor /

    Process Actor / Process
  98. @jenny_codes Elixir’s concurrency model: Actors Thread Thread Thread

  99. @jenny_codes “It’s multi-core age today. Concurrency is very important. With

    Ractor, along with Async Fiber, Ruby will be a real concurrent language.” — Matz Ruby’s concurrency abstraction: Ractor https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/
  100. @jenny_codes Ractor Ractor Ractor ︴︴︴ ︴︴︴ ︴︴︴ Ruby’s concurrency abstraction:

    Ractor ︴︴︴= threads
  101. @jenny_codes Ractor Ractor Ractor 🔐 🔐 🔐 ︴︴︴ ︴︴︴ ︴︴︴

    🔐 = Global Interpreter Lock Ruby’s concurrency abstraction: Ractor ︴︴︴= threads
  102. @jenny_codes Ractor Ractor Ractor 🔐 🔐 🔐 ︴︴︴ ︴︴︴ ︴︴︴

    🔐 = Global Interpretation Lock Ruby’s concurrency abstraction: Ractor ︴︴︴= threads ❓
  103. @jenny_codes

  104. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions Concurrency
  105. Recap

  106. @jenny_codes We are defined not so much by what we

    can do, but what we don’t do.
  107. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions Concurrency
  108. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions Concurrency
  109. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions Concurrency
  110. @jenny_codes A FP program Value Time Behavior Immutable data Pure

    functions Concurrency
  111. Functional programming helps us write better Ruby code

  112. @jenny_codes Resources: Functional programming mental model • Jose Valim’s ElixirConf

    2017 Keynote by Jose Valim • Are we there yet by Rich Hickey • The language of programming by Anjana Vakil • Value Object by Martin Fowler
  113. @jenny_codes Resources: Functional programming architecture (That I didn’t get to

    talk about because I’d get too excited and run overtime) • Functional Architecture for the Practical Rubyist by Tim Riley • Functional web with Ruby by Luca Luigi • Boundaries by Gary Bernhardt • The Hanami Ruby framework • (Book) Domain Modelling Made Functional by Scott Wlaschin
  114. @jenny_codes Resources: Ruby docs • https://ruby-doc.org/core-3.2.0_preview2/Ractor.html • https://scoutapm.com/blog/ruby-ractor • https://dev.to/baweaver/new-in-ruby-32-datade

    fi ne-2819 • https://github.com/ruby/ruby/pull/6353