Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 798 Bytes

swap-two-items-in-a-vector.md

File metadata and controls

31 lines (23 loc) · 798 Bytes

Swap Two Items in a Vector

If you want to replace the value at an index in a vector, you can use the assoc function supplied by clojure.core like so:

> (assoc [5 6 7 8] 1 9)
; [5 9 7 8]

Likewise, if you want to replace two items in a vector, you can extend the above like so:

> (assoc [5 6 7 8] 1 2 2 4)
; [5 2 4 8]

We can take advantage of that second example to construct a function that will swap two values:

(defn swap
  [items i j]
  (assoc items i (items j) j (items i)))

This function will break on values of i and j that are out of the bounds of items, but dealing with that is left as an exercise to the reader.

source