Is there a better or more concise way to do the following?
(defn swap [v i1 i2]
"swap two positions in a vector"
(let [e1 (v i1)
e2 (v i2)]
(-> (assoc v i1 e2)
(assoc i2 e1))))
The std::vector::swap() function is used to swap the entire contents of one vector with another vector of same type. If std::swap() function is used for swapping two vectors A and B, it will call specialized std::swap algorithm for std::vector which in turn calls A. swap(B) and swaps the contents.
The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables. Parameters: The function accepts two mandatory parameters a and b which are to be swapped. The parameters can be of any data type.
I can't think of a particularly elegant solution, either. Here's how I'd write it though:
(defn swap [v i1 i2]
(assoc v i2 (v i1) i1 (v i2)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With