What is the idiomatic way to swap two elements in a vector

前端 未结 1 2023
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 03:51

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)]
         


        
相关标签:
1条回答
  • 2020-12-31 03:56

    I can't think of a particularly elegant solution, either. Here's how I'd write it though:

    (defn swap [v i1 i2] 
       (asso­c v i2 (v i1) i1 (v i2)))­
    
    0 讨论(0)
提交回复
热议问题