KEMBAR78
Functional (web) development with Clojure | PDF
Functional
(web) development
with Clojure
Why functional programming?
Introduction.
Clojure is a strongly typed,
dynamic, functional programming
language that is defined in terms
of its own persistent data structures.
Introduction.
Introduction.
Primary performance
bottleneck
Complexity
Introduction.
Ability to comprehend complexity
Normal person Smartest person in the world
Complexity
Introduction.
Have to bring the complex
thing closer to us. The other
way around doesn’t work.
Introduction.
Hence, Clojure.
Functional programming is only
interesting to the degree it helps
us achieve this goal.
Clojure provides tools for the mind
rather than for the machine.
Introduction.
Persistent (immutable) data structures
Object/stateless programming model
Software Transactional Memory (STM)
Powerful data processing and transformation tools
Powerful data validation and testing tools
Strongly and optionally typed
Powerful (and easy) concurrency+parallelism
Instant feedback-driven development
Hosted on several platforms
Features of the language.
Variables complects* time and state.
Objects complects time, state and identity.
Methods complects function and state.
Syntax complects order and meaning.
…
The real features of the language.
*To complect: to create complexity, usually by
intertwining two or more unrelated concepts
Values
Values
Functions
Data
…
In the browser, on NodeJS, on JVM, on .Net, on
Android and in iOS. Same language.
Piggybacks on other stuff. Acting like a symbiote,
with direct, toll-less access to the underlying
platform if need be.
Symbiote.
Beauty
Brains
Sample stack.
“Client”
“Server”
App
API
NodeJS
ClojureScript
Browser
ClojureScript
Clojure
JVM
Shared state
Server-side rendering
Crossref Walmart The Daily Mail Akamai
Circle CI Metabase Boeing Atlassian
Netflix CitiGroup LinkedInThoughtWorks
Cisco Nubank …
In production.
Apache Storm
Language.
Clojure, the language
String “Lol”
Character &
Number 267
Keyword :elephant
Var get-pizza
Data structures.
List (1 2 3 4 5)
Vector [1 2 3 4 5]
Map {:hello “world”}
Set #{1 2 3 4 5}
Data structures.
(explore-temple :machete :whip :gun)
Syntax.
That’s it.
Syntax.
Given a map of args,
construct a URL string.
Example.
“Simplicity does not precede
complexity, but follows it.”
Example.
Alan Perlis
Example.
{:tool “whip”
:garment “hat”
:nationality “american”
:location “jungle”}
?tool=whip&garment=hat&nationality=american&location=jungle
First attempt.
(def args {:tool "whip"
:garment "hat"
:nationality "american"
:location "jungle"})
(defn stringify [arg]
(update-in arg [0] name))
(defn splice-eqal [[key-part val-part]]
[key-part "=" val-part])
( ->> args
(vec)
(map stringify)
(map splice-equal)
(interpose "&")
(flatten)
(apply str))
Final result.
(defn format-arg
"Takes a vector of two items, where the first is
a keyword, which it turns into a string. It then
splices in an = between the two items, and fuses
them all together into a single string.”
[[key-part val-part]]
(str (name key-part) "=" val-part))
(defn make-string
"Takes a vector of args as strings, interposes ‘&’
between them and then mashes everything in it
together into a string."
[args]
( ->> args (interpose “&”) (apply str)))
(defn make-args
"Turns a map of args into a string."
[args]
(make-string (map format-arg args)))
What about frontend development?
Example.
Frontend development.
;; The state
(defonce app-state (atom 0))
;; Functions for modifying state
(defn increase-int []
(swap! app-state inc))
(defn decrease-int []
(swap! app-state dec))
;; The "template" for page content
(defn hello-world []
[:div
[:h1 (str "Current value in app-state: " @app-state)]
[:button {:on-click increase-int} "Inc"]
[:button {:on-click decrease-int} "Dec"]])
;; How we mount the "template"
(reagent/render-component [hello-world]
(. js/document (getElementById "app")))
The rendered result
Most work is done in libraries.
core.async decouples systems, adds both thread-
based and in-thread asynchrony.
core.spec regexes for data structures: validation
(syntactically and semantically),
generates automatic tests, generates
documentation, generates APIs, etc.
transducers higher order functions on steroids.
transit type-aware data structures on the wire.
How to learn how to think.
Use it.
How to learn how to think.
“The value of a language that strongly enforces a paradigm is
that it immerses you in that approach to solving problems.
“The more it enforces the paradigm, the more you are forced to
attempt solutions using that paradigm.
“The more functional your language, the more training you get
in approaching problems in a functional way.“
Can I do FP in my language?
Eric Normand
Incentives.
Things to have a look at.
Simple Made Easy. How complexity impacts business performance.
https://www.infoq.com/presentations/Simple-Made-Easy
Success stories. Some examples of Clojure in production.
https://clojure.org/community/success_stories
Clojure TV on Youtube. Tons of lectures and education.
https://www.youtube.com/user/ClojureTV
Clojure for the Brave and True. A gentle introduction to the language.
https://www.braveclojure.com/clojure-for-the-brave-and-true/

Functional (web) development with Clojure

  • 1.
  • 2.
  • 3.
    Clojure is astrongly typed, dynamic, functional programming language that is defined in terms of its own persistent data structures. Introduction.
  • 4.
  • 5.
    Complexity Introduction. Ability to comprehendcomplexity Normal person Smartest person in the world
  • 6.
    Complexity Introduction. Have to bringthe complex thing closer to us. The other way around doesn’t work.
  • 7.
  • 8.
    Functional programming isonly interesting to the degree it helps us achieve this goal. Clojure provides tools for the mind rather than for the machine. Introduction.
  • 9.
    Persistent (immutable) datastructures Object/stateless programming model Software Transactional Memory (STM) Powerful data processing and transformation tools Powerful data validation and testing tools Strongly and optionally typed Powerful (and easy) concurrency+parallelism Instant feedback-driven development Hosted on several platforms Features of the language.
  • 10.
    Variables complects* timeand state. Objects complects time, state and identity. Methods complects function and state. Syntax complects order and meaning. … The real features of the language. *To complect: to create complexity, usually by intertwining two or more unrelated concepts Values Values Functions Data …
  • 11.
    In the browser,on NodeJS, on JVM, on .Net, on Android and in iOS. Same language. Piggybacks on other stuff. Acting like a symbiote, with direct, toll-less access to the underlying platform if need be. Symbiote.
  • 12.
  • 13.
    Crossref Walmart TheDaily Mail Akamai Circle CI Metabase Boeing Atlassian Netflix CitiGroup LinkedInThoughtWorks Cisco Nubank … In production. Apache Storm
  • 14.
  • 15.
    String “Lol” Character & Number267 Keyword :elephant Var get-pizza Data structures.
  • 16.
    List (1 23 4 5) Vector [1 2 3 4 5] Map {:hello “world”} Set #{1 2 3 4 5} Data structures.
  • 17.
  • 18.
  • 19.
    Given a mapof args, construct a URL string. Example.
  • 20.
    “Simplicity does notprecede complexity, but follows it.” Example. Alan Perlis
  • 21.
    Example. {:tool “whip” :garment “hat” :nationality“american” :location “jungle”} ?tool=whip&garment=hat&nationality=american&location=jungle
  • 22.
    First attempt. (def args{:tool "whip" :garment "hat" :nationality "american" :location "jungle"}) (defn stringify [arg] (update-in arg [0] name)) (defn splice-eqal [[key-part val-part]] [key-part "=" val-part]) ( ->> args (vec) (map stringify) (map splice-equal) (interpose "&") (flatten) (apply str))
  • 23.
    Final result. (defn format-arg "Takesa vector of two items, where the first is a keyword, which it turns into a string. It then splices in an = between the two items, and fuses them all together into a single string.” [[key-part val-part]] (str (name key-part) "=" val-part)) (defn make-string "Takes a vector of args as strings, interposes ‘&’ between them and then mashes everything in it together into a string." [args] ( ->> args (interpose “&”) (apply str))) (defn make-args "Turns a map of args into a string." [args] (make-string (map format-arg args)))
  • 24.
    What about frontenddevelopment? Example.
  • 25.
    Frontend development. ;; Thestate (defonce app-state (atom 0)) ;; Functions for modifying state (defn increase-int [] (swap! app-state inc)) (defn decrease-int [] (swap! app-state dec)) ;; The "template" for page content (defn hello-world [] [:div [:h1 (str "Current value in app-state: " @app-state)] [:button {:on-click increase-int} "Inc"] [:button {:on-click decrease-int} "Dec"]]) ;; How we mount the "template" (reagent/render-component [hello-world] (. js/document (getElementById "app"))) The rendered result
  • 26.
    Most work isdone in libraries. core.async decouples systems, adds both thread- based and in-thread asynchrony. core.spec regexes for data structures: validation (syntactically and semantically), generates automatic tests, generates documentation, generates APIs, etc. transducers higher order functions on steroids. transit type-aware data structures on the wire.
  • 27.
    How to learnhow to think. Use it.
  • 28.
    How to learnhow to think. “The value of a language that strongly enforces a paradigm is that it immerses you in that approach to solving problems. “The more it enforces the paradigm, the more you are forced to attempt solutions using that paradigm. “The more functional your language, the more training you get in approaching problems in a functional way.“ Can I do FP in my language? Eric Normand
  • 29.
  • 30.
    Things to havea look at. Simple Made Easy. How complexity impacts business performance. https://www.infoq.com/presentations/Simple-Made-Easy Success stories. Some examples of Clojure in production. https://clojure.org/community/success_stories Clojure TV on Youtube. Tons of lectures and education. https://www.youtube.com/user/ClojureTV Clojure for the Brave and True. A gentle introduction to the language. https://www.braveclojure.com/clojure-for-the-brave-and-true/