Skip to content

Instantly share code, notes, and snippets.

@iantruslove
Last active April 21, 2016 21:50
Show Gist options
  • Save iantruslove/32d0136acc0406d7af44 to your computer and use it in GitHub Desktop.
Save iantruslove/32d0136acc0406d7af44 to your computer and use it in GitHub Desktop.
Clojure intro talk outline

A gentle introduction to Clojure

Lisp Cycles (XKCD 297 / https://xkcd.com/297/)

Ian Truslove

NoCo Web Scale

2015-12-01


A gentle introduction to Clojure

"Lisp is over half a century old and it still has this perfect timeless air about it". Clojure is a Lisp. It's a dynamic programming language that targets the JVM and JavaScript.

It's also awesome.

In this talk I'll give you a really quick dive into Clojure programming, show you some of the most important language features and tools, and hopefully inspire you to dig a little deeper yourself.


Intro


"Snippets" project: build a web service for sharing text snippets

(gist.github.com anyone?)

Interface:

$ curl -X POST \
    -H "content-type: text/plain" \
    -d "Hello, world!" \
    http://server/snippets/
# Should return HTTP 302 with a Location header for us to follow

$ curl -X GET http://server/snippets/1234
Hello, world!

About Clojure

  • It's (primarily) functional
  • It's a Lisp
    • It's not John McCarthy's Lisp
  • Dynamic
  • Things you can do in Clojure / things inspired by Clojure
    • Distributed systems and web services
    • Apache Storm
    • Om - react.js but faster
    • Immutable.js
    • Overtone

Functional Programming


Leiningen


The REPL

  • Read Evaluate Print Loop

Editors

  • A wealth of choices:
  • Must-have features:
    • nREPL integration
    • Syntax highlighting
    • Auto paren matching
    • Structural navigation and editing

The first hurdle

(Source: http://www.thejach.com/view/2011/8/short_guide_to_lisp_syntax)


Hurdle? A curb, surely no more than that...

(Source: http://emacslife.com/how-to-read-emacs-lisp.html)

My advice: read from the inside out.


Data and basic operations

  • Numbers, strings, keywords
  • def and immutability

Collections and basic operations

  • Lists, vectors, maps, sets
  • seq ops
    • first, rest
    • conj
    • for, doseq
    • recursion
  • map ops
    • assoc, dissoc
  • map, filter
  • reduce

Immutable data

  • Data don't change
  • Structural sharing used for efficiency

(source: http://debasishg.blogspot.com/2010/05/grokking-functional-data-structures.html)


Snippets: Intro to Ring

  • Ring is a simple (conceptual) framework for building web applications.
    • It also has some useful code.
  • Request maps:
{:server-port 8080
 :server-name "localhost"
 :uri "/foo/bar/baz"
 :scheme :https
 :headers {"content-type" "text/plain"
 :body <some Java stream object>}
  • Response maps:
{:status 200
 :headers ["content-type" "text/plain"]
 :body "Hello, world!"}

Functions

  • fn
  • defn
  • Higher order functions
    • e.g. map takes a function as its second argument

Common Code Structures

  • Threading macros: -> and ->>
  • let - local "variables"
  • if, when, cond
  • Destructuring

Infinite computation

  • Lazy seqs
  • Recursion with (explicit) tail call optimization
    • Example: max of a list of numbers

Organisation

  • Namespaces, vars
  • require to make other code available for use.

Snippets: Ring Basics

  • Creating and modifying responses
    • The functions in ring.util.response are useful

Snippets: Ring Handlers

  • Straightforward requirements:
    • Take a single parameter (request map)
    • Return a response map
(fn [req]
  {:status 200 :body ""})  
  • Sketch out the GET and POST handlers...

Defining "things"

  • plain maps
  • deftype
  • defrecord

Polymorphism

  • Multimethods
    • user-defined dispatch - typically on values
  • Protocols
    • Type-based dispatch
    • Can extend (closed) Java classes to participate

Unit testing


Immutability By Default

  • Design choices making state and concurrency easy to reason about pervade the language.
  • Persistent data structures
    • "Persistent" as in it always preserves previous versions of itself, not as in a database.
  • STM (Software Transactional Memory)

Snippets: Storing snippets in an atom

  • reset! and swap!

Concurrency and Parallelism

  • Lots of choices:
    • Threads
    • Futures
    • Promises
    • Agents
    • pmap (parallel map)
    • Reducers
    • JVM platform options, e.g. executors
    • core.async

Interop

  • Platform interop for Java and JavaScript

Snippets: UUIDs

  • We need them, Java has them.

Snippets: Put it together

  • Write the rest of the code!

Snippets: Simple Extensions

  • Web pages
  • URL shortening
  • Durable datastore
  • Automatic content conversion (if possible)
    • e.g. JSON <--> EDN, JSON --> HTML, ...

Resources

Websites

Books


More Resources

Other

Conferences

  • Clojure/conj
  • Clojure/west
  • Strange Loop

End

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment