"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.
- My background
- It's gonna be quick in places - stop me at any time and ask for clarifications.
- Links (also at end):
- Repo: github.com/iantruslove/gentle-intro
- These slides: git.io/vB5Ov
(gist.github.com anyone?)
$ 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!
- 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
- Think about verbs not nouns
- See Steve Yegge's Execution in the Kingdom of Nouns
- Data "shape" is important
- Functions are first class
- The de-facto build tool - http://leiningen.org/
- but not the only build tool - Boot
- It's for creating and managing projects
- Read Evaluate Print Loop
- A wealth of choices:
- Emacs + clojure-mode + Cider
- Vim + Fireplace + Cider
- IntelliJ + Cursive
- Nightcode
- (?) Light Table
- Must-have features:
- nREPL integration
- Syntax highlighting
- Auto paren matching
- Structural navigation and editing
(Source: http://www.thejach.com/view/2011/8/short_guide_to_lisp_syntax)
(Source: http://emacslife.com/how-to-read-emacs-lisp.html)
- Numbers, strings, keywords
def
and immutability
- Lists, vectors, maps, sets
- seq ops
first
,rest
conj
for
,doseq
- recursion
- map ops
assoc
,dissoc
map
,filter
reduce
- Data don't change
- Structural sharing used for efficiency
(source: http://debasishg.blogspot.com/2010/05/grokking-functional-data-structures.html)
- 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!"}
fn
defn
- Higher order functions
- e.g.
map
takes a function as its second argument
- e.g.
- Threading macros:
->
and->>
let
- local "variables"if
,when
,cond
- Destructuring
- Lazy seqs
- Recursion with (explicit) tail call optimization
- Example: max of a list of numbers
- Namespaces, vars
require
to make other code available for use.
- Creating and modifying responses
- The functions in
ring.util.response
are useful
- The functions in
- Straightforward requirements:
- Take a single parameter (request map)
- Return a response map
(fn [req]
{:status 200 :body ""})
- Sketch out the GET and POST handlers...
- ...in a Compojure
defroutes
setup
- ...in a Compojure
- plain maps
deftype
defrecord
- Multimethods
- user-defined dispatch - typically on values
- Protocols
- Type-based dispatch
- Can extend (closed) Java classes to participate
- clojure.test is the standard.
- some others:
- 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)
reset!
andswap!
- Lots of choices:
- Threads
- Futures
- Promises
- Agents
- pmap (parallel map)
- Reducers
- JVM platform options, e.g. executors
- core.async
- Platform interop for Java and JavaScript
- We need them, Java has them.
- Write the rest of the code!
- Web pages
- URL shortening
- Durable datastore
- Automatic content conversion (if possible)
- e.g. JSON <--> EDN, JSON --> HTML, ...
- Clojure for the Brave and True
- now a book too
- Parens of the Dead
- Slack - get access via clojurians.net
- IRC - on Freenode, #clojure, #clojure-beginners, #clojurescript, #clojure-emacs, ...
- Clojure Google Group
- Meetup groups:
- Denver Den of Clojure
- Boulder Clojurians
- Clojure/conj
- Clojure/west
- Strange Loop
- Links
- Repo: github.com/iantruslove/gentle-intro
- These slides: git.io/vB5Ov
- Gist for slides: http://git.io/vB5Ya
- Me: