Re: take-while2?

2010-08-08 Thread bOR_
Nice! Ran into a blogpost yesterday of someone calculating 'e', and I was fiddling around after reading that with take-while looking for a way to do exactly this. user (time (/ (apply + (repeatedly 1000 (fn [] (inc (count (take- while-acc + #( % 1) (repeatedly #(rand 1000.0))

Re: take-while2?

2010-08-08 Thread Steve Purcell
On 8 Aug 2010, at 04:56, Michał Marczyk wrote: Yet another version: (defn take-while-acc [f pred coll] (map (fn [_ x] x) (take-while pred (reductions f coll)) coll)) Seems to work: user (take-while-acc + #( % 100) (range)) (0 1 2 3 4 5 6 7 8 9 10 11 12 13) Delightful

Re: take-while2?

2010-08-08 Thread Andreas Liljeqvist
That's succinct :) I haven't tested it against Meikels version which seems to be based upon take-while. However I think this functionality would be nice to have in core or contrib, how do one propose it? Thanks for all contributing to this thread. btw bOR_: I was reading the same blogpost :)

Re: take-while2?

2010-08-08 Thread gary ng
On Sat, Aug 7, 2010 at 8:56 PM, Michał Marczyk michal.marc...@gmail.com wrote: Yet another version: (defn take-while-acc [f pred coll]  (map (fn [_ x] x)       (take-while pred (reductions f coll))       coll)) Seems to work: user (take-while-acc + #( % 100) (range)) (0 1 2 3 4 5 6 7 8

take-while2?

2010-08-07 Thread bonega
Hi. Are there some function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100) (iterate inc 0)) returns: (0 1 2 3 4 5 6 7 8 9 10 11 12 13) -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email

Re: take-while2?

2010-08-07 Thread Steve Purcell
On 7 Aug 2010, at 11:15, bonega wrote: Hi. Are there some function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100) (iterate inc 0)) returns: (0 1 2 3 4 5 6 7 8 9 10 11 12 13) I'm feeling a bit stupid because I can't see from the above example how take

Re: take-while2?

2010-08-07 Thread Andreas Liljeqvist
function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100) (iterate inc 0)) returns: (0 1 2 3 4 5 6 7 8 9 10 11 12 13) I'm feeling a bit stupid because I can't see from the above example how take-while2 is supposed to work. Can you clarify please? -Steve

Re: take-while2?

2010-08-07 Thread Steve Purcell
passed to pred. This examples takes elements while their total sum is less than 100. 2010/8/7 Steve Purcell st...@sanityinc.com On 7 Aug 2010, at 11:15, bonega wrote: Hi. Are there some function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100

Re: take-while2?

2010-08-07 Thread Joop Kiefte
. Are there some function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100) (iterate inc 0)) returns: (0 1 2 3 4 5 6 7 8 9 10 11 12 13) I'm feeling a bit stupid because I can't see from the above example how take-while2 is supposed to work. Can you clarify please

Re: take-while2?

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 8:14 AM, Steve Purcell st...@sanityinc.com wrote: Oh, right, so maybe: (last (take-while #( (apply + %) 100) (reductions conj [] (iterate inc 0 = [0 1 2 3 4 5 6 7 8 9 10 11 12 13] -Steve or user= (map second (take-while (fn [e] ( (first e) 100)) (rest (reductions

Re: take-while2?

2010-08-07 Thread Steve Purcell
On 7 Aug 2010, at 20:23, gary ng wrote: On Sat, Aug 7, 2010 at 8:14 AM, Steve Purcell st...@sanityinc.com wrote: Oh, right, so maybe: (last (take-while #( (apply + %) 100) (reductions conj [] (iterate inc 0 = [0 1 2 3 4 5 6 7 8 9 10 11 12 13] -Steve or user= (map second

Re: take-while2?

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 12:46 PM, Steve Purcell st...@sanityinc.com wrote: Nice - that's about twice as fast as my version (with the 100 limit scaled up to 1 million), though perhaps a less general pattern since the code structure assumes knowledge of +'s cumulative nature. Yes, it needs a

Re: take-while2?

2010-08-07 Thread Andreas Liljeqvist
On 7 Aug 2010, at 11:15, bonega wrote: Hi. Are there some function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100) (iterate inc 0)) returns: (0 1 2 3 4 5 6 7 8 9 10 11 12 13) I'm feeling a bit stupid because I can't see from the above example how take

Re: take-while2?

2010-08-07 Thread Meikel Brandmeyer
Hi, Am 07.08.2010 um 19:44 schrieb Andreas Liljeqvist: Your example code can be quite slow since all sublists are summed before comparision. I would like a function that does this with a accumulator for the reduced values. You can always go low-level: (defn take-until [f initial pred

Re: take-while2?

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 10:44 AM, Andreas Liljeqvist bon...@gmail.com wrote: Thanks that works. What I really want is a function like in my initial posting. Is there something like that in core or contrib? Your example code can be quite slow since all sublists are summed before comparision. I

Re: take-while2?

2010-08-07 Thread Randy Hudson
to pred. This examples takes elements while their total sum is less than 100. 2010/8/7 Steve Purcell st...@sanityinc.com On 7 Aug 2010, at 11:15, bonega wrote: Hi. Are there some function like this: (defn take-while2 [f pred coll] ... usage: (take-while2 + #( % 100) (iterate

Re: take-while2?

2010-08-07 Thread Michał Marczyk
Yet another version: (defn take-while-acc [f pred coll] (map (fn [_ x] x) (take-while pred (reductions f coll)) coll)) Seems to work: user (take-while-acc + #( % 100) (range)) (0 1 2 3 4 5 6 7 8 9 10 11 12 13) Note that reductions does use an accumulator (in the form of a

Re: take-while2?

2010-08-07 Thread Randy Hudson
Nice! On Aug 7, 11:56 pm, Michał Marczyk michal.marc...@gmail.com wrote: Yet another version: (defn take-while-acc [f pred coll]   (map (fn [_ x] x)        (take-while pred (reductions f coll))        coll)) Seems to work: user (take-while-acc + #( % 100) (range)) (0 1 2 3 4 5 6 7 8 9