Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Clojure

Balint Erdi
January 29, 2013

Intro to Clojure

Brief introduction to Clojure presented at the Budapest Clojure User Group.

Balint Erdi

January 29, 2013
Tweet

More Decks by Balint Erdi

Other Decks in Technology

Transcript

  1. Clojure is a dynamic programming language that targets the JVM

    and is a dialect of Lisp with a strong focus on concurrency.
  2. Clojure is a dynamic programming language that targets the JVM

    and is a dialect of Lisp with a strong focus on concurrency.
  3. Rich Hickey (RH): Why did I write yet another programming

    language? Basically because I wanted: • A Lisp • for Functional Programming • symbiotic with an established platform • designed for concurrency • ... and couldn’t find one
  4. History • Rich Hickey sends an email to Common Lisp

    friends in 2007 • After working on it for 2.5 years • Gets picked up, now at 1.4 (Apr 2012) • Targets other platforms, too (Javascript, CLR, Python)
  5. Rationale (http://clojure.org/rationale) • Lisp is a good thing • Functional

    programming is a good thing • Languages and platforms • Object Orientation is overrated • Polymorphism is a good thing • Concurrency and the multi-core future
  6. Atomic types • Number (3, 3.14, 1e6) • Character (\a,

    \b, \c) • String (“foo”) • Symbol (‘a, ‘b, ‘c) • Keywords • for very fast equality test (:cool, :bad) • Regex (#”clo[s|j]ure”) • Boolean (true, false) • Only nil and false are falsey
  7. Collections • A handful (list, vector, map, set) • Sequential,

    counted, associative • A large number of functions that operate on them • All of them are immutable • Extensible: deftype, defrecord, reify
  8. Vectors • Like an array • Fast random access by

    index • Sequential, counted and associative (by index) • [:a :b :a :c :c :d] • or (vector :a :b :a :c :c :d)
  9. Maps • Hash, dictionary, “associative” array • Fast access by

    key • Counted and associative, not sequential • {“bob” 35, “alice” 28, “charlie” 19} • (hash-map “bob” 35, “alice” 28, “charlie” 19)
  10. Sets • Distinct collection of elements • Fast lookup for

    containment • Counted, not sequential, not associative • #{“alice” “bob” “charlie”} • (set '("alice" "bob" "charlie"))
  11. Lists • First and rest (car and cdr in Lisp)

    • Counted and sequential, not associative • Fast access to first and rest • ‘(:apple :banana :cherry) • (list :apple :banana :cherry)
  12. Functions • First class citizens (no surprise there) • Defined

    by (defn fn-name [...] body) • Anonymous functions • (fn [...] body) • (map #(+ % 2) collection)
  13. Laziness • Only gets evaluated when the result is needed

    • Makes for elegant, readable programming • Enables working with infinite sequences • Most functions are lazy (map, filter, for, take, drop, partition, etc.) • Use eager functions if you need side effects
  14. Prefix notation • Function is always the first argument •

    Needs some getting used to • Makes variable-length arg functions simple • e.g (+ 1 -7 3 15) • Operator precedence is done away with
  15. Java interop • The whole Java ecosystem at your fingertips

    • A few succinct forms to reach them (. .. new doto) • Enterprise-ready :)
  16. Concurrency • Data structures are immutable • Mechanisms • Refs/Transactions

    (STM) • Atoms (thread isolation) • Agents (independent/asynchronous change)