Re: swap two elements in an arbitrary collection

2009-11-19 Thread David Brown
On Thu, Nov 19, 2009 at 05:51:22AM -0500, John Harrop wrote: >On Thu, Nov 19, 2009 at 4:31 AM, Lauri Pesonen wrote: > >> (clojure.walk/macroexpand-all '(cond (even? 2) :foo (odd? 2) :bar :else >> :baz)) >> (if (even? 2) :foo (if (odd? 2) :bar (if :else :baz nil))) > >Eeeuw. Perhaps the cond macro

Re: swap two elements in an arbitrary collection

2009-11-19 Thread John Harrop
On Thu, Nov 19, 2009 at 4:31 AM, Lauri Pesonen wrote: > (clojure.walk/macroexpand-all '(cond (even? 2) :foo (odd? 2) :bar :else > :baz)) > (if (even? 2) :foo (if (odd? 2) :bar (if :else :baz nil))) Eeeuw. Perhaps the cond macro should check if the last condition is self-evaluating and, if it is

Re: swap two elements in an arbitrary collection

2009-11-19 Thread Lauri Pesonen
2009/11/18 Jacek Laskowski : > > user=> (macroexpand '(-> v (assoc i (v j)) (assoc j (v i > (assoc (clojure.core/-> v (assoc i (v j))) j (v i)) > > How to expand the macro in the subform above? You can use clojure.walk/macroexpand-all: (clojure.walk/macroexpand-all '(cond (even? 2) :foo (odd?

Re: swap two elements in an arbitrary collection

2009-11-18 Thread Jacek Laskowski
On Sat, Nov 14, 2009 at 11:59 PM, Meikel Brandmeyer wrote: > And there is always macroexpand(-1)... > > user=> (macroexpand-1 '(-> foo (bar baz))) > (bar foo baz) I'm glad you've pointed it out as I've recently been asking myself how to expand a macro entirely (including subforms)? I'd prefer kn

Re: swap two elements in an arbitrary collection

2009-11-15 Thread Meikel Brandmeyer
Hi, Am 13.11.2009 um 17:29 schrieb Lauri Pesonen: I hope this helps. I don't think I did a particularly good job in explaining how the macro works... And there is always macroexpand(-1)... user=> (macroexpand-1 '(-> foo (bar baz))) (bar foo baz) Sincerely Meikel smime.p7s Description:

Re: swap two elements in an arbitrary collection

2009-11-13 Thread Lauri Pesonen
Hi Mark, 2009/11/13 Mark Tomko : > > I notice you used the '->' macro.  Perhaps I'm a little dense, but I > haven't found the documentation for it to be terribly helpful.  Do you > have simple, succinct explanation for what it does? The -> macro calls the given forms with the return value of prev

Re: swap two elements in an arbitrary collection

2009-11-13 Thread Mark Tomko
Indeed it does. I still think the array implementation of a heap is probably worth using, in spite of its use of indexes, but rather than use a generalized swap implementation to percolate elements up and down the tree, I'll stick with a vector implementation and keep it all hidden beneath the imp

Re: swap two elements in an arbitrary collection

2009-11-13 Thread Sean Devlin
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 op

Re: swap two elements in an arbitrary collection

2009-11-13 Thread Christophe Grand
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 ass

Re: swap two elements in an arbitrary collection

2009-11-12 Thread Mark Tomko
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

Re: swap two elements in an arbitrary collection

2009-11-12 Thread Mark Tomko
Oh, I posted too soon. My implementation has a bug. On Nov 12, 9:56 pm, Mark Tomko 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)] >  

swap two elements in an arbitrary collection

2009-11-12 Thread Mark Tomko
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 p