Pro Yearly is on sale from $80 to $50! »

Adopting FP: the good, the familiar, and the unknown

Adopting FP: the good, the familiar, and the unknown

E16bc9c356b65d61ee1d74c8f06ae35b?s=128

Colin Jones

July 21, 2016
Tweet

Transcript

  1. Adopting FP: the good, the familiar, and the unknown Colin

    Jones @trptcolin
  2. Adopting FP: the good, the familiar, and the unknown Colin

    Jones @trptcolin
  3. language paradigms functional object-oriented structured logic

  4. the good

  5. What is functional programming? first-class functions immutability

  6. first-class functions def presented_users users = User.where(active: true) users.map {|user|

    UserPresenter.new(user)} end
  7. first-class functions def present_user lambda {|user| UserPresenter.new(user)} end def presented_users

    users = User.where(active: true) users.map(&present_user) end
  8. immutability def get_client_id(params) params[:warehouse][:client_id] end def test_client_id_equality params = {warehouse:

    {client_id: 42}} client_id_1 = get_client_id(params) client_id_2 = get_client_id(params) assert_equal(client_id_1, client_id_2) end
  9. immutability def get_client_id(params) params[:warehouse].delete(:client_id) end def test_client_id_equality params = {warehouse:

    {client_id: 42}} client_id_1 = get_client_id(params) client_id_2 = get_client_id(params) assert_equal(client_id_1, client_id_2) end
  10. immutability (deftest test-client-id-equality (let [params {:warehouse {:client-id 42}} client-id-1 (get-client-id

    params) client-id-2 (get-client-id params)] (is (= client-id-1 client-id-2))))
  11. immutability (defn- get-item-value [item] (* (:price item) (:quantity-available item))) (defn

    get-total-value [inventory] (->> (:items inventory) (map get-item-value) (reduce +)))
  12. immutability (let [params {:warehouse {:client-id 42}} updated (update-in params [:warehouse

    :client-id] inc)] (println "params:" (pr-str params)) (println "updated:" (pr-str updated))) ;; params: {:warehouse {:client-id 42}} ;; updated: {:warehouse {:client-id 43}} ;; => nil
  13. #1 hit benefits safer concurrency / parallelism simplicity / clarity

    easier testing / modularity useful constraints
  14. the familiar

  15. "This FP stuff sounds great… for toy examples"

  16. How do I structure larger systems in FP?

  17. How do I structure larger systems in OO?

  18. What tools does OO provide? polymorphism namespacing encapsulation inheritance/composition data

    containment state change
  19. What tools does FP provide? polymorphism namespacing encapsulation inheritance/composition data

    containment state change
  20. What tools does FP provide? polymorphism namespacing encapsulation inheritance/composition data

    containment state change
  21. OO Principles DRY 4 rules of simple design command-query separation

    SOLID principles package principles code smells refactorings
  22. OO FP Software Principles DRY 4 rules of simple design

    command-query separation SOLID principles package principles code smells refactorings
  23. Example: Dependency Inversion Principle A. High level modules should not

    depend upon low level modules. Both should depend upon abstractions. B. Abstractions should not depend upon details. Details should depend upon abstractions.
  24. What is an "abstraction"? interfaces (Java, …) classes w/ only

    pure virtual methods (C++, …) duck typing (Ruby, …)
  25. What is an "abstraction"? duck typing (Erlang, Elixir, …) generic

    types (Haskell, Elm, F#, …) protocols (Clojure, Swift, Elixir, …) multimethods (Clojure, CLOS, …) typeclasses (Haskell, …) signatures (ML, OCaml, …) behaviours (Erlang, …) functions (we all have these!!!)
  26. the unknown

  27. Risk

  28. Learning curve

  29. The virtues of boring tech

  30. Mitigating by learning practice reading community teammates/consultants

  31. Mitigating with use cases innovation budget developer tools testing breakable

    toys/katas low-risk applications/services
  32. Takeaways

  33. Reading "Why Functional Programming Matters" by John Hughes https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf "What

    is Functional Programming?" by Kris Jenkins http://blog.jenkster.com/2015/12/what-is-functional-programming.html "Go To Statement Considered Harmful" by Edsger Dijkstra http://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf "Understanding Clojure's Persistent Vectors" by Jean-Niklas L'Orange http://hypirion.com/musings/understanding-persistent-vector-pt-1 "Clojure Protocols" by Stuart Halloway http://gotocon.com/dl/jaoo-aarhus-2010/slides/StuartHalloway_ClojureProtocolsArenotInterfaces.pdf "Choose Boring Technology" by Dan McKinley http://mcfunley.com/choose-boring-technology
  34. Thanks! Colin Jones @trptcolin