G
Gregory Brown
Hi folks,
I came across something I thought should have an obvious answer and
came up blank. I'm hoping folks could give me a nice solution to this
problem.
Given the output below:
I'd like to recover the original two arrays, something like
unzip(a) #=> [[1,2,3,4],[5,6,7,8]]
The best ordinary solution I could come up with was this:
b = a.each_with_object([[],[]]) do |pair, memo|
memo[0] << pair[0]
memo[1] << pair[1]
end
Shorter, but perhaps weirder, is:
require "matrix"
b = Matrix[*a].column_vectors.map(&:to_a)
Although either of these solutions are likely good enough for my
needs, neither feel natural to me. Anyone have something better?
-greg
I came across something I thought should have an obvious answer and
came up blank. I'm hoping folks could give me a nice solution to this
problem.
Given the output below:
=> [[1, 5], [2, 6], [3, 7], [4, 8]]a = [1,2,3,4].zip([5,6,7,8])
I'd like to recover the original two arrays, something like
unzip(a) #=> [[1,2,3,4],[5,6,7,8]]
The best ordinary solution I could come up with was this:
b = a.each_with_object([[],[]]) do |pair, memo|
memo[0] << pair[0]
memo[1] << pair[1]
end
Shorter, but perhaps weirder, is:
require "matrix"
b = Matrix[*a].column_vectors.map(&:to_a)
Although either of these solutions are likely good enough for my
needs, neither feel natural to me. Anyone have something better?
-greg