How do I extend this: What is the idiomatic way to swap two elements in a vector
to essentially a 2D array?
[[1 2 3] [4 5 6] [7 8 9]] --> [[1 2 5] [4 3 6] [7 8 9]]
How do I extend this: What is the idiomatic way to swap two elements in a vector
to essentially a 2D array?
[[1 2 3] [4 5 6] [7 8 9]] --> [[1 2 5] [4 3 6] [7 8 9]]
(defn swap-in [vv p1 p2]
(let [v1 (get-in vv p1)
v2 (get-in vv p2)
(-> vv
(assoc-in p1 v2)
(assoc-in p2 v1))))
So now:
(def my-vec [[1 2 3] [4 5 6] [7 8 9]])
(swap-in my-vec [0 2] [1 1])
=> [[1 2 5] [4 3 6] [7 8 9]]
This will work with vectors nested to any "depth", so long as the specified positions in my-vec actually exist.