Slide 1

Slide 1 text

@AdamTornhill FUNCTIONAL PROGRAMMING IN THE REAL WORLD http://www.empear.com/

Slide 2

Slide 2 text

@AdamTornhill My Road To Functional Programming

Slide 3

Slide 3 text

Clojure @AdamTornhill My Road To Functional Programming 1980 1990 2000 2010 2015 Assembly C Pascal C++ Java TCL http://www.paulgraham.com/avg.html Erlang J?

Slide 4

Slide 4 text

Less Code, More Features, More Fun @AdamTornhill Erlang Java Lines of Code

Slide 5

Slide 5 text

The 60/60 Rule @AdamTornhill https://www.amazon.com/Facts-Fallacies-Software-Engineering-Robert/dp/0321117425/

Slide 6

Slide 6 text

What’s Functional Programming? @AdamTornhill “If you ask 100 programmers for their definition, you'll likely receive 100 different answers” — Michael Fogus & Chris Houser Immutability Lambda Calculus Recursion Lazy Evaluation Category Theory Monads Pure Functions Referential Transparency Hindley–Milner type inference

Slide 7

Slide 7 text

Immutable State @AdamTornhill i = 41;

Slide 8

Slide 8 text

5 ? 1 2 8 3 4 6 7 A Quick Reasoning Test @AdamTornhill

Slide 9

Slide 9 text

@AdamTornhill Don’t Turn Your Code Into An IQ Test state Side effects Working memory tax

Slide 10

Slide 10 text

IMMUTABILITY ALLOWS PROGRAMS TO SCALE IN COGNITIVE TERMS @AdamTornhill

Slide 11

Slide 11 text

TDD: A Diminishing Return in FP @AdamTornhill

Slide 12

Slide 12 text

REPL interactions -> soon to become unit tests REPL Driven Development @AdamTornhill

Slide 13

Slide 13 text

The Perils Of Test Setup @AdamTornhill Code from https://github.com/aspnet/Mvc

Slide 14

Slide 14 text

@AdamTornhill “The length of a test’s setup method is inversely related to the readability of the code under test.” —Adam’s Heuristic

Slide 15

Slide 15 text

Unit Tests With Immutable State 0 (ZERO) SETUP METHODS

Slide 16

Slide 16 text

Threading Macros @AdamTornhill (defn costs-by-file
 [costs file-name->ticket]
 (->> costs (map (partial assign-to-files file-name->ticket)) (filter passes-thresholds?) (reduce by-file-name))) Readability and Code Evolution

Slide 17

Slide 17 text

@AdamTornhill (defn costs-by-file
 [costs file-name->ticket]
 (->> costs (map (partial assign-to-files file-name->ticket)) (filter passes-thresholds?) (reduce by-file-name))) Performance And Lazy Evaluation lazy lazy

Slide 18

Slide 18 text

In Introductory Presentations …In The Real World @AdamTornhill Functional Programming

Slide 19

Slide 19 text

A Production System in FP @AdamTornhill https://codescene.io/

Slide 20

Slide 20 text

A Production Systems in FP @AdamTornhill https://codescene.io/ CodeScene on-premises Two

Slide 21

Slide 21 text

Analysing Android @AdamTornhill Analysis of https://github.com/android/platform_frameworks_base

Slide 22

Slide 22 text

@AdamTornhill docs core services packages tools media

Slide 23

Slide 23 text

@AdamTornhill

Slide 24

Slide 24 text

@AdamTornhill

Slide 25

Slide 25 text

@AdamTornhill

Slide 26

Slide 26 text

@AdamTornhill

Slide 27

Slide 27 text

The Need For Concurrency @AdamTornhill . . .

Slide 28

Slide 28 text

Concurrency Made Easy @AdamTornhill Possible (map complexity-trend files) (pmap complexity-trend files) Concurrency Exception!

Slide 29

Slide 29 text

Functional Architecture @AdamTornhill MACRO LEVEL: same difference SUB-SYSTEM LEVEL: more freedom ISOLATE SIDE EFFECTS Side effects should be clear and kept to a thin layer at the edge of your system.

Slide 30

Slide 30 text

… Social Architectural Pattern: Pipes And Filters Input Sources Git JIRA Teams Transformations Time Users Files Analyses Hotspots Effort Pattern Detectors Risk Neural Nets … Git Time Users Files Hotspots Early Warnings Run-time behaviour Result, purely functional, no side-effects

Slide 31

Slide 31 text

Remember The Pipeline Style? @AdamTornhill (defn costs-by-file
 [costs file-name->ticket]
 (->> costs (map (partial assign-to-files file-name->ticket)) (reduce by-file-name) (filter passes-thresholds?)))

Slide 32

Slide 32 text

… Social Architectural Pattern: Pipes And Filters Input Sources Git JIRA Teams Transformations Time Users Files Analyses Hotspots Effort Pattern Detectors Risk Neural Nets … Git Time Users Files Hotspots Early Warnings Run-time behaviour Result, purely functional, no side-effects

Slide 33

Slide 33 text

Beyond Myths: A Real-World Dirty REST API Domain specific language for describing routes Side-effects: 1. Read from DB 2. Fire-off a pipeline of business logic 3. Write the results to DB, reply

Slide 34

Slide 34 text

Finally A Silver Bullet? @AdamTornhill

Slide 35

Slide 35 text

LAZY EVALUATION IS GREAT @AdamTornhill …EXCEPT FOR WHEN IT’S NOT

Slide 36

Slide 36 text

@AdamTornhill (defn costs-by-file
 [costs file-name->ticket]
 (->> costs (map (partial assign-to-files file-name->ticket)) (reduce by-file-name) (filter passes-thresholds?))) The code we write: clojure.lang.AFn.applyToHelper (AFn.java:152) clojure.lang.AFn.applyTo (AFn.java:144) clojure.core$apply.invokeStatic (core.clj:646) clojure.core$apply.invoke (core.clj:641) codescene.analysis.diagnostics$trace_ns$fn__9583 clojure.lang.RestFn.invoke (RestFn.java:397) clojure.lang.AFn.applyToHelper (AFn.java:152) clojure.lang.RestFn.applyTo (RestFn.java:132) clojure.core$apply.invokeStatic (core.clj:646) clojure.core$apply.invoke (core.clj:641) codescene.analysis.diagnostics$trace_ns$fn__9583 clojure.lang.RestFn.invoke (RestFn.java:397) clojure.lang.AFn.applyToHelper (AFn.java:152) clojure.lang.RestFn.applyTo (RestFn.java:132) clojure.core$apply.invokeStatic (core.clj:646) clojure.core$apply.invoke (core.clj:641) codescene.analysis.diagnostics$trace_ns$fn__9583 clojure.lang.RestFn.invoke (RestFn.java:397) clojure.lang.AFn.applyToHelper (AFn.java:152) clojure.lang.RestFn.applyTo (RestFn.java:132) clojure.core$apply.invokeStatic (core.clj:646) clojure.core$apply.invoke (core.clj:641) codescene.analysis.diagnostics$trace_ns$fn__9583 clojure.lang.RestFn.invoke (RestFn.java:397) clojure.lang.AFn.applyToHelper (AFn.java:152) clojure.lang.RestFn.applyTo (RestFn.java:132) clojure.core$apply.invokeStatic (core.clj:646) clojure.core$apply.invoke (core.clj:641) codescene.analysis.diagnostics$trace_ns$fn__9583 clojure.lang.RestFn.invoke (RestFn.java:397) clojure.lang.AFn.applyToHelper (AFn.java:152) clojure.lang.RestFn.applyTo (RestFn.java:132) clojure.core$apply.invokeStatic (core.clj:646) The exception we get Clojure Specific: The Semantic Gap

Slide 37

Slide 37 text

Java — The Assembly Language of the JVM @AdamTornhill Dealing With Performance Issues

Slide 38

Slide 38 text

Immutability is Key @AdamTornhill Takeaways Think Pipelines - Functions and Architecture No Silver Bullet - Programming is still hard

Slide 39

Slide 39 text

@AdamTornhill Beyond Functional Programming http://www.adamtornhill.com/articles/jlang/beyondfunctional.html The Day I Parsed A Monster http://www.empear.com/blog/parse-a-monster/ Lisp for the Web (free) https://leanpub.com/lispweb Try CodeScene https://codescene.io/ [email protected]