Created
January 27, 2014 00:37
-
-
Save daveyarwood/8641433 to your computer and use it in GitHub Desktop.
why's (poignant) guide to ruby in clojure - chapter 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ex. 1: | |
(repeat 5 "Odelay!") | |
; ex. 2: | |
(when-not (re-find #"aura" "restaurant") (System/exit 0)) | |
; ex. 3: | |
(map clojure.string/capitalize ["toast" "cheese" "wine"]) | |
; ex. 4: | |
(def teddy-bear-fee 121.08) | |
; ex. 5: | |
(def total (+ orphan-fee teddy-bear-fee gratuity)) | |
; ex. 6: | |
(def population 12000000000) ; no underscore notation :( | |
; ex. 7: | |
(def avril-quote | |
"I'm a lot wiser. Now I know what the business is like -- | |
what you have to do and how to work it.") | |
; ex. 8: | |
(print oprah-quote) | |
(print avril-quote) | |
(print ashlee-simpson-debacle) | |
; ex. 9: | |
(def EmpireStateBuilding "350 5th Avenue, NYC, NY") | |
; although most values are essentially immutable in Clojure, | |
; so there is usually no need to capitalize the names of symbols | |
; to reflect that they are "constants" | |
; ex. 10: | |
(open front-door) | |
; ex. 11: | |
(close (open front-door)) | |
; ex. 12: | |
(is-open? front-door) | |
; ex. 13: | |
(paint front-door 3 :red) | |
; ex. 14: | |
(-> front-door | |
(paint 3 :red) | |
(dry 30) | |
(close)) | |
; ex. 15: | |
(print "See, no dot.") | |
; ex. 16: Door::new( :oak ) | |
; you could create a Door record, or you could simply represent doors as | |
; map structures with a :material attribute like so: | |
(def oaken-door {:material :oak}) | |
; and maybe define a (redundant) function to "create" a new door, like this: | |
(defn door [material] | |
{:material material}) | |
(door :oak) | |
; ex. 17: | |
(repeat 2 "Yes, I've used chunky bacon in my examples, but never again!") | |
; ex. 18: | |
(loop [] | |
(print "Much better.") | |
(print "Ah. More space!") | |
(print "My back was killin' me in those crab pincers.") | |
(recur)) | |
; ex. 19: ; { |x,y| x + y } | |
; no general equivalent of Ruby blocks/block arguments in Clojure, | |
; although this idea fits into the concept of locals/bindings. | |
; #(+ %1 %2) is kind of a translation of this Ruby code | |
; ex. 20: | |
{:name "Peter", :profession "lion tamer", :great-love "flannel"} | |
; ex. 21: | |
(slurp "http://www.ruby-lang.org/en/LICENSE.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment