Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Comments
- ; Obligatory
- (println "Hello, JISCRIans!")
- ; Numbers
- 25
- 1.0
- 22/7
- ; Strings
- "eggs"
- ; Keywords evaluate to themselves
- :this
- :that
- ; Everything else is a "symbol", and represents something else
- ;
- ; Lists, by default evaluate as function calls
- (+ 5 5)
- ; N.B. + is not an operator, just a symbol that represents a function
- ; use ' to prevent evaluation
- '("pig" "cow" "aufschnitt")
- ; Vectors
- [1 2 3 4 5]
- ["eggs" "bacon" "black pudding"]
- ; Maps (commas are considered to be whitespace)
- {"fred" 5, "bill" 10}
- ; Keywords useful here, and used to define structs
- {:id 3, :name "Jack"}
- ; Maps are functions of their keys, and keywords are functions of any map
- (:name {:id 3, :name "Jack"})
- ({:id 3, :name "Jack"} :id)
- ; Sets
- #{"this" "that" "the other"}
- #{"this" "that" "the other" "this"}
- ; You've now seen pretty much all of the syntax
- ; Defining things (creating a symbol to refer to them)
- (def jack {:id 3 :name "Jack"})
- (println jack)
- (:name jack)
- ; Functions
- (fn [person]
- (:name person))
- ((fn [person]
- (:name person)) jack)
- (defn get-name [person]
- (:name person))
- (get-name jack)
- ; Java interop
- (def sb (StringBuilder. "Quo Vadis?"))
- (.reverse sb)
- ; or
- (. sb reverse)
- ; Chaining methods with ..
- (.. sb reverse toString toUpperCase)
- (doto (new java.util.HashMap)
- (.put "a" 1)
- (.put "b" 2))
- ; Exception handling
- (def f (java.io.File. "not there"))
- (try
- (java.io.FileInputStream. f)
- (catch java.io.FileNotFoundException _ (println "Didn't work!")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement