Skip to content

Slides from my Railscamp Germany 2014 talk "Alien Technology: Ideas from the Clojureverse - A Breakneck Journey through Functional Programming, LISP, Clojure(Script), Om, and React."

Notifications You must be signed in to change notification settings

paulkoegel/railscamp-2014-alien-technology

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Alien Technology: Ideas from the Clojureverse

A Breakneck Journey through Functional Programming, LISP, Clojure(Script), Om, and React.

Slides from Railscamp Germany 2014.

View the presentation here: https://dl.dropboxusercontent.com/u/15321563/railscamp-2014-presentation/index.html
Press p to switch to presenter mode and see my notes.

Here are the slides in markdown:


images/rug.jpg


Paul Wittmann

  • Background: philosophy & English literature
  • Rails, JavaScript at Railslove by day
  • Increasingly more Clojure at night

Goal

Whet your appetite!


Alien Technology

alien technology

Too-good-to-be-true but also foreign

“Anyone could learn LISP in one day, except that if they already knew Fortran, it would take three days.” — Marvin Minsky


Proviso

  • introducing several new ideas at once
  • not a tutorial, very little code
  • aiming for breadth over in-depth

ToC v1:

  • Clojure (FP, LISP)
  • Om (React, ClojureScript)

For the purposes of this talk: Clojure ~= ClojureScript

(unlike Java - JavaScript)


Motivation to learn Clojure

  • modern LISP, good for FP]
  • Rich Hickey's "Simplicity Matters"
  • learn Clojure to gain better access to Rich's thinking
  • great community

Why Om for a beginners talk?

  • "JavaScript MVC" in ClojureScript
  • first steps in the browser
  • tangible results in a familiar environment

-> I built a card game called "Omingard"


dramatis personae

Photos of David Nolen and Rich Hickey © and courtesy of Mike Bridge. Sources: David, Rich.

The men behind this

LISP: 1958; Fortran 1956 List Processing

Clojure: like Ruby, a general-purpose programming language, 2007

Om: ClojureScript interface for React; "JavaScript MVC"; last December


Here are four core features of Clojure

Boo when you can't imagine living with that.

Cheers also welcome.


1. Clojure is dynamic -
there are no types.


2. Clojure is functional -
there are no objects.


3. Clojure runs on the JVM -
it's a compiled language.


4. Clojure is a LISP -
has weird syntax.

(+ 1 1)


elegant weapons


LISP: Code as data / Homoiconicity

“Lisp is a programmable programming language.” — John Foderaro

  • Ruby: 3 * 4 + 7

  • Clojure: (+ 7 (* 3 4))

  • homoiconicity: code has same structure as the abstract syntax tree

  • in Ruby this isn't the case; interpreter has to know about operator precedence and so do you


(+ 1 1)
  • Code is data: the code above is a list
  • When writing macros, code can be manipulated like any other data - you can call functions like map on it etc.

Clojure is pragmatic

  • pragmatic versus academic (Haskell)
  • dynamic
  • impurely functional (you can mutate special reference types - e.g. atoms)
  • runs on:
    • the JVM (Clojure)
    • JavaScript engines (ClojureScript)

Two big ideas

  • LISP

  • embraces immutability & values

    • good for concurrency
  • immutability is widely seen as the more important one!


Clojure compiler

  • is written in Java
  • compiles Clojure code to Java bytecode

ClojureScript Compiler]

  • compiles a subset of Clojure code to JavaScript
  • subset: single thread, can't do some Clojure stuff
  • we treat them as one language here targeting two platforms - Java doesn't run everywhere after all :)
  • has Clojure semantics
  • Clojure compilation contains more than CoffeeScript compilation, since it remodels Clojure data structures in JavaScript

ToC v2: Ideas

  • simplicity (Clojure)
  • trees / immutable persistent data structures (Clojure)

Om

  • React's virtual DOM
  • power of immutable data structures
  • global app state
  • (core.async) - not covered :(

Clojure: Simplicity

"We can make the same exact software we are making today with dramatically simpler stuff. Radically simpler than Ruby, which seems really simple. Why aren't we?"

Rich Hickey. 2012. "Simplicity matters".

If there's one thing to take away from this talk then it's to watch this talk.


Simple (the good one)

"Simple is the opposite of complex. A thing is simple if it has no interleaving, if it has one purpose, one concept, one dimension, one task.
Being simple does not imply one instance or one operation: it's about interleaving, not cardinality. Importantly, this means that simplicity is objective."

Rich Hickey. "Simple made easy".


Easy (potentially a false friend)

Easy is the opposite of hard, or difficult. A thing is easy if it's near to hand, if it's easy to get at (location), if it's near to our understanding (familiarity) [...] This means that ease is relative.

Rich Hickey. "Simple made easy".


Example

"Speaking English is dead easy for me, but that doesn't mean that speaking English is intrinsically simple. I find French quite difficult. Little French children speak French all the time, [...] It's easy for them, it lies near to them."

Rich Hickey. "Simple made easy".


Summary

Simple

  • opposite of complex (braided, interwoven)
  • about one thing
  • objective

Easy

  • opposite of hard
  • relative
  • easy for me -vs- easy for you

Big Danger

  • mistake easy for simple things

  • gem install hairball

  • German: einfach (simple) -vs- leicht (easy)


more_ancestors_than_jesus


Complex -vs- Simple

  • syntax - data
  • state, objects - values
  • ORM - unadorned data

"Simplicity Matters"
(http://youtu.be/rI8tNMsozo0?t=21m41s)


Immutability

  • functions never modify their arguments - but return copies

(clojure.string/upper-case "MiXeD cAsE")


And in Ruby

  • Ruby mutable hashes, arrays, strings etc.
x = "MiXeD cAsE"

x.upcase!
=> "MIXED CASE"

x
=> "MIXED CASE" # (same object_id)
  • thank God there's normal upcase as well, so mutation is not the default, but it's possible. And we mutate arrays or hashes all over the place.

  • Clojure: immutable. Easier-to-reason-about applications.

  • (atoms: mutable, but the REF - not the function/method decides whether mutation takes place)


Example

(def my_vec '( 1 2 3 4 ))
=> #'user/my_vec

(conj (pop my_vec) 11) ;; remove first el, add 11 to front
=> (11 2 3 4)

(-> my_vec ;; threading macro
    pop
    (conj 11))
=> (11 2 3 4)

my_vec
=> (1 2 3 4)

linked list

  • Works like git, only small diffes are kept to create second vector.
  • important for Om

break necks

Now the breakneck part.

  • Buckle your seatbelt.
  • There will be a lot more details on this in the upcoming blog post!

Facebook's React

  • "JavaScript MVC"

Killer Feature: Virtual DOM

  • DOOM 3 rendering engine

Virtual DOM (VDOM)

  • normal JS-MVC:
    data -> templates -> DOM

  • React: data -> templates -> virtual DOM -> DOM

  • data: models, JSON

  • templates: handlebars etc.


Bottleneck: DOM updates

  • VDOM is a plain JS object
  1. data changes, templates get rerendered
  2. new VDOM gets DIFFed with old one
  3. minimal set of actual DOM updates calculated

Backbone

  • change a user's name that's displayed in several Views on the page: all these views get rerendered entirely

React

  • if any part of our app's data changes, we can just rerender EVERYTHING.

  • frameworks like Ember do a lot for you behind the scenes to remain performant and make your life easier (remember simplicity!?) - React is just way simpler than that.


Om

  • Om benefits from ClojureScript's immutable data structures

  • even faster diffing

  • faster diffing: only reference equality checks -vs- traversing the entire JSON structure.


Om

  • global app state
  • undo in 13 lines of code
  • not a memory problem b/c our persistent data structures use structural sharing.

Slides:

http://tinyurl.com/clojureverse


More Ideas


Links:

ClojureScript:


CSP & core.async:

About

Slides from my Railscamp Germany 2014 talk "Alien Technology: Ideas from the Clojureverse - A Breakneck Journey through Functional Programming, LISP, Clojure(Script), Om, and React."

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages