introduction clojure - geneva jug - octobre 2012

22
Introduction à Clojure Pablo Tamarit 30 octobre 2012 Geneva JUG

Upload: pablo-tamarit

Post on 17-May-2015

1.041 views

Category:

Technology


0 download

DESCRIPTION

Présentation de 15 minutes au Java User Group de Genève.

TRANSCRIPT

Page 1: Introduction Clojure - Geneva JUG - Octobre 2012

Introduction àClojure

Pablo Tamarit30 octobre 2012

Geneva JUG

Page 2: Introduction Clojure - Geneva JUG - Octobre 2012

?2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012

1999 - 2005EIG

2007 - 2049CERN

N N

Page 3: Introduction Clojure - Geneva JUG - Octobre 2012

(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"}]}

Avant-goût

Page 4: Introduction Clojure - Geneva JUG - Octobre 2012

• Partie philosophique

• Partie théorique

• Partie pratique

Page 5: Introduction Clojure - Geneva JUG - Octobre 2012

Partie philosophique

Page 6: Introduction Clojure - Geneva JUG - Octobre 2012

Clojure

• Créé en 2007• Simplicité• Pas d’orienté

objet!• Typage dynamique

• Tout est immutable• Pas d'état• Pas d'effets de

bord• Dialecte de Lisp!

+ λ

Page 7: Introduction Clojure - Geneva JUG - Octobre 2012

Lisp

• Inventé en 1958

Page 8: Introduction Clojure - Geneva JUG - Octobre 2012

John McCarthy1927 – 2011

Page 9: Introduction Clojure - Geneva JUG - Octobre 2012

Lisp

• Inventé en 1958• Lots ofIrritatingSuperfluousParentheses?

Page 10: Introduction Clojure - Geneva JUG - Octobre 2012

Lisp

• Inventé en 1958• Lots ofIrritatingSuperfluousParentheses

• Homoiconicité• Macros

paulgraham.com/avg.html

Page 11: Introduction Clojure - Geneva JUG - Octobre 2012

Partie théorique

Page 12: Introduction Clojure - Geneva JUG - Octobre 2012

Langage

• Syntaxe simple, vocabulaire riche.

– Syntaxe: données, fonctions

– Vocabulaire: fonctions

clojure.org/cheatsheet

Page 13: Introduction Clojure - Geneva JUG - Octobre 2012

Syntaxe: collectionsListe ("un" 2 3 3)

Vecteur ["un" 2 3 3]

Set #{"un" 2 3}

Map {:nom "Pablo", :age 28}

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

28

Page 14: Introduction Clojure - Geneva JUG - Octobre 2012

Syntaxe: fonctionsAppel (+ 1 2 3)

Définition (defn abs [n] (if (neg? n) (- n) n))

Définition anonyme (fn [x y] (+ (abs x) (abs y)))

Définition anonyme sucrée #(+ (abs %1) (abs %2))

conditionthenelse

vecteurliste fn args

Page 15: Introduction Clojure - Geneva JUG - Octobre 2012

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)

Page 16: Introduction Clojure - Geneva JUG - Octobre 2012

InteropJava 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))

24

13

Page 17: Introduction Clojure - Geneva JUG - Octobre 2012

Partie pratique

Page 18: Introduction Clojure - Geneva JUG - Octobre 2012

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

Page 19: Introduction Clojure - Geneva JUG - Octobre 2012

Outils

• Build– leiningen– clojure-maven-plugin

• IDE– Counterclockwise (Eclipse)– La Clojure (IntelliJ IDEA)– Emacs!

Page 20: Introduction Clojure - Geneva JUG - Octobre 2012

Apprentissage

• Présentations:– blip.tv/clojure– infoq.com/clojure

• Livres:– clojurebook.com– joyofclojure.com

• Exercices:– 4clojure.com

Page 21: Introduction Clojure - Geneva JUG - Octobre 2012

Questions?

Page 22: Introduction Clojure - Geneva JUG - Octobre 2012

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)