Your SlideShare is downloading. ×
Clojure and Modularity
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Clojure and Modularity

1,192
views

Published on

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,192
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Clojure and Modularity Philly Lambda Tuesday, July 21, 2009 Stuart Sierra http://stuartsierra.com/
  • 2. www.altlaw.org
  • 3. Clojure's Four Elements List (print :hello "Philly") Vector [:eat "Pie" 3.14159] Map {:lisp 1 "The Rest" 0} Set #{2 1 3 5 "Eureka"}
  • 4. defn (defn greet [name] (println "Hello," name)) (defn average [& args] (/ (reduce + args) (count args)))
  • 5. Data are Functions ({:f "foo" :b "bar"} :f) "foo" (:key {:key "value", :x "y"}) "value" ([:a :b :c] 2) :c (#{1 5 3} 3) true
  • 6. defmacro (defmacro when [test & body] (list 'if test (cons 'do body)))
  • 7. Java (import '(com.example.package MyClass YourClass)) (.method object argument) (MyClass/staticMethod argument) (MyClass. argument)
  • 8. synchronous asynchronous coordinated ref independent atom agent unshared var
  • 9. Vars (def life 42) (defn meaning [] (println life)) (meaning) 42 (binding [life 37] (let [life 37] (println life) (meaning)) (meaning)) 37 37 42
  • 10. Refs (def c (ref 100)) (deref c) 100 (dosync (alter c inc)) (deref c) 101
  • 11. Agents (def fib (agent [1 1 2])) (deref fib) [1 1 2] (send fib conj 3 5) returns immediately! later on... (deref fib) [1 1 2 3 5]
  • 12. clojure.contrib.http.agent (http-agent "http://www.altlaw.org/" :on-success (fn [a] (println (response-body-str a))))
  • 13. Multimethods (defmulti copy (fn [in out] [(class in) (class out)]))
  • 14. Multimethods (defmethod copy [InputStream OutputStream] .. (defmethod copy [InputStream Writer] ... (defmethod copy [InputStream File] ... (defmethod copy [Reader OutputStream] ... (defmethod copy [Reader Writer] ... (defmethod copy [Reader File] ... (defmethod copy [File OutputStream] ... (defmethod copy [File Writer] ... (defmethod copy [File File] ...
  • 15. Namespaces (ns my.cool.project (:require [clojure.contrib.math :as math]) (:import (java.math BigDecimal))) (defn lower-median [x y] (math/floor (/ x y))
  • 16. Modularity and Dependency Management ● CPAN ● OSGi ● Python Eggs ● Maven ● Rubygems ● Ivy ● PEAR (PHP)
  • 17. Modularity Your program Library X Library Y Library Z, Library Z, version 1 version 2
  • 18. "JAR hell" Your program x.jar y.jar Library X Library Y Library Z, Library Z, version 1 version 2
  • 19. CPAN ● System-wide or per-user library installation ● User manages libraries ● One repository, many mirrors ● Multiple versions of a lib may be installed; each process may only use one version ● Integrated docs, tests, & bug tracker
  • 20. Rubygems ● System-wide or per-user library installation ● User manages libraries ● Multiple repositories, names may conflict ● Multiple versions of a lib may be installed; each process may only use one version ● Docs, tests, and bug tracking not integrated
  • 21. ASDF ● System-wide or per-user library installation ● User manages libraries ● Wiki page acts as the repository! ● No integrated docs/tests/bug-tracking ● Does not support multiple versions of the same lib
  • 22. Maven / Ivy ● Per-project library installation ● Build system manages libraries; user manages private repository ● Multiple public repositories ● Optional integration with docs/tests ● Permits multiple versions of a same lib, must be handled by a framework
  • 23. OSGi ● Java EE, Glassfish, Eclipse ● Bundle: JAR file + extra manifest headers ● Each Bundle gets its own ClassLoader ● Multiple, nested ClassLoader contexts within a single JVM
  • 24. More ● http://clojure.org/ ● Google Groups: Clojure ● #clojure on irc.freenode.net ● http://github.com/richhickey/clojure-contrib ● http://stuartsierra.com/ ● http://github.com/stuartsierra ● http://www.altlaw.org/