Another way to skin the cat: (defn take-by [f coll] (cons (first coll) (for [[x y] (map (fn [x y] [x y]) coll (rest coll)) :while (= (f x) (f y))] x)))
Cheers Andreas On 03/04/2011, at 2:21 PM, Ken Wesson wrote: > On Sun, Apr 3, 2011 at 12:16 AM, Ken Wesson <kwess...@gmail.com> wrote: >> On Sat, Apr 2, 2011 at 11:38 PM, Stefan Rohlfing >> <stefan.rohlf...@gmail.com> wrote: >>> I am sure there is a standard functional way of comparing adjacent items in >>> a coll and would be glad if someone could point me to it. >> >> (defvar- sentinel (Object.)) >> >> (defn take-by [f coll] >> (let [fs (map f coll) >> ps (map = fs (rest fs)) >> zs (map #(if %1 %2 sentinel) ps coll)] >> (take-while (partial not= sentinel) zs)) >> >> user=> (take-by #(mod % 3) [1 4 1 7 34 16 10 2 99 103 42]) >> (1 4 1 7 34 16) > > Actually, this should include the 10 after the 16. It needs to be > > (defn take-by [f coll] > (let [fs (map f coll) > ps (map = fs (rest fs)) > zs (map #(if %1 %2 sentinel) ps (rest coll))] > (cons (first coll) (take-while (partial not= sentinel) zs)))) > > -- > 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 -- "Test-driven Dentistry (TDD!) - Not everything should be test driven" - Michael Fogus -- ********************************************************** Andreas Koestler, Software Engineer Leica Geosystems Pty Ltd 270 Gladstone Road, Dutton Park QLD 4102 Main: +61 7 3891 9772 Direct: +61 7 3117 8808 Fax: +61 7 3891 9336 Email: andreas.koest...@leica-geosystems.com ************www.leica-geosystems.com************* when it has to be right, Leica Geosystems Please consider the environment before printing this email. -- 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