Upcoming SlideShare
×

# Clojure for Rubyists

3,022 views

Published on

Slides for a small Clojure presentation given at Montreal.rb on September 21st 2010.

Published in: Technology, Education
4 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,022
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
6
0
Likes
4
Embeds 0
No embeds

No notes for slide

### Clojure for Rubyists

1. 1. Clojure for Rubyists For Montreal.rb on September 21 st 2010 By Jeff Heon
2. 2. Clojure in a nutshell <ul><li>Functional
3. 3. Concurrency
4. 4. JVM
5. 5. Lisp </li></ul>
6. 6. Similarities <ul>Interactive development </ul>Ruby Clojure irb REPL (read eval loop print) java -cp clojure.jar clojure.main
7. 7. Similarities <ul>Ruby symbols are Clojure keywords. </ul>Ruby Clojure :symbol :keyword
8. 8. Similarities <ul>In a conditional, nil and false evalute to false and everything else to true. </ul>Ruby Clojure if 0 then true end => true if nil then true else false end => false (if 0 true) => true (if nil true false) => false
9. 9. Similarities <ul>Last expression in a method is the return value. </ul>Ruby Clojure def convert_farenheit_to_celcius(f) (f - 32) * 5.0 / 9 end (defn convert-farenheit-to-celcius [f] (* (- f 32) (/ 5.0 0)))
10. 10. Basic types Ruby Clojure letter_digit = /^[a-zA-Z][0-9]\$/ (def letter-digit #&quot;^[a-zA-Z][0-9]\$&quot;) [0, 'Ruby', 42] my_array[0] [0 &quot;Ruby&quot; 42] (my-vector 0) { :r => 'Ruby', :c => 'Clojure' } #Accessing my_hash[:r] { :r &quot;Ruby&quot; :c &quot;Clojure&quot; } ;Accessing (my-hash :c)
11. 11. Functional Ruby Clojure def odd?(n) (n % 2) == 1 end #Sum of odd numbers from 0 to 99 (0..99). select { |n| odd? n }. inject(0) { |acc, n| acc + n } => 2500 (defn odd? [n] (= 1 (mod n 2))) ;Sum of odd numbers from 0 to 99 (reduce + (filter odd? (range 100))) => 2500
12. 12. Functional, but lazy Ruby Clojure def odd_trace?(n) puts n (n % 2) == 1 end #Sum of odd numbers from 0 to 9 (0..99). select { |n| puts n;(n % 2) == 1 }. first(10). inject(0) { |acc, n| acc + n } => prints 0 to 99, returns 100 (defn odd-trace? [n] (print n) (= 1 (mod n 2))) ;Sum of odd numbers from 0 to 9 (reduce + (take 10 (filter odd-trace? (range 100)))) => prints 0 to 31, returns 100
13. 13. Immutable data structure Data structure are always immutable. Adding or removing an element to an array, vector, hash or set will always return a new collection. But, they are efficient because they are persistant (in the sense that they share structure.)
14. 14. Concurrency Software transactional memory Or having multiple threads access the same reference using transactions instead of locks. STMs are not all the same (like GC) Readers are never blocked. Works hand in hand with persistant data structure.
15. 15. Ref
16. 16. Where did my books go? (def book1 { :author &quot;David Flanagan&quot; :title &quot;The Ruby Programming language&quot; }) (def book2 { :author &quot;Gregory Brown&quot; :title &quot;Ruby Best Practices&quot;}) (def home-books (ref #{book1 book2})) (def lent-books (ref #{})) (alter home-books disj book1) => No transaction running (dosync (alter home-books disj book1) (alter lent-books conj book1)) @home-books => #{{:author &quot;Gregory Brown&quot;, :title &quot;Ruby Best Practices&quot;}} @lent-books => #{{:author &quot;David Flanagan&quot;, :title &quot;The Ruby Programming language&quot;}}
17. 17. Further exploration If nothing else: Are we there yet? Main site: Clojure.org News & getting started: Disclojure Books ( a comparison ) <ul><li>Programming Clojure
18. 18. Clojure in Action
19. 19. The Joy of Clojure
20. 20. Practical Clojure
21. 21. And one upcoming from O'Reilly </li></ul>