I like this solution as well. You have to pull in the seq-utils for the indexed function though.
(use 'clojure.contrib.seq-utils) (defn nonzero-idxs [s] (for [[i n] (indexed s) :when (and n (not (zero? n)))] i)) On Wed, Nov 19, 2008 at 10:39 AM, Rich Hickey <[EMAIL PROTECTED]> wrote: > > > > On Nov 19, 11:18 am, capricorn20 <[EMAIL PROTECTED]> wrote: > > I want create seq which keep indexes of non-zero elements of > > collection. > > Code: > > > > (defn nonzero-inx [s] > > (filter (fn [x] (not (nil? x))) (map (fn [a b] (if (not (== a 0)) > > b)) s (range (count s))))) > > > > look complicated than solution in most imperative languages. > > May be possible simplest way in Clojure? > > You are on the right track, this a bit more succinct: > > (defn nonzero-idxs [s] > (remove nil? > (map #(when-not (zero? %1) %2) > s (iterate inc 0)))) > > Rich > > > --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---