Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

language paradigms functional object-oriented structured logic

Slide 4

Slide 4 text

the good

Slide 5

Slide 5 text

What is functional programming? first-class functions immutability

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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))))

Slide 11

Slide 11 text

immutability (defn- get-item-value [item] (* (:price item) (:quantity-available item))) (defn get-total-value [inventory] (->> (:items inventory) (map get-item-value) (reduce +)))

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

#1 hit benefits safer concurrency / parallelism simplicity / clarity easier testing / modularity useful constraints

Slide 14

Slide 14 text

the familiar

Slide 15

Slide 15 text

"This FP stuff sounds great… for toy examples"

Slide 16

Slide 16 text

How do I structure larger systems in FP?

Slide 17

Slide 17 text

How do I structure larger systems in OO?

Slide 18

Slide 18 text

What tools does OO provide? polymorphism namespacing encapsulation inheritance/composition data containment state change

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

OO Principles DRY 4 rules of simple design command-query separation SOLID principles package principles code smells refactorings

Slide 22

Slide 22 text

OO FP Software Principles DRY 4 rules of simple design command-query separation SOLID principles package principles code smells refactorings

Slide 23

Slide 23 text

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.

Slide 24

Slide 24 text

What is an "abstraction"? interfaces (Java, …) classes w/ only pure virtual methods (C++, …) duck typing (Ruby, …)

Slide 25

Slide 25 text

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!!!)

Slide 26

Slide 26 text

the unknown

Slide 27

Slide 27 text

Risk

Slide 28

Slide 28 text

Learning curve

Slide 29

Slide 29 text

The virtues of boring tech

Slide 30

Slide 30 text

Mitigating by learning practice reading community teammates/consultants

Slide 31

Slide 31 text

Mitigating with use cases innovation budget developer tools testing breakable toys/katas low-risk applications/services

Slide 32

Slide 32 text

Takeaways

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Thanks! Colin Jones @trptcolin