E.g., I have a vector [1, 2, 3]
, and I want to update the second element so that the vector becomes [1, 5, 3]
. In other languages, I would just do something like array[1] = 5
, but I'm not aware of anything that would allow me to do this easily in Clojure.
Thoughts on how to accomplish this, or on whether I should be using a different data structure?
assoc
works fine for that. It takes the index where to put the new value and return the newly created vector:
Clojure> (assoc [1 2 3] 1 5)
[1 5 3]
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