Transpose

This occurred to me while lying sick in bed...

robert@fussel /cygdrive/c/Temp
$ ruby dd.rb
[["foo", "bar", "baz", "biz"], [1, 2, 3, 4], [100, 200, 300, 400]]
[["foo", 1, 100], ["bar", 2, 200], ["baz", 3, 300], ["biz", 4, 400]]

robert@fussel /cygdrive/c/Temp
$ cat dd.rb

require 'enumerator'
require 'pp'

def transpose(a)
   a.inject {|aa,b| aa.to_enum(:zip, b)}.inject([]){|aa,b| aa << b.flatten}
end

a = [
   %w{foo bar baz biz},
   [1,2,3,4],
   [100,200,300,400],
]

pp a, transpose(a)

Did we have this before?

Whatever...

  robert

a = [
   %w{foo bar baz biz},
   [1,2,3,4],
   [100,200,300,400],
]

a.transpose

Blessings,
TwP

···

On 1/31/07, Robert Klemme <shortcutter@googlemail.com> wrote:

This occurred to me while lying sick in bed...

robert@fussel /cygdrive/c/Temp
$ ruby dd.rb
[["foo", "bar", "baz", "biz"], [1, 2, 3, 4], [100, 200, 300, 400]]
[["foo", 1, 100], ["bar", 2, 200], ["baz", 3, 300], ["biz", 4, 400]]

robert@fussel /cygdrive/c/Temp
$ cat dd.rb

require 'enumerator'
require 'pp'

def transpose(a)
   a.inject {|aa,b| aa.to_enum(:zip, b)}.inject(){|aa,b| aa << b.flatten}
end

a = [
   %w{foo bar baz biz},
   [1,2,3,4],
   [100,200,300,400],
]

pp a, transpose(a)

Did we have this before?

Whatever...

Robert,

It's actually a built in method on array.

a.transpose will get you the same result.
Nice solution, however.

--Tyler

···

On 1/31/07, Robert Klemme <shortcutter@googlemail.com> wrote:

This occurred to me while lying sick in bed...

robert@fussel /cygdrive/c/Temp
$ ruby dd.rb
[["foo", "bar", "baz", "biz"], [1, 2, 3, 4], [100, 200, 300, 400]]
[["foo", 1, 100], ["bar", 2, 200], ["baz", 3, 300], ["biz", 4, 400]]

robert@fussel /cygdrive/c/Temp
$ cat dd.rb

require 'enumerator'
require 'pp'

def transpose(a)
   a.inject {|aa,b| aa.to_enum(:zip, b)}.inject(){|aa,b| aa << b.flatten
}
end

a = [
   %w{foo bar baz biz},
   [1,2,3,4],
   [100,200,300,400],
]

pp a, transpose(a)

Did we have this before?

Whatever...

        robert

__sick__ being the operative word!

:wink:

-a

···

On Thu, 1 Feb 2007, Robert Klemme wrote:

This occurred to me while lying sick in bed...

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama