On Thu, Nov 19, 2009 at 1:23 PM, Sean Devlin <francoisdev...@gmail.com>wrote:

> Try clojure.contrib.seq-utils :)
>
> As a learning exercise, I'd recommend re-writing it to be lazy.  Your
> version is eager because it uses loop.  In order to make it lazy,
> you'd want to construct a lazy-seq.  See the macro w/ the same name.
>
> Another choice is to use built-in functions, like this:
>
> (defn positions [pred coll]
>  (map second
>    (filter (comp pred first)
>      (map vector coll (iterate inc 0)))))


(defn indexed [coll]
  (map vector (iterate inc 0) coll))

(defn positions [pred coll]
  (for [[i e] (indexed coll) :when (pred e)] i))

Seems to work:

user=> (positions even? [1 1 2 9 3 4 8 7 6])
(2 5 6 8)

(yes, I know there's already an "indexed" with similar semantics in
clojure.contrib.)

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

Reply via email to