RFC - Recursive Array Method call acting on element batches

Discussion in 'Ruby' started by rubydeckard@gmail.com, Nov 24, 2007.

  1. Guest

    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
     
    , Nov 24, 2007
    #1
    1. Advertising

  2. 7stud -- Guest

    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/.
     
    7stud --, Nov 24, 2007
    #2
    1. Advertising

  3. Guest

    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
     
    , Nov 25, 2007
    #3
  4. 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.
     
    Eivind Eklund, Nov 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Magnusson

    Unused method is acting up

    Martin Magnusson, Nov 19, 2003, in forum: C++
    Replies:
    2
    Views:
    282
    Victor Bazarov
    Nov 19, 2003
  2. Donnal Walter
    Replies:
    12
    Views:
    499
    Donnal Walter
    Oct 18, 2004
  3. graham

    looping in batches

    graham, Mar 24, 2011, in forum: C++
    Replies:
    5
    Views:
    278
    Michael Doubez
    Mar 25, 2011
  4. Ivan Shmakov
    Replies:
    3
    Views:
    1,154
    Kari Hurtta
    Feb 13, 2012
  5. Phrogz
    Replies:
    2
    Views:
    115
    Jim Weirich
    Apr 1, 2007
Loading...

Share This Page