Or more in line with the implementation of take-while:
(defn take-by [f coll]
(lazy-seq
(when-let [s (seq coll)]
(let [x (first s)
y (second s)]
(if (= (f x) (f y))
(cons x (take-by f (rest s)))
(list x))))))
Kind Regards
Andreas
On 03/04/2011, at 2:16 PM, Ken Wesson 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)
>
> 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 [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
--
"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: [email protected]
************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 [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