Exercise 04 Vectors - MikeyYeahYeah/clojure-koans GitHub Wiki

Chapter 04 - Vectors

A Vector is an array like structure. Allow you to access items via elements.

  • (vector 1 2 3)
  • (["alpha" "beta" "charlie"])

Create a vector from a list or elements

(vec '(1 2 3 4 5))
	;;=> [1 2 3 4 5]

(vector nil nil)
	;;=> [nil nil]

Conjoining on a list appends to the end of a vector

Conjoin will always try and perform the most effecient method. In a vector, things get added to the end of a sequence as opposed to the beggining like in a list.

(conj [111 222] 333)
	;;=> [111 222 333]

Working on Vectors

Grab First Item
(first [:peanut :butter :and :jelly])
  ;;=> :peanut
Grab Last Item
(last [:peanut :butter :and :jelly])
  ;;=> :jelly
Grab Specific Index
(nth [:peanut :butter :and :jelly] 3)
  ;;=> :jelly
Grab a Slice of a Vector
(subvec [:peanut :butter :and :jelly] 1 3)
  ;;=> [:butter :and]