Yeah, and this code has the side effect of working on maps user=>(swap [:a :b :c :d] 0 2) [:c :b :a :d]
user=>(swap {:a "1" :b "2" :c "3" :d "4"} :a :c) {:a "3", :b "2", :c "1", :d "4"} Hmmm... is this worth keeping? Maybe use the name switch instead, to avoid confusion with swap! (the atom operation). Thoughts? On Nov 13, 3:22 am, Christophe Grand <christo...@cgrand.net> wrote: > It's a pet peeve of mine but, please, try hard not to use indices :-) > (or if you need indices pick a better suited data structure) > The code you try to write is hard to write because you are going > against the grain. > > If you try to write swap for vectors (which support efficient random > lookup and assoc) it's pretty simple: > (defn swap [v i j] > (-> v (assoc i (v j)) (assoc j (v i)))) > > But if you want to make it work with any kind of collection and return > a seq (it's the code you have written) it's far less pleasant... and > efficient. > > hth > > Christophe > > > > On Fri, Nov 13, 2009 at 7:07 AM, Mark Tomko <mjt0...@gmail.com> wrote: > > Let's try this again: > > > (defn swap [coll i j] > > (if (= i j) coll > > (let [li (min i j) ui (max i j)] > > (let [[pre-li post-li] (split-at li coll)] > > (let [[post-li-pre-ui post-li-post-ui] > > (split-at (- ui 1 li) (rest post-li))] > > (concat > > pre-li > > (list (nth coll ui)) > > post-li-pre-ui > > (list (nth coll li)) > > (rest post-li-post-ui))))))) > > > The code is actually even more complicated. I'm sure with a little > > more time I could clean it up. > > > On Nov 12, 9:59 pm, Mark Tomko <mjt0...@gmail.com> wrote: > >> Oh, I posted too soon. My implementation has a bug. > > >> On Nov 12, 9:56 pm, Mark Tomko <mjt0...@gmail.com> wrote: > > >> > I came up with a way to do it, but I'm sure there's a simpler way. > >> > Here's what I have: > > >> > (defn swap [coll i j] > >> > (let [li (min i j) ui (max i j)] > >> > (let [[pre-li post-li] (split-at li coll)] > >> > (let [[post-li-pre-ui post-li-post-ui] (split-at (- ui 1) (rest > >> > post-li))] > >> > (concat > >> > pre-li > >> > (list (nth coll j)) > >> > post-li-pre-ui > >> > (list (nth coll i)) > >> > (rest post-li-post-ui)))))) > > >> > Basically, I find the lower index and the upper index. I then find > >> > the elements in the collection that appear before the lower index, and > >> > the elements that appear between the lower index and the upper index, > >> > and the elements that appear after the upper index. I then create a > >> > new list that is the concatenation of these sublists, in order. It's > >> > sort of the definition of swap on an immutable data structure, but it > >> > feels like an awful lot of code. > > >> > I considered using subvec: > > >> >http://clojure.org/api#toc548 > > >> > But I didn't want to require that my input collection be a vector, or > >> > convert it to one. This the precursor to my implementing a heap data > >> > structure, as a little toy application. > > > -- > > You received this message because you are subscribed to the Google > > Groups "Clojure" group. > > To post to this group, send email to clojure@googlegroups.com > > Note that posts from new members are moderated - please be patient with > > your first post. > > To unsubscribe from this group, send email to > > clojure+unsubscr...@googlegroups.com > > For more options, visit this group at > >http://groups.google.com/group/clojure?hl=en > > -- > Professional:http://cgrand.net/(fr) > On Clojure:http://clj-me.cgrand.net/(en) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en