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]]
On Tue, Sep 6, 2011 at 11:47 PM, Intransition [email protected]
wrote:
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:
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.
On Sep 6, 7:52pm, “Aaron D. Gifford” [email protected] wrote:
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.
Ah, of course. Thanks.
may be you could write
On Wed, Sep 7, 2011 at 1:18 PM, Intransition [email protected]
wrote:
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 Thu, Sep 8, 2011 at 10:12 AM, rahul patil
[email protected]wrote:
irb(main):001:0> arr = [0,1,2,3,4]
def to_range puts "Fixnum methid"
sorry, just remove this puts !!!
On Wed, Sep 7, 2011 at 12:47 AM, Intransition [email protected]
wrote:
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 Thu, Sep 8, 2011 at 8:54 PM, Michael K. [email protected]
wrote:
On Wed, Sep 7, 2011 at 12:47 AM, Intransition [email protected] wrote:
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 Fri, Sep 9, 2011 at 1:15 PM, rahul patil
[email protected] wrote:
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]]
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs