Clojure again

Welcome back to my clojure ramblings while I try to understand more.
Tonight we are going to pick up where we left off, and that has to do with Vectors.

NOTE:

I am writing this as a way to digest and understand the book that I am working through entitled. Living Clojure

Vectors

Vectors can be found because they are wrapped in []

user=> [ :bucket 3 4 :cup ]
[:bucket 3 4 :cup]

user=> [:thing "other" :mine]
[:thing "other" :mine]

Vectors are similar to lists we can have a mix of types in them, and some of the same operators can be used on them. Such as first and rest

user=> (first [ :thing1 :thing2 ] )
:thing1

user=> (rest [ :thing1 :thing2 :thing3 ])
(:thing2 :thing3)

However we get a bonus with vectors and that is “fast access”. We can take the vector and go to a specific point in the vector without having to iterate our way through. This is super usefull if we need to go to the 100th and 1000th place in the vector. We do that by using the nth

user=> (nth [:thing1 :thing2 :thing3 :thing4] 3)
:thing4

Cool that means vectors can be super fast, I like that. Same thing with the last operator, I don’t want to know how many things I have, I just want to go to the end.

user=> (last [:thing1 :thing2 :thing3 :thing4] )
:thing4

These two operators are available to lists, however we will have to step through every element in the list before we get to the one that we want. This can be a very costly operation.

Both of these are collections, and the cool thing about collections in clojure is that they are immutable which means they original will always be the same. Because, everything is a function, when we try and do a thing to the data object function, we are not transmuting or changing the original, we are getting a new thing on top of it. This allows us to always know what the original is and always know all the steps in between.

Maps

Maps are rad, and they are powerful. They are another collection, and they are the key value pairs for clojure. Again, we can drop off the use of the commas, and never look back. Its ok I know that it is weird, but trust me it is better this way, we have cake.

user=> {:thing1 "is blue" :thing2 "is purple" :thing3 "is orange"}
{:thing1 "is blue", :thing2 "is purple", :thing3 "is orange"}

The maps also have a getter and a default value option that will populate if nothing is found. Lets take a look at those.

user=> (get {:thing1 "is blue" :thing2 "is purple" :thing3 "is orange"} :thing2)
"is purple"

user=> (get {:thing1 "is blue" :thing2 "is purple" :thing3 "is orange"} :thing4, "not found")
"not found"

The same thing can be acheived by using the key as the function, which IMO is easier anyway.

user=> (:thing4 {:thing1 "is blue" :thing2 "is purple" :thing3 "is orange"} "not found")
"not found"
user=> (:thing1 {:thing1 "is blue" :thing2 "is purple" :thing3 "is orange"} "not found")
"is blue"

Then we have assoc and merge. assoc will update an existing key, however do not think of it as updating the key. Think of it as getting a new map with everything the same except the new value. Merge is how we are going to merge to maps together.

user=> (assoc {:one 1 } :one 2)
{:one 2}

user=> (merge {:one 1 :two 2} {:three 3 :four 4})
{:one 1, :two 2, :three 3, :four 4}

Well I think that is about all that I can handle for tonight, I know that it doesn’t seem like I went very far, but it is something right?