Introduction à
    Clojure




  Pablo Tamarit
 30 octobre 2012
   Geneva JUG
1999 - 2005                                       2007 - 2049
                 EIG                                               CERN
2000   2001   2002   2003   2004   2005   2006   2007   2008   2009   2010   2011   2012




                                          ?
Avant-goût
 (def panier [{:nom "pommes",     :type :fruits}
              {:nom "poires",     :type :fruits}
              {:nom "scoubidous", :type :objets}])



 (group-by :type panier)



 {:fruits [{:type :fruits, :nom "pommes"}
           {:type :fruits, :nom "poires"}],
  :objets [{:type :objets, :nom "scoubidous"}]}
• Partie philosophique

• Partie théorique

• Partie pratique
Partie philosophique
Clojure

                                + λ
•   Créé en 2007           •   Tout est immutable
•   Simplicité             •   Pas d'état
•   Pas d’orienté objet!   •   Pas d'effets de bord
•   Typage dynamique       •   Dialecte de Lisp!
Lisp
• Inventé en 1958
John McCarthy
  1927 – 2011
Lisp
• Inventé en 1958
• Lots of
  Irritating
  Superfluous
  Parentheses?
Lisp
• Inventé en 1958     • Homoiconicité
• Lots of             • Macros
  Irritating
  Superfluous
  Parentheses


        paulgraham.com/avg.html
Partie théorique
Langage

• Syntaxe simple, vocabulaire riche.

  – Syntaxe: données, fonctions

  – Vocabulaire: fonctions

                             clojure.org/cheatsheet
Syntaxe: collections
Liste                    ("un" 2 3 3)
Vecteur                  ["un" 2 3 3]
Set                      #{"un" 2 3}
Map                      {:nom "Pablo", :age 28}




          (:age {:nom "Pablo", :age 28})

                       28
Syntaxe: fonctions
                             fn           args
                     liste                                   vecteur
Appel                             (+ 1 2 3)
Définition                        (defn abs [n]
                                    (if (neg? n)           condition
                                      (- n)                then
                                      n))                  else
Définition anonyme                (fn [x y] (+ (abs x) (abs y)))
Définition anonyme sucrée         #(+ (abs %1) (abs %2))
Vocabulaire: fonctions
(def coll [-42 5 -0.1])

(map abs coll)
 (42 5 0.1)

(reduce + 0 coll)
 -37.1

(filter neg? coll)
 (-42 -0.1)
Interop
                   Java                            Clojure
    new JFrame("Titre")              (JFrame. "Titre")
    Color.RED                        Color/RED
    frame.setTitle("Titre2")         (.setTitle frame "Titre2")
4   frame.getTitle().toLowerCase()   (.. frame getTitle toLowerCase)   2
3   frame.add(panel)                 (doto frame                       1
    frame.pack()                       (.add panel)
    frame.setVisible(true)             (.pack)
                                       (.setVisible true))
Partie pratique
Quoi installer?
• Rien!
  <dependency>
    <groupId>org.clojure</groupId>
    <artifactId>clojure</artifactId>
    <version>1.4.0</version>
  </dependency>

• Qui a-t-il dans le JAR?
  – La librairie
  – REPL: java -jar clojure.jar
  – Compilateur: .clj  .class
Outils
• Build
  – leiningen
  – clojure-maven-plugin


• IDE
  – Counterclockwise (Eclipse)
  – La Clojure (IntelliJ IDEA)
  – Emacs!
Apprentissage
• Présentations:
  – blip.tv/clojure
  – infoq.com/clojure

• Livres:
  – clojurebook.com
  – joyofclojure.com

• Exercices:
  – 4clojure.com
Questions?
Crédits graphiques
• Logo Clojure: © Tom Hickey (tomhickey.com)
• Vue aérienne du LHC: © 2001 CERN
  (cdsweb.cern.ch/record/42370)
• La Trahison des images: © 1929 René Magritte
• Duke models: © Sun Microsystems Inc. (duke.kenai.com/models)
• Portrait de John McCarthy: © 2006 null0 (flic.kr/p/q39Sg)
• L'Appel des cimes: © 1942 René Magritte
• La 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)

Introduction Clojure - Geneva JUG - Octobre 2012

  • 1.
    Introduction à Clojure Pablo Tamarit 30 octobre 2012 Geneva JUG
  • 2.
    1999 - 2005 2007 - 2049 EIG CERN 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 ?
  • 3.
    Avant-goût (def panier[{:nom "pommes", :type :fruits} {:nom "poires", :type :fruits} {:nom "scoubidous", :type :objets}]) (group-by :type panier)  {:fruits [{:type :fruits, :nom "pommes"} {:type :fruits, :nom "poires"}], :objets [{:type :objets, :nom "scoubidous"}]}
  • 4.
    • Partie philosophique •Partie théorique • Partie pratique
  • 5.
  • 6.
    Clojure + λ • Créé en 2007 • Tout est immutable • Simplicité • Pas d'état • Pas d’orienté objet! • Pas d'effets de bord • Typage dynamique • Dialecte de Lisp!
  • 7.
  • 8.
    John McCarthy 1927 – 2011
  • 9.
    Lisp • Inventé en1958 • Lots of Irritating Superfluous Parentheses?
  • 10.
    Lisp • Inventé en1958 • Homoiconicité • Lots of • Macros Irritating Superfluous Parentheses paulgraham.com/avg.html
  • 11.
  • 12.
    Langage • Syntaxe simple,vocabulaire riche. – Syntaxe: données, fonctions – Vocabulaire: fonctions clojure.org/cheatsheet
  • 13.
    Syntaxe: collections Liste ("un" 2 3 3) Vecteur ["un" 2 3 3] Set #{"un" 2 3} Map {:nom "Pablo", :age 28} (:age {:nom "Pablo", :age 28})  28
  • 14.
    Syntaxe: fonctions fn args liste vecteur Appel (+ 1 2 3) Définition (defn abs [n] (if (neg? n) condition (- n) then n)) else Définition anonyme (fn [x y] (+ (abs x) (abs y))) Définition anonyme sucrée #(+ (abs %1) (abs %2))
  • 15.
    Vocabulaire: fonctions (def coll[-42 5 -0.1]) (map abs coll)  (42 5 0.1) (reduce + 0 coll)  -37.1 (filter neg? coll)  (-42 -0.1)
  • 16.
    Interop Java Clojure new JFrame("Titre") (JFrame. "Titre") Color.RED Color/RED frame.setTitle("Titre2") (.setTitle frame "Titre2") 4 frame.getTitle().toLowerCase() (.. frame getTitle toLowerCase) 2 3 frame.add(panel) (doto frame 1 frame.pack() (.add panel) frame.setVisible(true) (.pack) (.setVisible true))
  • 17.
  • 18.
    Quoi installer? • Rien! <dependency> <groupId>org.clojure</groupId> <artifactId>clojure</artifactId> <version>1.4.0</version> </dependency> • Qui a-t-il dans le JAR? – La librairie – REPL: java -jar clojure.jar – Compilateur: .clj  .class
  • 19.
    Outils • Build – leiningen – clojure-maven-plugin • IDE – Counterclockwise (Eclipse) – La Clojure (IntelliJ IDEA) – Emacs!
  • 20.
    Apprentissage • Présentations: – blip.tv/clojure – infoq.com/clojure • Livres: – clojurebook.com – joyofclojure.com • Exercices: – 4clojure.com
  • 21.
  • 22.
    Crédits graphiques • LogoClojure: © Tom Hickey (tomhickey.com) • Vue aérienne du LHC: © 2001 CERN (cdsweb.cern.ch/record/42370) • La Trahison des images: © 1929 René Magritte • Duke models: © Sun Microsystems Inc. (duke.kenai.com/models) • Portrait de John McCarthy: © 2006 null0 (flic.kr/p/q39Sg) • L'Appel des cimes: © 1942 René Magritte • La 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)