> On Oct 10, 2016, at 6:50 AM, Rastko Soskic <rss...@gmail.com> wrote:
> 
> I am specially interested in better way for processing input which shrinks 
> after each step.

Vectors have some nice properties if your action is mostly at the end.  The 
peek and pop functions are useful for “shrinking” a vector.  Also, you can 
assoc into a vector, which is sometimes simpler to use than list manipulations. 
 By the way, reduce-kv works with vectors so that’s a good way to find the 
smallest element and its index.

I’m cheating a bit by filling the “hole” of the selected item with the last 
element so I can pop the input vector without losing anything.

Caveat: not a recommended way to sort, only lightly tested.

(defn selsort [coll]
  (loop [v (vec coll) sv []]
    (if (seq v)
      (let [pi (dec (count v))
            px (peek v)
            [i0 x0] (reduce-kv (fn [r i x] (if (< x (peek r)) [i x] r))
                                  [pi px]
                                  (pop v))]
        (recur (pop (assoc v i0 px)) (conj sv x0)))
      sv)))



-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to