Head into Clojure – Part 2: The Basic Types

More Clojure-ness code examples. This time some basic types, collections and sequences


; Basic Types

;; Chars
;; Clojure represents all chars in with a leading \
(char 97)					;; Returns \a, 97 being ASCII a
(char? \a)					;; true
(char? (char 97))				;; true

;; Strings
“This is a string”				;; Define a string, uses double quotes “…”
(string? "Are you a string")			;; Check if a string
(println "Hello world")				;; Print a string out
print-str
println-str
pr-str
prn-str
(with-out-str (println "this should return as a string"))
(subs "1234" 1)
(subs “abcdef” 1 2)
(count “1 2 3 4”)				;; Counts the number of chars in a string

;; Collections and Sequences

;; For a good description about the difference ( mainly in your head ) between Clojure collections
;; and sequences, head over to http://www.brainonfire.net/files/seqs-and-colls/main.html
;; What you need to get your head round is every sequence is a collection, but not every collection is a sequence.

;;; Lists					;; Linked List
(+ 1 2 3)					;; List, function as first item, evaluates to 6
‘( 1 2 3 4 )					;; List, made up of 4 items
(list 1 3 4 5)					;; Another way to produce a list
(def my-list ‘(1 2 3))				;; List, assigned to variable my-list

;;; Vectors
[1 2 3]						;; Vector of 3 items
[[1 2] [3 4] [5 6]]				;; Vector of 3 Vectors
(vector 1 2 3)					;; Another way to create a vector
(def my-vector [1 2 3 4])			;; Assign vector of 4 times to variable my-vector
(my-vector 2)					;; Access 3rd element ( vectors zero based )
(get my-vector 0)                               ;; 1
(get my-vector 2)                               ;; 3
(get my-vector 10)                              ;; nil

;;; Maps					;; value to value
{ 1 2 3 4 }					;; Create a vector or 2 elements, 1 => 2 and 3 => 4
						;; Creating maps always needs an even number of elements
(def my-map {1 2 3 4})
(my-map 1)					;; Returns value with key 1
(my-map 5)					;; Returns nil
(def my-map {:name "fred" :age 32})		;; Map with keywords
(my-map :name)					;; Access map via keyword
(get my-map :name)                              ;; fred
(def my-map2 {“name” "fred" “age” 32})		;; Map with strings as keys
(my-map2 “name”)				;; Access map via str key

;;; Sets					;; Sets - List of unique values, no duplicates
#{1 2 3 4}                                      ;; Defined using #{..}
(set [1 2 3 4 5 6])				;; Results in a set of 6 items 1, 2, 3, 4, 5 and 6
(set [1 2 2 1 1 2])				;; Results in a set of 2 items 1 and 2
(my-set 1)
(get my-set 1)

;; Manipulation Collections

(first '(1 2 3))				;; Get the first element of a list
(first [1 2 3])					;; Get the first element of a Vector
(first {1 2 3 4})				;; Get the first element of a value map
(first {:first 1 :second 2})			;; Get first element of keyword map, but which gets
						;; returned is indeterminate because you don’t know
						;; internally how the map is stored in order

(rest '(1 2 3))					;; Get all but the first element of a list
(rest [1 2 3])					;; Get all but the first element of a Vector
(rest {1 2 3 4})				;; Get all but the first element of a value map
(rest {:first 1 :second 2})			;; Get all but the first element of keyword map, 
						;; but which gets returned is indeterminate because 
						;; you don’t know internally how the map is stored in order

(cons 1 '(2 3 4))				;; Add element to front of list	
(cons 1 [2 3 4])				;; Add element to front of Vector