Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Running multiple closures per iteration

Reply
Thread Tools

Running multiple closures per iteration

 
 
Joshua Chia
Guest
Posts: n/a
 
      08-03-2007
Is there a way to run more than one closure per iteration through a
collection? For example, if I want to do the following two things, can
I combine them so that I only end up with one pass over the collection,
without doing a manual for loop?

o = a.select {|x| x >= 0}
p = a.inject(0) {|s, x| s += x}

Such a combination is useful when iterating through a collection is
expensive, e.g. on a large file on a slow disk.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-03-2007
Joshua Chia wrote:
> Is there a way to run more than one closure per iteration through a
> collection? For example, if I want to do the following two things, can
> I combine them so that I only end up with one pass over the collection,
> without doing a manual for loop?
>
> o = a.select {|x| x >= 0}
> p = a.inject(0) {|s, x| s += x}
>
> Such a combination is useful when iterating through a collection is
> expensive, e.g. on a large file on a slow disk.



Did you mean

o = a.select {|x| x >= 0}
p = o.inject(0) {|s, x| s += x}
^^^
?

Then you can do this:

a = [1,-1,2,-2,3,-3]
sp = a.inject(0) {|s, x| x >= 0 ? s + x : s}
p sp # ==> 6

(note that += is not needed, + is good enough)

But there is no way in general to combine two iterations.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
 
 
 
Joshua Chia
Guest
Posts: n/a
 
      08-03-2007
Joel VanderWerf wrote:
> Did you mean
>
> o = a.select {|x| x >= 0}
> p = o.inject(0) {|s, x| s += x}
> ^^^
> ?
>


No, I meant to compute two different things from a.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Tim Pease
Guest
Posts: n/a
 
      08-03-2007
On 8/3/07, Joshua Chia <> wrote:
> Joel VanderWerf wrote:
> > Did you mean
> >
> > o = a.select {|x| x >= 0}
> > p = o.inject(0) {|s, x| s += x}
> > ^^^
> > ?
> >

>
> No, I meant to compute two different things from a.


Well, it's not pretty, but here's my best shot at it

o, p = a.inject( [[], 0] ) do |ary,x|
ary[0] << x if x >= 0
ary[1] += x
ary
end

I don't think Ruby natively supports such a creature, though. Please
correct me if I'm wrong.

Blessings,
TwP

 
Reply With Quote
 
Stefano Cobianchi
Guest
Posts: n/a
 
      08-03-2007
On Aug 3, 7:38 pm, Joshua Chia <joshc...@gmail.com> wrote:
> Is there a way to run more than one closure per iteration through a
> collection? For example, if I want to do the following two things, can
> I combine them so that I only end up with one pass over the collection,
> without doing a manual for loop?
>
> o = a.select {|x| x >= 0}
> p = a.inject(0) {|s, x| s += x}


btw, you don't need the += operator here:
a.inject(0) {|s, x| s + x}
=> 6

> Such a combination is useful when iterating through a collection is
> expensive, e.g. on a large file on a slow disk.


o = 0; p = []
a.each { |x| o << x if x >= 0; p += x }

--
S.

 
Reply With Quote
 
dblack@rubypal.com
Guest
Posts: n/a
 
      08-03-2007
Hi --

On Sat, 4 Aug 2007, Stefano Cobianchi wrote:

> On Aug 3, 7:38 pm, Joshua Chia <joshc...@gmail.com> wrote:
>> Is there a way to run more than one closure per iteration through a
>> collection? For example, if I want to do the following two things, can
>> I combine them so that I only end up with one pass over the collection,
>> without doing a manual for loop?
>>
>> o = a.select {|x| x >= 0}
>> p = a.inject(0) {|s, x| s += x}

>
> btw, you don't need the += operator here:
> a.inject(0) {|s, x| s + x}
> => 6
>
>> Such a combination is useful when iterating through a collection is
>> expensive, e.g. on a large file on a slow disk.

>
> o = 0; p = []
> a.each { |x| o << x if x >= 0; p += x }

a.each { |x| p << x if x >= 0; o += x }




David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Simon Kröger
Guest
Posts: n/a
 
      08-04-2007
Tim Pease schrieb:
> On 8/3/07, Joshua Chia <> wrote:
>> Joel VanderWerf wrote:
>>> Did you mean
>>>
>>> o = a.select {|x| x >= 0}
>>> p = o.inject(0) {|s, x| s += x}
>>> ^^^
>>> ?
>>>

>> No, I meant to compute two different things from a.

>
> Well, it's not pretty, but here's my best shot at it
>
> o, p = a.inject( [[], 0] ) do |ary,x|
> ary[0] << x if x >= 0
> ary[1] += x
> ary
> end


you can beautify this to

o, p = a.inject( [[], 0] ) do |(l, s), x|
[x >= 0 ? l << x : l, s + x]
end

> I don't think Ruby natively supports such a creature, though. Please
> correct me if I'm wrong.


Well, depends on what 'natively' realy means...

> Blessings,
> TwP


cheers

Simon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Per packet vs per flow routing R Siffredi Cisco 8 03-24-2005 06:33 PM
Per-to-Per is OK but no ICS Jens Mander Wireless Networking 4 01-23-2005 10:24 PM
Should i select per device or per server to do all exercises? ADRENALINE MCSE 20 01-22-2004 09:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57