John, You should have added that you code came from Programming Clojure.
Regards, Emeka On Thu, Nov 19, 2009 at 8:05 PM, John Harrop <[email protected]> wrote: > On Thu, Nov 19, 2009 at 1:23 PM, Sean Devlin <[email protected]>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 [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected]<clojure%[email protected]> > 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 post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. 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
