• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Clojure Intro
 

Clojure Intro

on

  • 10,744 views

Introduction to Clojure given as a tech talk at my work

Introduction to Clojure given as a tech talk at my work

Statistics

Views

Total Views
10,744
Views on SlideShare
6,675
Embed Views
4,069

Actions

Likes
5
Downloads
123
Comments
0

14 Embeds 4,069

http://writequit.org 3967
http://feeds.feedburner.com 53
http://www.slideshare.net 22
http://translate.googleusercontent.com 7
http://planet-clojure.org 4
http://www.computersecurityarticles.info 3
http://static.slidesharecdn.com 3
http://newsblur.com 3
http://computersecurityarticles.info 2
http://theoldreader.com 1
http://www.newsblur.com 1
http://coderwall.com 1
http://webcache.googleusercontent.com 1
http://www.netvibes.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Clojure Intro Clojure Intro Presentation Transcript

  • Clojure The LISP that makes the JVM dynamic http://clojure.org
  • What is Clojure? Clojure is a dynamic, LISP-like programming language that runs on the JVM.
  • Clojure History Started in 2007 Original author (and BDFL) Rich Hickey Currently version 1.1 (1.2 expected within the next month) Available under Eclipse Public License “The Eclipse Public License is designed to be a business-friendly free software license and features weaker copyleft provisions than contemporary licenses such as the GNU General Public License (GPL).”
  • Defining features LISP syntax Macros Immutability Functional programming Concurrency Java interoperability REPL-based
  • Syntax numbers - 1234 ratios - 22/7 strings - “foo”, “bar” characters - a b c symbols - foo, bar keywords - :foo, :bar boolean - true/false null - nil
  • Syntax Lists - (1 2 3 4) Vectors - [1 2 3 4] Maps - {:a 1 :b 2 :c 3} or {:a 1, :b 2, :c 3} Sets - #{foo bar baz}
  • Syntax That’s it. There is no other syntax[1]. Data structures are code. Homoiconic All data literals stand for themselves except: Lists Symbols [1]: Technically, there are other special symbols for shortcutting syntax provided for the developer, but no other necessary syntax
  • Lists (+ 1 1) ; => 2 (+ 1 2 3) ; => 6 (+ 1 2 (+ 3 4)) ; => 10 (class “foo”) ; => java.lang.String (first [:a :b :c]) ; => :a (rest ‘(“foo” “bar” “baz”)) ; => (“bar” “baz”) The ‘ tells the reader not to evaluate the first element as a function ‘(1 2 3) is shorthand for (quote (1 2 3))
  • Hello World (ns helloworld) (defn hello “An example function” [string] (println “Hello” string)) (hello “World”) ; => “Hello World”
  • REPL demo
  • Operating on Collections Collections are the main datastructure in Clojure A collection can be a list, a vector, a map or a set Some example collection functions: first, second, last, count, reverse, concat, conj, contains?, map, reduce, apply, filter, some, remove, every?, nth, into, doall, repeat, repeatedly, range,
  • Laziness Sequences can be lazy, which allows them to be infinite: (cycle [1 2 3]) ; => (1 2 3 1 2 3 1 2 3 ...) (repeat :a) ; => (:a :a :a :a :a :a :a ...) When using lazy sequences, processing does not occur until the sequence is realized. (class (repeat :a)) ; => clojure.lang.LazySeq Realize (part of) a lazy sequence: (take 5 (repeat 42)) ; => (42 42 42 42 42)
  • More laziness Can do neat things with infinite sequences: (filter even? (iterate inc 1)) ; => (2 4 6 8...) Lazily read lines from a file: (read-lines “/tmp/foo.txt”) Lines aren’t read until they are used in the returned list
  • Java interop (.toLowerCase “EMC”) ; => “emc” (<method> <object> [<arg1> <arg2> ...]) (StringBuffer. “foo” ) ; => #<StringBuffer foo> (Integer/parseInt “5”) ; => 5 (.contains “My spoon is too big.” “spoon“) ; => true (javax.swing.JOptionPane/showMessageDialog nil "Sup!") “Clojure is a better Java than Java”
  • Java datastructures Anything that’s a Collection (or array) in Java can easily be treated (or converted) as a Clojure collection (first (.split “This is a sentence” “ “)) ; => “This” (import ‘java.util.ArrayList) (def foo (ArrayList. [1 2 3])) ; => #<ArrayList [1, 2, 3]> (seq foo) ; => (1 2 3) (vec foo) ; => [1 2 3]
  • More Java-interop Type hints (defn #^String uppercase [#^String s] (.toUpperCase s)) Proxying Java classes (doto (javax.swing.JFrame.) (addKeyListener (proxy [java.awt.event.KeyListener] [] (keyPressed [e] (println (.getKeyChar e) " key pressed")) (keyReleased [e] (println (.getKeyChar e) " key released")) (keyTyped [e] (println (.getKeyChar e) " key typed")))) (setVisible true)) Lots more gen-class, annotations, primitives, definterface, defprotocol
  • Immutability (defn add-age [person] (assoc person :age 10)) (def timmy {:name “Timmy”}) (print timmy) ; => {:name “Timmy”} (add-age timmy) ; => {:name “Timmy”, :age 10} ; timmy never changes (print timmy) ; => {:name “Timmy”} Under the hood: 32-way trie trees. O(log32n)
  • Functional Programming Functions are intended to be side-effect free They take values and return values Same argument, same result No notion of time Easy to add concurrency to the mix sanely Testing is easier because there is no state Having no side effects isn’t always possible (io, mutable java objects)
  • Concurrency Locks are bad Clojure’s defining concurrency feature is STM Gives you ACI out of ACID Atomicity Consistency Isolation Immutability is what makes this possible 4 methods for changing data concurrently:
  • Dead-simple Concurrency (def accounts [{:name “Bob” :balance 1000} {:name “Sue” :balance 2000} {:name “Joe” :balance 3000} {:name “Ann” :balance 4000}]) (defn add-interest [acct] (let [bal (:balance acct)] (Thread/sleep 1500) (assoc acct :balance (* bal 1.25)))) (time (doall (map add-interest accounts))) ; => "Elapsed time: 6001.023 msecs" (time (doall (pmap add-interest accounts))) ; => "Elapsed time: 1519.73 msecs"
  • More simple concurrency All functions defined in Clojure implement Runnable: (defn dowork [] (println “doing work”)) (.start (Thread. dowork)) ; => “doing work” Access to all of Java’s concurrency stuff (ThreadPoolExecutor, etc) with Java interop
  • Concurrency (Refs) (def x (ref 100)) @x ; => 100 (def amounts [5 10 1 9 2]) (defn dec-x [n] (dosync (alter x #(- % n)))) (pmap dec-x amounts) @x ; => 73
  • Concurrency (Atoms) (def x (atom 100)) @x ; => 100 (def amounts [5 10 1 9 2]) (defn dec-x [n] (swap! x #(- % n))) (pmap dec-x amounts) @x ; => 73
  • Concurrency (Agents) (def x (agent 0)) @x ; => 0 (defn increment [c n] (+ c n)) (send x increment 5) ; @x -> 5 (send x increment 10) ; @x -> 15
  • Macros (defmacro def-name-filter [n r] (let [docstring (str "Given a list of people maps, filter " n " in a list.")] `(defn ~n ~docstring [elements#] (filter (fn [t#] (re-find ~r (:name t#))) elements#)))) (def-name-filter foo-filter #"^foo") (def people [{:name "pat"} {:name "foobar"} {:name "foo"} {:name "bar"}]) (doc foo-filter) ; => Given a list of people maps, filter foo-filter in a list. (foo-filter people) ; => ({:name "foobar"} {:name "foo"})
  • Macroexpand example
  • List de-structuring ; Destructure in defn (defn print-items [item & rest] ; Destructure in let (println "item:" item) (println "rest:" rest) (def my-list ‘([:a 1] [:b 2] [:c 3])) (if-not (nil? rest) (apply print-items rest))) (defn print-pair [pair] (print-items 1 2 3 4 5) (let [[key val] pair] (println "key:" key "val:" val))) item: 1 rest: (2 3 4 5) (map print-pair my-list) item: 2 rest: (3 4 5) key: :a val: 1 item: 3 key: :b val: 2 rest: (4 5) key: :c val: 3 item: 4 rest: (5) item: 5 rest: nil
  • Much More Polymorphism/multimethods Pre and Post conditions for functions Futures, Promises Watchers Bindings/Transients Metadata features Compilation Clojure-in-Clojure So much more!
  • More info http://clojure.org http://clojure.blip.tv http://www.assembla.com/wiki/show/clojure/Getting_Started #clojure on irc.freenode.net Come talk to me about it!
  • Thanks! Questions?