[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

convert array to array-of-arrays?

Phlip

2/4/2008 3:46:00 PM

Rubies:

Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?

I had figured a variation on Array#transpose would be available, but I can't
find one!

--
Phlip
13 Answers

Chris Hulan

2/4/2008 3:52:00 PM

0

On Feb 4, 10:45 am, Phlip <phlip2...@gmail.com> wrote:
> Rubies:
>
> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
>
> I had figured a variation on Array#transpose would be available, but I can't
> find one!
>
> --
> Phlip

have a look at enum.partition (http://ruby-doc.org/cor...
Enumerable.html#M003161)

cheers

Phlip

2/4/2008 4:01:00 PM

0

Chris Hulan wrote:

>> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?

> have a look at enum.partition (http://ruby-doc.org/cor...
> Enumerable.html#M003161)

I can't partition by value, so the closest conceptual match would be something like:

assert do
[1, 2, 3, 7].partition_with_index{|p,i| i.odd? }.transpose ==
[[1, 2], [3, 7]]
end

And that's a heckuva lot of typing, even if partition_with_index existed!

--
Phlip

Reacher

2/4/2008 4:18:00 PM

0

On Feb 4, 10:00 am, Phlip <phlip2...@gmail.com> wrote:
> Chris Hulan wrote:
> >> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
> > have a look at enum.partition (http://ruby-doc.org/cor...
> > Enumerable.html#M003161)
>
> I can't partition by value, so the closest conceptual match would be something like:
>
>     assert do
>        [1, 2, 3, 7].partition_with_index{|p,i| i.odd? }.transpose ==
>             [[1, 2], [3, 7]]
>     end
>
> And that's a heckuva lot of typing, even if partition_with_index existed!
>
> --
>    Phlip

Roll your own?

class Array
def regroup(count)
# do some Ruby awesomeness*
end
end

>> a = [1, 2, 3, 4]
[1, 2, 3, 4]
>> a.regroup(2)
[[1, 2], [3, 4]]

* - exercise left up to the reader

Robert Klemme

2/4/2008 4:20:00 PM

0

2008/2/4, Chris Hulan <chris.hulan@gmail.com>:
> On Feb 4, 10:45 am, Phlip <phlip2...@gmail.com> wrote:

> > Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
> >
> > I had figured a variation on Array#transpose would be available, but I can't
> > find one!
>
> have a look at enum.partition (http://ruby-doc.org/cor...
> Enumerable.html#M003161)

Not sure what the OP wants, but AFAIK #partition only ever returns two
partitions. If the task is to combine consecutive elements
#each_slice works well:

$ irb -r enumerator
irb(main):001:0> [1, 2, 3, 4].to_enum(:each_slice, 2).to_a
=> [[1, 2], [3, 4]]
irb(main):002:0>

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Chris Hulan

2/4/2008 4:27:00 PM

0

On Feb 4, 11:00 am, Phlip <phlip2...@gmail.com> wrote:
> Chris Hulan wrote:
> >> Given [1, 2, 3, 4], what's the most Rubiesque way to get [[1, 2], [3, 4]]?
> > have a look at enum.partition (http://ruby-doc.org/cor...
> > Enumerable.html#M003161)
>
> I can't partition by value, so the closest conceptual match would be something like:
>
> assert do
> [1, 2, 3, 7].partition_with_index{|p,i| i.odd? }.transpose ==
> [[1, 2], [3, 7]]
> end
>
> And that's a heckuva lot of typing, even if partition_with_index existed!
>
> --
> Phlip

Have a look at Facets.Enumerable.group_by (http://facets.ruby...
quick/rdoc/core/classes/Enumerable.html#M000423)
Lots of other neat stuff there...

Cheers

Phlip

2/4/2008 4:32:00 PM

0

Robert Klemme wrote:

> irb(main):001:0> [1, 2, 3, 4].to_enum(:each_slice, 2).to_a

Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how to write
to_enum in Ruby? I found each_slice, and I know C and ruby.h, but my Ruby
internals are rusty...

--
Phlip

Phlip

2/4/2008 4:46:00 PM

0

The winner is ActiveSupport's Array#in_groups_of (calling each_slice).

Thanks y'all!

But it only won for one reason - I need the array-of-twos so I can then
immediately call .each on it. in_groups_of already requires a block (and does
not return the twizzled array!), so I can just use it without the each. So I
would have called it each_group, but that didn't seem to catch on...

Here's why I need it. Ruby's opcodes express Hashes as strictly even-lengthed
arrays, going [key1, value1, key2, value2, etc]. So I just needed to iterate
those things in pairs, without excessive moduli to detect if the current item is
a key or a value.

--
Phlip

Chris Hulan

2/4/2008 4:50:00 PM

0

On Feb 4, 11:32 am, Phlip <phlip2...@gmail.com> wrote:
> Robert Klemme wrote:
> > irb(main):001:0> [1, 2, 3, 4].to_enum(:each_slice, 2).to_a
>
> Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how to write
> to_enum in Ruby? I found each_slice, and I know C and ruby.h, but my Ruby
> internals are rusty...
>
> --
> Phlip

Ruby-docs.org indicates Object.to_enum exists in latest 1.8.6, can you
upgrade?

Phlip

2/4/2008 4:56:00 PM

0

Chris Hulan wrote:

> Ruby-docs.org indicates Object.to_enum exists in latest 1.8.6, can you
> upgrade?

1.8.6 p111!

But here's the before and after on that refactor:

- array.each_with_index do |n, index|
- expression << _send(n)
- if (index % 2) == 0
- expression << ' => '
- elsif n != array.last
- expression << ', '
- end
- end

+ array.in_groups_of 2 do |key, value|
+ expression << _send(key) + ' => ' + _send(value)
+ expression << ', ' if value != array.last
+ end

Getting tight, huh? C-; Only one if to go...

Robert Klemme

2/4/2008 6:23:00 PM

0

On 04.02.2008 17:32, Phlip wrote:
> Robert Klemme wrote:
>
>> irb(main):001:0> [1, 2, 3, 4].to_enum(:each_slice, 2).to_a
>
> Thanks! Now, for those of us mired in the doldrums of Ruby 1.8.x, how to
> write to_enum in Ruby? I found each_slice, and I know C and ruby.h, but
> my Ruby internals are rusty...

AFAIK it is in *all* Ruby 1.8.* versions. You just need an extra
"require" (see my first posting in this thread).

Cheers

robert