Without bugs this time:
(defn take-by [f coll]
(lazy-seq
(when-let [s (seq coll)]
(let [x (first s)
y (second s)]
(if (and x y (= (f x) (f y)))
(cons x (take-by f (rest s)))
(list x))))))
On 03/04/2011, at 2:21 PM, Ken Wesson wrote:
> 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
--
"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