Greg's Blog

helping me remember what I figure out

Clojure Data Structures

| Comments

Always easier to remember things when you write them down :).

Syntax

Operations follow this pattern:

1
(operator operand1 operand2 operandn)

No commas between operands, just whitespace.

Clojure uses prefix notation as opposed to infix notation which is more familiar in other languages

1
2
(+ 1 1)
=> 2

Equality

1
2
3
4
5
6
(= 1 1)
=> true
(= "test" "test")
=> true
(= [1 2 3] [1 2 3])
=> true

Strings

Use double quotes to delineate strings, e.g. : "This is a string"

For concatenation use str function:

1
2
3
(def name "string")
(str "This is a " name)
=> "This is a string"

Maps

Map values can be of any type and can be nested.

1
2
3
{:first 1
 :second {:name "Greg" :surname "Stewart"}
 :third "My name"}

Use get to look up values and get-in to look up values in nested maps. Instead of get you can treat it as a function with the key as a parameter.

1
2
3
4
5
6
7
8
9
10
(def my_map {:first 1
#_=>  :second {:name "Greg" :surname "Stewart"}
#_=>  :third "My name"})

(get my_map :first)
=> 1
(get-in my_map [:second :name])
=> "Greg"
(my_map :first)
=> 1

Keywords

In these examples :first is a keyword. Key words can be used as functions:

1
2
(:first my map)
=> 1

Vectors

Think array in other languages. Elements of a Vector can be of any type and you can retrieve values using get as well.

1
2
3
(def my_vector [1 "a" {:name "Greg"}])
(get my_vector 0)
=> 1

Can also be created using vector function:

1
2
(vector "hello" "world" "!")
=> ["hello" "world" "!"]

Using conj you add elements to a vector. Elements get added to the end of a vector.

Lists

Like vectors, however you can’t use get to retrieve values. Use nth instead

1
2
3
(def my_list '("foo" "bar" "baz"))
(nth my_list 1)
=> "bar"

Lists can be created using the list function. Use conj to add items to a list. Unlike vectors they get added to the beginning of the list.

Sets

Collection of unique values. Created either using #{} or set.

1
2
(set [3 3 3 4 4])
#{4 3}

Use get to retrieve values. You can create sets using hash-set or sorted-set as well:

1
2
3
4
(hash-set 3 1 3 3 2 4 4)
=> #{1 4 3 2}
(sorted-set 3 1 3 3 2 4 4)
=> #{1 2 3 4}

Symbols

Another assignment method, however apparently we can manipulate them as if they were data. Not sure what that means yet.

Quoting

' is referred to as quoting. Used this to define a list. Used in macros.

Comments