Re: partition-starting-every : yet another partition function

2010-09-17 Thread gary ng
On Fri, Sep 17, 2010 at 10:49 AM, Nicolas Oury wrote: > I was just saying that not returning something that is a pair, for > example nil, is good enough. The implement is equivalent, most languages I know that has unfold use your approach(i.e. return Maybe or None). This link use the unfold p f

Re: partition-starting-every : yet another partition function

2010-09-17 Thread Nicolas Oury
(defn unfold ([grow seed] (lazy-seq (if-let [[elt next-seed] (grow seed)] (cons elt (unfold grow next-seed) ([grow finished? seed] (unfold #(when (not (finished? %)) (grow %)) seed))) (unfold (fn [x] [(* x x) (inc x)]) #(> % 10) 0) (0 1 4 9 16 25 36 49 64 81 100) (unfol

Re: partition-starting-every : yet another partition function

2010-09-17 Thread Nicolas Oury
I was just saying that not returning something that is a pair, for example nil, is good enough. (unfold (fn [x] (when (<= x 10) [(* x x) (inc x)])) would work. Both function can be written with each other anyway. And they don't have the same number of args so they are compatible with each othe

Re: partition-starting-every : yet another partition function

2010-09-17 Thread Gijs S.
Finished is a predicate which designates when the seed is exhausted. Because seed is not necessary a sequence, finished is not always empty?. For instance: => (unfold (fn [x] [(* x x) (inc x)]) #(> % 10) 0) (0 1 4 9 16 25 36 49 64 81 100) Or the zipmap (zip2) example from the wikipedia pa

Re: partition-starting-every : yet another partition function

2010-09-16 Thread Thomas
I agree with number 2: it would be very nice to have this in contrib. I needed it last month and rolled my own (less clean) version. Thomas On Sep 10, 10:26 pm, Matt Smith wrote: > problem: convert a collection [1 2 0 1 2 3 0 1 2 3 0 0 1 2] into > partitions like: > ((1 2) (0 1 2 3) (0 1 2 3) (

Re: partition-starting-every : yet another partition function

2010-09-16 Thread Christophe Grand
On Thu, Sep 16, 2010 at 10:45 PM, Chouser wrote: > On Fri, Sep 10, 2010 at 4:26 PM, Matt Smith wrote: > > problem: convert a collection [1 2 0 1 2 3 0 1 2 3 0 0 1 2] into > > partitions like: > > ((1 2) (0 1 2 3) (0 1 2 3) (0) (0 1 2)) > > In this case, start each partition on a 0. > > Iteratio

Re: partition-starting-every : yet another partition function

2010-09-16 Thread Chouser
On Fri, Sep 10, 2010 at 4:26 PM, Matt Smith wrote: > problem: convert a collection [1 2 0 1 2 3 0 1 2 3 0 0 1 2] into > partitions like: > ((1 2) (0 1 2 3) (0 1 2 3) (0) (0 1 2)) > In this case, start each partition on a 0. > > > I looked at the various partition functions but none of them would

Re: partition-starting-every : yet another partition function

2010-09-16 Thread Nicolas Oury
I think that unfold (or co-reduce, or generate) should find its way in contrib. I am not sure we need finished arg though. The traditional finish in the seq family is nil. My own version of unfold: (defn unfold "(unfold seed grow) use the seed and a function grow that returns an element and an

Re: partition-starting-every : yet another partition function

2010-09-16 Thread Gijs S.
This answers neither question 1 nor 2, but there is a function from the map, filter and reduce family of higher order functions that is applicable to this problem. The function is called unfold and is the opposite of reduce (see http://en.wikipedia.org/wiki/Anamorphism). "An unfold builds a (poten