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))
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
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 :)
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
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
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
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
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
.
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
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
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
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
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
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
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
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
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
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
18 matches
Mail list logo