Your SlideShare is downloading. ×
0
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
Clojure for Rubyists
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 for Rubyists

2,696

Published on

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

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

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,696
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
3
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 for Rubyists For Montreal.rb on September 21 st 2010 By Jeff Heon
  • 2. Clojure in a nutshell <ul><li>Functional
  • 3. Concurrency
  • 4. JVM
  • 5. Lisp </li></ul>
  • 6. Similarities <ul>Interactive development </ul>Ruby Clojure irb REPL (read eval loop print) java -cp clojure.jar clojure.main
  • 7. Similarities <ul>Ruby symbols are Clojure keywords. </ul>Ruby Clojure :symbol :keyword
  • 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. 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. 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. 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. 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. 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. 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. Ref
  • 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. 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. Clojure in Action
  • 19. The Joy of Clojure
  • 20. Practical Clojure
  • 21. And one upcoming from O'Reilly </li></ul>

×