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

Joy of Clojure

Joy of Clojure

A very brief introduction to Clojure delivered at Barcamp Shanghai on March 3, 2012.

Avatar for John Biesnecker

John Biesnecker

March 02, 2012
Tweet

More Decks by John Biesnecker

Other Decks in Programming

Transcript

  1. Quick show of hands. Clojure junkies? LISP hackers? Functional Programming

    aficionados? Java nerds? How many of you are ...
  2. (defmacro unless [condition & body] `(if (not ~condition) (do ~@body)))

    ;; (unless (= true false) ;; (println “You don’t understand booleans, do you?”))
  3. ;; sum of multiples of 27 under one million (reduce

    + (filter #(zero? (mod % 27)) (range 0 1e6))) ;; 18518981481
  4. ;; functions are first-class object (defn increment-by-three [x] (+ x

    3)) (defn contrived-example [avector afunction] (map afunction avector) ;; (contrived-example [1 2 3] increment-by-three) ;; => (4 5 6)
  5. ;; anonymous functions are easy (defn contrived-example [avector afunction] (map

    afunction avector) ;; (contrived-example [1 2 3] #(+ % 3)) ;; => (4 5 6)
  6. ;; writing a high-performance thread-safe cache is hard (import ‘com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)

    (import ‘com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap$Builder) (-> (ConcurrentLinkedHashMap$Builder.) (.maximumWeightedCapacity 1000) (.build)) ;; thanks, Google ;-)