Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > RFC - Recursive Array Method call acting on element batches

Reply
Thread Tools

RFC - Recursive Array Method call acting on element batches

 
 
rubydeckard@gmail.com
Guest
Posts: n/a
 
      11-24-2007
I work for the Department of Redundancy Department and I'm thinking
there must be a better, more elegant way to doing the following:

With an array of 200 elements
Call a method to act on 10 elements at a time
Repeat until all elements are processed.
Return the processed array.

Here's my solution to the above:

__BEGIN__

class Array
def process(method_obj, batch_size = 10, fin = [])
if (this_batch = self.first(batch_size)).size > 0
fin.concat method_obj.call(this_batch)
(self - this_batch).process(method_obj, batch_size, fin)
else
return fin
end
end
end

def munge(ary)
ary.collect { |e| e.+ 10 }
end

my_meth = method(:munge)

ARY = [1,2,3,4,5,6]
p ARY.process(my_meth, 2) # run my_meth against 2 elements at a time

__END__

The above produces: [11, 12, 13, 14, 15, 16]

This is just a trivial example, but, for what I'm actually doing
there's a limit of 100 items that can be processed by this mysterious
(dynamic) method, hence the need to "batch" process the array.

Does my solution scream newb? Anyone have a suggestion that would
"own me with easy"?

Thanks in advance for any comments.

Ben
 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      11-24-2007
unknown wrote:
>
> With an array of 200 elements
> Call a method to act on 10 elements at a time
> Repeat until all elements are processed.
> Return the processed array.
>



Pre ruby 1.9:

require 'enumerator'

def sum(arr)
total = 0

arr.each {|elmt| total += elmt}
#faster than the ponderously slow inject method

total
end

results = []
arr = Array.new(15) {|i| i}
p arr

arr.each_slice(10) do |portion|
results << sum(portion)
end

p results

--output:--
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[45, 60]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
rubydeckard@gmail.com
Guest
Posts: n/a
 
      11-25-2007
Thanks for your input guys. That put me on the right track.

def proc_array(method_obj, batch_size = 2)
result = []
self.each_slice(batch_size) do |batch|
result.concat method_obj.call(batch)
end
result
end

That turns out to be twice as fast as the recursive method. The only
trouble I experienced was getting each_slice into my class. Turns out
that there's an error with the ruby core library documentation
produced by RDoc. #each_slice is available via the *standard* library
'enumerator.rb', not the core module library Enumerable as is
documented at www.ruby-doc.org. Once I figured that out, all is
peachy keen.

Thanks,

Ben
 
Reply With Quote
 
Eivind Eklund
Guest
Posts: n/a
 
      11-27-2007
On Nov 25, 2007 8:30 PM, <> wrote:
> Thanks for your input guys. That put me on the right track.
>
> def proc_array(method_obj, batch_size = 2)
> result = []
> self.each_slice(batch_size) do |batch|
> result.concat method_obj.call(batch)
> end
> result
> end


Better:
def proc_array(batch_size = 2, &method_obj)
result = []
self.each_slice(batch_size) do |batch|
result.concat method_obj.call(batch)
end
result
end

Can be called as

...proc_array do |batch|
end

or

...proc_array(2, &method_obj)

Eivind.

 
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
an XML-based format with semantics like RFC 5322 (RFC 822)? Ivan Shmakov XML 3 02-13-2012 04:26 PM
looping in batches graham C++ 5 03-25-2011 09:34 AM
Scanner for large batches of Slides Toby Digital Photography 6 09-08-2007 08:36 AM
Moving batches of files with Rake Phrogz Ruby 2 04-01-2007 01:19 AM
Full-size DVD cases in batches of 20 (price check) Bruce P. Burrell DVD Video 5 01-24-2006 01:56 AM



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