# Clojure Koans: Vectors

Learning all about vectors in this episode. They are very similar to lists but have some key differences that Richard Hickey deemed necessary. Lets get started!

`lein koan run`
“You can use vectors in clojure as array-like structures”
`(= __ (count [42]))`

1

“You can create a vector from a list”
`(= __ (vec ‘(1)))`

[1]

“Or from some elements”
`(= __ (vector nil nil))`

[nil nil]

Thats an interesting vector.

“But you can populate it with any number of elements at once”
`(= [1 __] (vec ‘(1 2)))`

vec: Creates a new vector containing the contents of coll. Java arrays will be aliased and should not be modified.

[1 2]

“Conjoining to a vector is different than to a list”
`(= __ (conj [111 222] 333))`

[111 222 333]

Vectors will join to the back while lists will conjoin to the front.

“You can get the first element of a vector like so”
`(= __ (first [:peanut :butter :and :jelly]))`

:peanut

“And the last in a similar fashion”
`(= __ (last [:peanut :butter :and :jelly]))`

:jelly

“Or any index if you wish”
`(= __ (nth [:peanut :butter :and :jelly] 3))`

:jelly

“You can also slice a vector”
`(= __ (subvec [:peanut :butter :and :jelly] 1 3))`

[:butter :and]

subvec: Returns a persistent vector of items in vector from start to end.

subvec does not include the last index.

“Equality with collections is in terms of values”
`(= (list 1 2 3) (vector 1 2 __)))`

3

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.