Top Banner
Functional Web with Clojure by John Stevenson @jr0cket clojure.practical.li
73

Functional web with clojure

Jan 18, 2017

Download

Software

John Stevenson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functional web with clojure

Functional Web with Clojure

by John Stevenson @jr0cket clojure.practical.li

Page 2: Functional web with clojure

@jr0cketSpeaker, author, conference organiser & community obsessed

developer.

Love Clojure, Emacs, Cats, Cycling & Agile development.

@Heroku@SalesforceDevs

#Trailhead

In a galaxy far, far away… London, UK

Page 3: Functional web with clojure

Why Functional Programmingit's not just because it's really fun...

Page 5: Functional web with clojure

The Complexity Iceberg

- @krisajenkins

● complexity is very dangerous when hidden

● You can't know what a function does for certain if it has side effects

Page 6: Functional web with clojure

Side Effects

Page 7: Functional web with clojure

Pure FunctionsThe results of the function are purely determined by its initial output and its own code

- no external influence, a function only uses local values- referential transparency (the function can be replaced by its value)

Page 8: Functional web with clojure

Impure Functions - side causesThe results of the function are purely determined by its initial output and its own code

- behaviour externally influenced and non-deterministic

Page 9: Functional web with clojure

Eliminating Side Effects

Functional programming is about eliminating side effects where you can, controlling them where you can't - @krisajenkins

The features in Functional Programming come from a desire to reduce side effects

Page 10: Functional web with clojure

ClojureGeneral purpose language hosted on JVM, JavaScript & CLR

Page 11: Functional web with clojure

Clojure / ClojureScript

A hosted language with simple interoperability with the host language

- (java.Util.Date.)- (js/alert “Client side apps are easier in Clojure”)

Page 12: Functional web with clojure

Clojure - basic syntax for this talk( ) ;; an empty list. The first element of a list is evaluated as a function call

(function-name data) ;; call a function with the data as its argument

(def name “data-or-value”) ;; assign (bind) a name to a data or legal value

:keyword-name ;; a keyword is a name that points to itself

;; Thread-first macro - chain function calls, passing the result of each call as the first argument to the next function. The ,,, indicates where the resulting argument goes.

(-> (function-a “data”) (function-b ,,,) ;; In Clojure commas , are whitespace (function-c ,,, “data”))

Page 13: Functional web with clojure

Removing boilerplate code by not defining types ever where

Page 14: Functional web with clojure

Persistent Data Structures - List, Vector, Map & SetClojure’s built-in data structures are all immutable- returning a new data structure when a function is applied

(list 1 2 3 4 5) ‘(“fish” “chips” 42)

(vec ‘(1 2 3 4)) [1 2 3 4]

{:key “value”} {:name “John” :skill “conferencing”}

(set ‘(1 2 3 4 4)) #{1 2 3 4}

Page 15: Functional web with clojure

Persistent Data Structures shared memory

Each function creates a new vector

Memory space for values is shared between each vector

Page 16: Functional web with clojure

Persistent Data Structures shared memory

By sharing memory you can apply functions over and over again effectively

Values persist until they are no longer referenced

Page 17: Functional web with clojure

Higher Order FunctionsFunctions always return a value & can be used as an argument to another function

Page 18: Functional web with clojure

Composing functions togetherExample: current value of the Clojure project from the configuration file- `slurp` in the project file, convert into a string and return the value at index 2

Page 19: Functional web with clojure

RecursionProcess a collection of values by feeding the remaining elements back to the function

- the sum function is polymorphic, it has different behaviours that could be evaluated depending on if passed 1 or 2 arguments

Page 20: Functional web with clojure

Recursion - tail call optomisationProtect the heap space from blowing by using the recur function

Using recur in the last line is the same as calling sum, however the memory required from the previous sum function call is over-written in memory. So only 1 memory slot is used instead of 10 billion

Page 21: Functional web with clojure

Lazy EvaluationOnly return a value when necessary

● maintain precision● optomise evaluation

Page 22: Functional web with clojure

Sequence / List Comprehension

Iterate over collections

Page 23: Functional web with clojure

Sequence / List Comprehension

Iterating through a range of generated values to create a list of 2 value vectors

Page 24: Functional web with clojure

Immutability - local binding

Assignments made locally are immutable

Page 25: Functional web with clojure

Concurrency is Easier

Concurrency is much easier to write and reason about because of

- Immutability by default- Persistent Data Structures- values are immutable- functional isolation & pure functions- state changes managed atomically (software transactional memory)- core.async library allows you to write asynchronous code as easily as sequential

code

Page 26: Functional web with clojure

Safe State changes

Changing state safely by not changing it

● Persistent data structures● Local bindings

Changing state safely by changing it atomically

● Software Transactional Memory (STM)○ Gives an mechanism like an in-memory atomic database that manages mutable state changes

under the covers

● Atoms● core.async

Page 27: Functional web with clojure

Concurrency syntax - atoms

An online card game has players that can join and have their winnings tracked

Page 28: Functional web with clojure

Concurrency syntax - atoms

The join-game function adds players to the atom by their name, but only up to 2 players

Page 29: Functional web with clojure

Concurrency syntax - refs for sync updatesThe join-game-safely adds players to the ref and alters their account & game account

Page 30: Functional web with clojure

Putting all the basics togetherLet's find all the most common words used in a popular Science Fiction novel

Page 31: Functional web with clojure

Clojure - Functional WebImmutability & composable functions for Web Apps

Page 32: Functional web with clojure

Ring - simplifying communication

Ring takes requests over HTTP and converts them into a map (think JSON)

Page 33: Functional web with clojure

Ring - creating an embedded server

Page 34: Functional web with clojure

Compojure - simplifying routingCompojure routes requests based on path & request type

Page 35: Functional web with clojure

Compojure - defining routes

Page 36: Functional web with clojure

Hiccup - html from Clojure

Page 37: Functional web with clojure

Ring - embedded server & dependencies

Page 38: Functional web with clojure

ClojureScript - Functional WebImmutability & composable functions for Web Apps

Page 39: Functional web with clojure
Page 40: Functional web with clojure
Page 41: Functional web with clojure

jQuery over all JavaScript frameworks

Page 42: Functional web with clojure
Page 43: Functional web with clojure

Google Closure tools & libraries

Page 44: Functional web with clojure
Page 45: Functional web with clojure

ClojureScript

Just add the dependencies & include the generated JavaScript

Page 46: Functional web with clojure
Page 47: Functional web with clojure
Page 48: Functional web with clojure

ClojureScript DemoInteractive development

Page 49: Functional web with clojure

Figwheel (flappy birds example)

Page 50: Functional web with clojure

Tools to learn Clojureinspire them & build up their motivation

Page 51: Functional web with clojure

Clojure support in many different tools

Page 52: Functional web with clojure

Leiningen - Clojure powered build automation

Page 53: Functional web with clojure
Page 54: Functional web with clojure

LightTable - Instarepl

Page 55: Functional web with clojure

Emacs & Spacemacs

Page 56: Functional web with clojure

Figwheel (flappy birds example)

Page 57: Functional web with clojure
Page 58: Functional web with clojure

Examples, examples, exampleswe learn by example...

Page 59: Functional web with clojure

Over 20 Books on Clojure...

Where to start with Clojure will be different...

Example:

I typically suggested BraveClojure.com as a starting point, however many people prefer LivingClojure or ClojureScript Unraveled...

Help people understand the relevance of a book and if it's the right thing for them at that time.

Page 60: Functional web with clojure

Clojure.org & ClojureDocs.org

Page 61: Functional web with clojure
Page 62: Functional web with clojure

Github

Page 63: Functional web with clojure

Clojure-through-code Git repository

Page 64: Functional web with clojure

http://practical.li/clojure-webapps

Page 65: Functional web with clojure
Page 66: Functional web with clojure

Testing your Clojure skills...

Page 67: Functional web with clojure
Page 68: Functional web with clojure

Clojurian Community in Person

Probably the most active language-specific developer communities in London

Page 69: Functional web with clojure

Learning by teaching others

I really started thinking in Clojure when I started talking to & teaching others

- Coding dojos- talks on Clojure (starting with the basics, showing the art of the possible)- moving on to running conferences- workshops at hack days

Page 70: Functional web with clojure

Overtone live performance - MetaX

Page 71: Functional web with clojure

Overtone live performance - MetaX

Page 72: Functional web with clojure

Take your own journey into Clojure