On Sun, Apr 3, 2011 at 12:16 AM, Ken Wesson <[email protected]> wrote:
> On Sat, Apr 2, 2011 at 11:38 PM, Stefan Rohlfing
> <[email protected]> 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 [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