Grouping an array into sub-arrays

Is there a method out there already that does anything like this?

    [0,1,2,3,4].group(0..-2, -1) #=> [[0,1,2,3]. [4]]
    [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

Is there a method out there already that does anything like this?

   [0,1,2,3,4].group(0..-2, -1) #=> [[0,1,2,3]. [4]]
   [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

Here's one way, if you only want one split and your condition is not always
this trivial:

[1,2,3,4,5].partition.with_index { |e, i| i < 2 }
[[1, 2], [3, 4, 5]]

For instance:

[1,2,3,4,5].partition { |e| e % 2 == 0 }

=> [[2, 4], [1, 3, 5]]

For other cases, the simplest is probably:

···

On Tue, Sep 6, 2011 at 11:47 PM, Intransition <transfire@gmail.com> wrote:

ary = [1,2,3,4,5]
[ary[0], ary[2..3], ary[4..-1]]

Is there a method out there already that does anything like this?

[0,1,2,3,4].group(0..-2, -1) #=> [[0,1,2,3]. [4]]
[0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

I guess I would just do something like this:

class Array
  def group(*ranges)
     ranges.map { |r| values_at(r) }
   end
end

[0,1,2,3,4].group(0..-2, -1)

=> [[0, 1, 2, 3], [4]]

[0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

=> [[0], [2, 3], [4]]

···

On Wed, Sep 7, 2011 at 12:47 AM, Intransition <transfire@gmail.com> wrote:

Just move things around a bit and you'll notice you've already got the
behavior you want (or pretty close):

irb(main):001:0> arr = [0,1,2,3,4]
=> [0, 1, 2, 3, 4]
irb(main):002:0> [0..-2, -1].map{|i| arr[i]}
=> [[0, 1, 2, 3], 4]
irb(main):003:0> [0, 2..3, 4..-1].map{|i| arr[i]}
=> [0, [2, 3], [4]]

So just put the list of group selectors first, execute a map/collect
on it and access the original array slices you want.

Aaron out.

> Is there a method out there already that does anything like this?
>
> [0,1,2,3,4].group(0..-2, -1) #=> [[0,1,2,3]. [4]]
> [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

I guess I would just do something like this:

yes it does works vey well, nice code snippet.

class Array
def group(*ranges)
    ranges.map { |r| values_at(r) }
  end
end

>> [0,1,2,3,4].group(0..-2, -1)
=> [[0, 1, 2, 3], [4]]
>> [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

can we have something so that if we pass array variable name instead of
actual elements of array like 0, 2..3, 4..-1
i mean if range=[ 0, 2..3, 4..-1], array= [0,1,2,3,4]
then array.group(range), should give same o/p

=> [[0], [2, 3], [4]]

···

On Thu, Sep 8, 2011 at 8:54 PM, Michael Kohl <citizen428@gmail.com> wrote:

On Wed, Sep 7, 2011 at 12:47 AM, Intransition <transfire@gmail.com> wrote:

--
Regards,
Rahul Patil

Ah, of course. Thanks.

···

On Sep 6, 7:52 pm, "Aaron D. Gifford" <astound...@gmail.com> wrote:

Just move things around a bit and you'll notice you've already got the
behavior you want (or pretty close):

irb(main):001:0> arr = [0,1,2,3,4]
=> [0, 1, 2, 3, 4]
irb(main):002:0> [0..-2, -1].map{|i| arr[i]}
=> [[0, 1, 2, 3], 4]
irb(main):003:0> [0, 2..3, 4..-1].map{|i| arr[i]}
=> [0, [2, 3], [4]]

So just put the list of group selectors first, execute a map/collect
on it and access the original array slices you want.

Aaron out.

try refining Michael's code.
eg,

class Array
  def group *ranges
    ranges.flatten.map{|r| values_at r}
  end
end
#=> nil

[0,1,2,3,4].group 0,2..3,4..-1
#=> [[0], [2, 3], [4]]
[0,1,2,3,4].group [0,2..3,4..-1]
#=> [[0], [2, 3], [4]]
[0,1,2,3,4].group r=[0,2..3,4..-1]
#=> [[0], [2, 3], [4]]
[0,1,2,3,4].group r
#=> [[0], [2, 3], [4]]
[0,1,2,3,4].group *r
#=> [[0], [2, 3], [4]]

best regards -botp

···

On Fri, Sep 9, 2011 at 1:15 PM, rahul patil <rahul.deshmukhpatil@gmail.com> wrote:

On Thu, Sep 8, 2011 at 8:54 PM, Michael Kohl <citizen428@gmail.com> wrote:

class Array
def group(*ranges)
ranges.map { |r| values_at(r) }
end
end
>> [0,1,2,3,4].group(0..-2, -1)
=> [[0, 1, 2, 3], [4]]
>> [0,1,2,3,4].group(0, 2..3, 4..-1) #=> [[0], [2,3], [4]]

can we have something so that if we pass array variable name instead of
actual elements of array like 0, 2..3, 4..-1
i mean if range=[ 0, 2..3, 4..-1], array= [0,1,2,3,4]
then array.group(range), should give same o/p

=> [[0], [2, 3], [4]]

may be you could write

> Just move things around a bit and you'll notice you've already got the
> behavior you want (or pretty close):
>
> irb(main):001:0> arr = [0,1,2,3,4]
> => [0, 1, 2, 3, 4]
> irb(main):002:0> [0..-2, -1].map{|i| arr[i]}
> => [[0, 1, 2, 3], 4]
> irb(main):003:0> [0, 2..3, 4..-1].map{|i| arr[i]}
> => [0, [2, 3], [4]]
>
> So just put the list of group selectors first, execute a map/collect
> on it and access the original array slices you want.
>
> Aaron out.

just modified Aaron code

class Range
    def to_range
        self
    end
end

class Fixnum
    def to_range
        puts "Fixnum methid"
        a=Range.new(self, self)
    end
end
range=[0, 2..3, 4..-1, 2..2]
array=[0,1,2,3,4]
puts "#{range.map {|id| id=id.to_range; array[id]}}"

How does it looks? it gives same o/p as urs !!!

···

On Wed, Sep 7, 2011 at 1:18 PM, Intransition <transfire@gmail.com> wrote:

On Sep 6, 7:52 pm, "Aaron D. Gifford" <astound...@gmail.com> wrote:

Ah, of course. Thanks.

--
Regards,
Rahul Patil

may be you could write

> Just move things around a bit and you'll notice you've already got the
> behavior you want (or pretty close):
>
> irb(main):001:0> arr = [0,1,2,3,4]
> => [0, 1, 2, 3, 4]
> irb(main):002:0> [0..-2, -1].map{|i| arr[i]}
> => [[0, 1, 2, 3], 4]
> irb(main):003:0> [0, 2..3, 4..-1].map{|i| arr[i]}
> => [0, [2, 3], [4]]
>
> So just put the list of group selectors first, execute a map/collect
> on it and access the original array slices you want.
>
> Aaron out.

just modified Aaron code

class Range
    def to_range
        self
    end
end

class Fixnum
    def to_range
        puts "Fixnum methid"

sorry, just remove this puts !!!

···

On Thu, Sep 8, 2011 at 10:12 AM, rahul patil <rahul.deshmukhpatil@gmail.com>wrote:

On Wed, Sep 7, 2011 at 1:18 PM, Intransition <transfire@gmail.com> wrote:

On Sep 6, 7:52 pm, "Aaron D. Gifford" <astound...@gmail.com> wrote:

        a=Range.new(self, self)
    end
end
range=[0, 2..3, 4..-1, 2..2]
array=[0,1,2,3,4]
puts "#{range.map {|id| id=id.to_range; array[id]}}"

How does it looks? it gives same o/p as urs !!!

Ah, of course. Thanks.

--
Regards,
Rahul Patil

--
Regards,
Rahul Patil