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:
Likewise, if you want to replace two items in a vector, you can extend the above like so:
We can take advantage of that second example to construct a function that will swap two values:
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.
Last updated