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

Introduction to Clojure

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Introduction to Clojure

Avatar for Pablo Tamarit

Pablo Tamarit

October 30, 2012
Tweet

More Decks by Pablo Tamarit

Other Decks in Programming

Transcript

  1. ? 2000 2001 2002 2003 2004 2005 2006 2007 2008

    2009 2010 2011 2012 1999 - 2005 EIG 2007 - 2049 CERN
  2. (def basket [{:name "apples", :type :fruits} {:name "pears", :type :fruits}

    {:name "scoubidous", :type :objects}]) (group-by :type basket) {:fruits [{:type :fruits, :name "apples"} {:type :fruits, :name "pears"}], :objects [{:type :objects, :name "scoubidous"}]} Sampling 
  3. Clojure • Created in 2007 • Simplicity • Not object

    oriented! • Dynamic typing • Everything is immutable • No state • No side effects • Dialect of Lisp! + λ
  4. Lisp • Invented in 1958 • Lots of Irritating Superfluous

    Parentheses • Homoiconicity • Macros paulgraham.com/avg.html
  5. Language • Simple syntax, rich vocabulary. – Syntax: data, functions

    – Vocabulary: functions clojure.org/cheatsheet
  6. Syntax: collections List ("one" 2 3 3) Vector ["one" 2

    3 3] Set #{"one" 2 3} Map {:name "Pablo", :age 28} (:age {:name "Pablo", :age 28})  28
  7. Syntax: functions Call (+ 1 2 3) Definition (defn abs

    [n] (if (neg? n) (- n) n)) Anonymous definition (fn [x y] (+ (abs x) (abs y))) Sugared anonymous definition #(+ (abs %1) (abs %2)) condition then else vector list fn args
  8. Vocabulary: functions (def coll [-42 5 -0.1]) (map abs coll)

     (42 5 0.1) (reduce + 0 coll)  -37.1 (filter neg? coll)  (-42 -0.1)
  9. Interop Java Clojure new JFrame("Titre") (JFrame. "Titre") Color.RED Color/RED frame.setTitle("Titre2")

    (.setTitle frame "Titre2") frame.getTitle().toLowerCase() (.. frame getTitle toLowerCase) frame.add(panel) frame.pack() frame.setVisible(true) (doto frame (.add panel) (.pack) (.setVisible true)) 2 4 1 3
  10. What to install? • Nothing! <dependency> <groupId>org.clojure</groupId> <artifactId>clojure</artifactId> <version>1.4.0</version> </dependency>

    • What’s in the JAR? – The library – REPL: java -jar clojure.jar – Compiler: .clj  .class
  11. Tools • Build – leiningen – clojure-maven-plugin • IDE –

    Counterclockwise (Eclipse) – La Clojure (IntelliJ IDEA) – Emacs!
  12. Resources • Presentations: – blip.tv/clojure – infoq.com/clojure • Books: –

    clojurebook.com – joyofclojure.com • Exercises: – 4clojure.com
  13. Image credits • Clojure logo: © Tom Hickey (tomhickey.com) •

    Aerial view of the LHC: © 2001 CERN (cdsweb.cern.ch/record/42370) • The Treachery of Images: © 1929 René Magritte • Duke models: © Sun Microsystems Inc. (duke.kenai.com/models) • Portrait of John McCarthy: © 2006 null0 (flic.kr/p/q39Sg) • The Call of the Peaks: © 1942 René Magritte • Clairvoyance: © 1936 René Magritte • Cooled Beans: © 2008 Chris Bay (flic.kr/p/4z6CBA) • Gears: © Dima Yagnyuk (thenounproject.com/noun/gears/#icon- No2174, thenounproject.com/Deadtype) • Tools: © Jacob Hardbower (thenounproject.com/noun/tools/#icon- No4684, thenounproject.com/HeartWired)