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)

I leave drop-by as an exercise for the reader, but the punch line here
is (map foo s (rest s)). :)

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