Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-05 Thread Meikel Brandmeyer
Hi, On 5 Apr., 14:25, Ken Wesson wrote: > I don't think so. I just tested it and adding a call to seq there > causes it to retain more data in memory, longer, not the other way > around. I don't understand this from a logical point of view. (seq (drop- while ...)) can only *remove* references,

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-05 Thread Ken Wesson
On Tue, Apr 5, 2011 at 8:09 AM, Meikel Brandmeyer wrote: > (defn partition-by [f s] >  (lazy-seq >    (let [s (seq s)] >      (if s >        (let [fs (map f s) >              eq (cons true (map = fs (rest fs))) >              eqs (seq (map list eq s)) >              step (fn step [eqs] >          

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-05 Thread Meikel Brandmeyer
Hi, On 5 Apr., 13:08, Ken Wesson wrote: > This seems to contradict the test results I posted earlier. Perhaps it > is behaving differently on your system -- what is your Clojure > version? Maybe some issue with locals clearing? Clojure 1.2 running on Windows with JDK 1.6.0_24. > In any event,

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-05 Thread Ken Wesson
>> If I understand your claim correctly, it's therefore wrong. > > Since it's correct, you don't understand my claim correctly. That is rather rude and arrogant. > Using your partition-by. [snip] This seems to contradict the test results I posted earlier. Perhaps it is behaving differently on yo

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-05 Thread Meikel Brandmeyer
Hi, Disclaimer: believing this kind of benchmark. No clue whether the following is really valid. On 5 Apr., 09:44, Ken Wesson wrote: > If I understand your claim correctly, it's therefore wrong. Since it's correct, you don't understand my claim correctly. The numbers in between are the memrem

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-05 Thread Ken Wesson
On Tue, Apr 5, 2011 at 2:27 AM, Meikel Brandmeyer wrote: > Hi, > > On 5 Apr., 01:27, Ken Wesson wrote: > >> Oh, and it also doesn't hold onto the head: > > Your version of partition-by is basically equivalent to: > > (defn partition-by >  [f coll] >  (lazy-seq >    (when-let [s (seq coll)] >    

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-04 Thread Meikel Brandmeyer
Hi, On 5 Apr., 01:27, Ken Wesson wrote: > Oh, and it also doesn't hold onto the head: Your version of partition-by is basically equivalent to: (defn partition-by [f coll] (lazy-seq (when-let [s (seq coll)] (let [fst (first s) value(f fst)

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-04 Thread Ken Wesson
On Mon, Apr 4, 2011 at 7:07 PM, Ken Wesson wrote: >> I summarised my issues with the different solutions. Since things got >> a bit philosophical I wrote up a short blog post on my point of view: >> http://bit.ly/hInw0J > > I'm surprised to hear that partition-by is not very lazy, and will > hang

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-04 Thread Ken Wesson
> I summarised my issues with the different solutions. Since things got > a bit philosophical I wrote up a short blog post on my point of view: > http://bit.ly/hInw0J I'm surprised to hear that partition-by is not very lazy, and will hang on an infinite seq. Fortunately, that can be fixed: (defn

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-04 Thread Meikel Brandmeyer
Hi, On 3 Apr., 14:45, Roman Sykora <4rt.f...@gmail.com> wrote: > Now, my question is what's the improvement of > > > (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-

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-04 Thread Alan
Yes. Generally push lazy-seq as high in the chain as you can (but no higher!), for that reason.. On Apr 3, 11:09 pm, Roman Sykora <4rt.f...@gmail.com> wrote: > Thank you for your time and kind answers. I really enjoy reading this > group and the whole clojure community. Enough flattery ;) > > A qu

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-04 Thread Roman Sykora
Thank you for your time and kind answers. I really enjoy reading this group and the whole clojure community. Enough flattery ;) A question remains though (for now). Would I use (lazy-seq (some (stuff ...))) instead of (some (stuff (lazy-seq ...))) because some stuff would only be

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Ken Wesson
On Sun, Apr 3, 2011 at 8:45 AM, Roman Sykora <4rt.f...@gmail.com> wrote: > Now, my question is what's the improvement of > >> (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) (ta

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Alan
I like your version, Roman. It seems as efficient as anything, and is easy to read. For what it's worth, I'd make a small rewrite: (defn take-by [f coll] (lazy-seq (when-let [[x & xs] (seq coll)] (let [val (f x)] (cons x (take-while (comp #{val} f) xs)) On Apr 3, 5:45 am, Rom

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Roman Sykora
Hi When I read this post in the morning I thought about how I would implement this function, and the result was: (defn take-by [f coll] (when-let [[x & xs] (seq coll)] (let [n (f x)] (lazy-seq (cons x (take-while #(= n (f %)) xs)) I didn't post it, because I really am a beginne

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Andreas Kostler
There you go, symmetry and simplicity :) On 04/04/2011, at 6:35 AM, Alan wrote: > Isn't all this just a special case of partition-by? > > (defn drop-by [f coll] > (apply concat (rest (partition-by f coll > > (defn take-by [f coll] > (first (partition-by f coll))) > > user> (drop-by (part

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Alan
Isn't all this just a special case of partition-by? (defn drop-by [f coll] (apply concat (rest (partition-by f coll (defn take-by [f coll] (first (partition-by f coll))) user> (drop-by (partial + 2) [2 2 2 3 3 4]) (3 3 4) user> (take-by #(mod % 3) [1 4 1 7 34 16 10 2 99 103 42]) (1 4 1 7

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Meikel Brandmeyer
Hi, On 3 Apr., 13:18, Ken Wesson wrote: > "Less" involved? Your solution combines 5 sequences with drop-while-first-map-second magic which I personally don't find very self-explaining at first sight. My solution does one step with only local impact and then delegates to a single well-known libr

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Ken Wesson
On Sun, Apr 3, 2011 at 7:15 AM, Meikel Brandmeyer wrote: > Hi, > > On 3 Apr., 12:24, Ken Wesson 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 >

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Meikel Brandmeyer
Hi, On 3 Apr., 12:24, Ken Wesson 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 1

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Ken Wesson
On Sun, Apr 3, 2011 at 3:23 AM, Stefan Rohlfing wrote: > @ Ken, Andreas > > Thank you for your nice implementations! > > As far as I can see, there are two main methods of comparing adjacent items > of a list: > > 1) Take the first item of a coll. Compare the remaining items with an offset > of 1:

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Stefan Rohlfing
Hi Andreas, You are right, the helpful Clojure community is a real asset to be proud of! I like your solution using* loop ... recur* as it makes the intention of the code a bit more clear. The pattern (lazy-seq (when-let [s (seq coll)] ... is used in the implementation of* take-whil

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Andreas Kostler
Hi Stefan, I am overwhelmed by the 'freedom of choice' Clojure gives you, too. In that respect it's more like ruby than python. Nevertheless, I think if you can come up with an algorithm using the built in functions over sequences like map, reduce, filter, etc. you should do so. Either way, it

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Andreas Kostler
I can't answer that Ken, I guess I wasn't thinking of vec when I wrote it :) On 03/04/2011, at 5:17 PM, Ken Wesson wrote: > On Sun, Apr 3, 2011 at 1:04 AM, Andreas Kostler > wrote: >> (map (fn [x y] [x y]) coll (rest coll)) > > What's your reason for using (fn [x y] [x y])

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Stefan Rohlfing
@ Ken, Andreas Thank you for your nice implementations! As far as I can see, there are two main methods of comparing adjacent items of a list: 1) Take the first item of a coll. Compare the remaining items with an offset of 1: (map f coll (rest coll)) ;; apply some sort of filter 2) Take the f

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-03 Thread Ken Wesson
On Sun, Apr 3, 2011 at 1:04 AM, Andreas Kostler wrote: >                       (map (fn [x y] [x y]) coll (rest coll)) What's your reason for using (fn [x y] [x y]) here instead of just vector? Is it more efficient because it isn't variable-arity? -- You received this message because you are su

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-02 Thread Andreas Kostler
Another way to skin the cat: (defn take-by [f coll] (cons (first coll) (for [[x y] (map (fn [x y] [x y]) coll (rest coll)) :while (= (f x) (f y))] x))) Cheers Andreas On 03/04/2011, at 2:21 PM, Ken Wesson wrote: > On S

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-02 Thread Andreas Kostler
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-

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-02 Thread Andreas Kostler
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))

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-02 Thread Ken Wesson
On Sun, Apr 3, 2011 at 12:16 AM, Ken Wesson wrote: > On Sat, Apr 2, 2011 at 11:38 PM, Stefan Rohlfing > 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 tak

Re: (take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-02 Thread Ken Wesson
On Sat, Apr 2, 2011 at 11:38 PM, Stefan Rohlfing 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

(take-by f coll), (drop-by f coll): Problem Comparing Adjacent Items Of A Collection

2011-04-02 Thread Stefan Rohlfing
Dear Clojure Group, In the style of *take-while* and *drop-while* I wrote two functions* take-by * and *drop-by* that take an arbitrary function f instead of a predicate and return a lazy sequence based on comparing the result of (f item) of successive items in the collection. However, whenev