On Sun, Apr 3, 2011 at 7:15 AM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi,
>
> On 3 Apr., 12:24, Ken Wesson <kwess...@gmail.com> wrote:
>
>> I don't. :)
>>
>> (defn drop-by [f coll]
>>   (let [fs (map f coll)
>>         ps (map = fs (rest fs))
>>         zs (map list ps (rest coll))]
>>     (map second (drop-while first zs))))
>>
>> user=> (drop-by #(mod % 3) [1 4 1 7 34 16 10 2 99 103 42])
>> (2 99 103 42)
>>
>> I especially like the symmetry of using take-while for the one and
>> drop-while for the other, under the hood.
>
> Or a bit less involved.
>
> (defn take-by
>  [f coll]
>  (lazy-seq
>    (when-let [s (seq coll)]
>      (let [fst   (first s)
>            value (f fst)]
>        (cons fst (take-while #(-> % f (= value)) (rest s)))))))
>
> (defn drop-by
>  [f coll]
>  (lazy-seq
>    (when-let [s (seq coll)]
>      (let [fst   (first s)
>            value (f fst)]
>        (drop-while #(-> % f (= value)) (rest s))))))

"Less" involved?

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