Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Clojure

1,697 views

Published on

Clojure is simple and mighty

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Clojure

  1. 1. Clojure Yiguang Hu [email_address] Twitter:yighu
  2. 2. Features <ul><li>Elegant, expressive code </li></ul><ul><li>Code is data </li></ul><ul><li>Easy, fast Java interoperability </li></ul><ul><li>A sequence library unifies all kinds of data </li></ul><ul><li>Functional programming encourages reusable, correct code </li></ul><ul><li>Concurrency without manual lock mgmt </li></ul>
  3. 3. Functional Programming <ul><li>Pure functions </li></ul><ul><li>Persistent data structures </li></ul><ul><li>Laziness and Recursion </li></ul><ul><li>Referential Transparency </li></ul>
  4. 4. Forms <ul><li>Constants:5, “xyz” </li></ul><ul><li>Keywords:#age </li></ul><ul><li>Symbols: map,reduce </li></ul><ul><li>Data structure: […] {…} #{…} </li></ul><ul><li>Callables: (if…) (var …) (map …) (and …) </li></ul>
  5. 5. map <ul><li>Map literal form </li></ul><ul><ul><li>(def inventors {&quot;Lisp&quot; &quot;McCarthy&quot; &quot;Clojure&quot; &quot;Hickey&quot;}) </li></ul></ul><ul><ul><li>(inventors &quot;Lisp&quot;) </li></ul></ul><ul><ul><li>&quot;McCarthy&quot; </li></ul></ul><ul><li>Map with keywords as key: </li></ul><ul><ul><li>(def inventors {:Lisp &quot;McCarthy&quot; :Clojure &quot;Hickey&quot;}) </li></ul></ul><ul><ul><li>(inventors :Lisp) </li></ul></ul><ul><ul><li>&quot;McCarthy“ </li></ul></ul><ul><li>Keywords are also functions </li></ul>
  6. 6. Structs <ul><li>(defstruct name & keys) </li></ul><ul><ul><li>(defstruct book :title :author) </li></ul></ul><ul><ul><li>(def b (struct book &quot;Naria&quot; &quot;CS Lewis&quot;)) </li></ul></ul><ul><ul><li>user=> b </li></ul></ul><ul><ul><li>{:title &quot;Naria&quot;, :author &quot;CS Lewis&quot;} </li></ul></ul><ul><ul><li>(:title b) </li></ul></ul><ul><ul><li>&quot;Naria&quot; </li></ul></ul>
  7. 7. Struct-map <ul><li>(def a (struct-map book :copyright 2008 :title &quot;Narnia&quot; :author &quot;CS Lewis&quot;)) </li></ul><ul><li>a </li></ul><ul><li>{:title &quot;Narnia&quot;, :author &quot;CS Lewis&quot;, :copyright 2008} </li></ul><ul><li>(:copyright a) </li></ul><ul><li>2008 </li></ul>
  8. 8. Map destructuring <ul><li>(defn fullname [{fname :first-name lname :lastname}] (str lname “, ” fname)) </li></ul><ul><li>(defn fullname [{:keys [first-name last-name])] (str last-name “, ” first-name)) </li></ul>
  9. 9. Define function <ul><li>(defn add [a b] </li></ul><ul><ul><ul><li>(+ a b) </li></ul></ul></ul><ul><ul><ul><li>) </li></ul></ul></ul><ul><li>(defn ne </li></ul><ul><ul><ul><li>([x] false) </li></ul></ul></ul><ul><ul><ul><li>([x y] (not (= x y))) </li></ul></ul></ul><ul><ul><ul><li>) </li></ul></ul></ul>
  10. 10. Squence library Create Sequences <ul><li>range,repeat, iterate, take,cycle… </li></ul><ul><li>(range 1 5) </li></ul><ul><li>(1 2 3 4) </li></ul><ul><li>(repeat 5 1) </li></ul><ul><li>(1 1 1 1 1) </li></ul><ul><li>(take 5 (iterate inc 1)) </li></ul><ul><li>(1 2 3 4 5) </li></ul><ul><li>(take 10 (cycle (range 3))) </li></ul><ul><li>(0 1 2 0 1 2 0 1 2 0) </li></ul>
  11. 11. Squence library Filtering Sequences <ul><li>(filter odd? (range 10)) </li></ul><ul><li>(1 3 5 7 9) </li></ul>
  12. 12. Squence library Transforming Sequences <ul><li>Map </li></ul><ul><li>reduce </li></ul><ul><li>sort </li></ul><ul><li>sort-by </li></ul>
  13. 13. map <ul><ul><ul><li>(map f coll) </li></ul></ul></ul><ul><li>(def a (list 1 2 3)) </li></ul><ul><li>(defn addn [a] (+ a 5)) </li></ul><ul><li>(map addn a) </li></ul><ul><li>(6 7 8) </li></ul>
  14. 14. Reduce <ul><li>(reduce f coll) </li></ul><ul><li>(reduce f val coll) </li></ul><ul><li>(defn mul [a b] (* a b)) </li></ul><ul><li>(reduce mul a) </li></ul><ul><li>6 </li></ul>
  15. 15. Map->Reduce <ul><li>(map addn a) </li></ul><ul><li>(6 7 8) </li></ul><ul><li>(reduce mul (map addn a)) </li></ul><ul><li>336 </li></ul><ul><li>In parallel: </li></ul><ul><li>(reduce mull (pmap addn a)) </li></ul>
  16. 16. Sort, Sort-by <ul><li>(sort > (range 5)) </li></ul><ul><li>(4 3 2 1 0) </li></ul><ul><li>(sort-by :grade > [{:grade 90} {:grade 80} {:grade 100}]) </li></ul><ul><li>({:grade 100} {:grade 90} {:grade 80}) </li></ul>
  17. 17. Refs <ul><li>(def current-track (ref &quot;oclc, kilgour&quot;)) </li></ul><ul><li>(deref current-track) </li></ul><ul><li>(ref-set current-track &quot;osu, jones tower&quot;) </li></ul><ul><li>java.lang.IllegalStateException: No transaction running (NO_SOURCE_FILE:0) </li></ul><ul><li>(dosync (ref-set current-track &quot;osu, jones tower&quot;)) </li></ul><ul><li>&quot;osu, jones tower&quot; </li></ul>
  18. 18. atoms <ul><li>user=> (def current-book (atom &quot;clojure, Hickey&quot;)) </li></ul><ul><li>#'user/current-book </li></ul><ul><li>user=> (deref current-book) </li></ul><ul><li>&quot;clojure, Hickey&quot; </li></ul><ul><li>user=> (reset! current-book &quot;java&quot;) </li></ul><ul><li>&quot;java&quot; </li></ul>
  19. 19. agent <ul><li>user=> (def counter (agent 0)) </li></ul><ul><li>#'user/counter </li></ul><ul><li>user=> counter </li></ul><ul><li>#<Agent@67e13e07: 0> </li></ul><ul><li>user=> (send counter inc) </li></ul><ul><li>#<Agent@67e13e07: 0> </li></ul><ul><li>user=> counter </li></ul><ul><li>#<Agent@67e13e07: 1> </li></ul>
  20. 20. Clojure in grails <ul><li>grails install-plugin clojure </li></ul><ul><li>New dir src/clj is created </li></ul><ul><li>Add file Addnumber.clj in src/clj </li></ul><ul><ul><ul><li>(ns grails) </li></ul></ul></ul><ul><ul><ul><li>(def twenty 20) </li></ul></ul></ul><ul><ul><ul><li>(defn add_numbers [a b] </li></ul></ul></ul><ul><ul><ul><li>(+ a b)) </li></ul></ul></ul>
  21. 21. Call Clojure from Service <ul><li>class ClojService { </li></ul><ul><li>boolean transactional = false </li></ul><ul><li>def addNumbers(x, y) { </li></ul><ul><li>clj.add_numbers(x, y) </li></ul><ul><li>} </li></ul><ul><li>def addTwenty(x) { </li></ul><ul><li>clj.add_numbers(x, clj.twenty) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  22. 22. Call Service in Controller <ul><li>import grails.converters.XML; </li></ul><ul><li>class PlayController { </li></ul><ul><li>def index = { } </li></ul><ul><li>def clojService </li></ul><ul><li>def cljaddnumber={ </li></ul><ul><li>render clojService.addNumbers(Integer.parseInt(params.a?:&quot;5&quot;), Integer.parseInt(params.b?:&quot;4&quot;)) </li></ul><ul><li>} </li></ul><ul><li>def cljadd20={ </li></ul><ul><li>render clojService.addTwenty(Integer.parseInt(params.a?:&quot;10&quot;)) </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 23. Reference <ul><li>http://www.clojure.org </li></ul><ul><li>Programming Clojure by Stuart Halloway </li></ul>

×