Hi,
I thought perhaps asking on IRC instead here - but chose here anyway :)

I am doing just some exercises in scope of various algorithms, data 
structures etc.
Currently, in scope is *selection sort*.

I came up of this implementation in Clojure (original examples are in 
Python and Ruby where
indices are directly manipulated and arrays tumbled over etc which is of 
course not encouraged in Clojure)

(defn- smallest [xs]
   "Searches for smallest element and returns it's value and position"
  (reduce #(let [[e im ci] %]
            (if (<= %2 e)
              [%2 ci (inc ci)]
              [e im (inc ci)])) [(first xs) 0 0] xs))

(defn selection-sort [s]
  "Selection sort itself :)"
  (loop [xs s acc []]
    (if (seq xs)
      (let [[x i] (smallest xs)]
        (recur (concat (take i xs) (drop (inc i) xs)) (conj acc x))) ; <--
      acc)))


I don't need optimized implementation or anything :) 
It should be, well, selection sort with all it's flaws :)

I am just looking for advice if it could be made more Clojure-way?
I am specially interested in better way for processing input which shrinks 
after each step.
I was thinking of using *iterate *equipped with appropriate iteration 
function 
but that would be kinda of ugly once input is exhausted...

Thanks in advance and apologizes to those who might find this kind of 
question in this group annoying.

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