1

I'm using Ruby 2.4. I'm trying to split elements in an array. What I would like is to form to equivalent arrays with the results of my split. I would like the first part of the split to be the first array and the second part of teh split to be the second array. So I have

data_col = ["mm a", "nn b", "nn a"]
arr1, arr2 = data_col.map do |x| 
  if x
    a, b, c = x.partition(/(^|[[:space:]]+)[ab]$/i)
    [b.strip, a + c] 
  else
      [nil, nil]
  end
end.transpose
 #=> [["a", "b", "a"], ["mm", "nn", "nn"]]

The problem is, the split is working, but the arrays are getting reversed. I want the ["mm", "nn", "nn"] to be the first element in the array. How do I rewrite things so that the arrays are returned properly -- that is, the first part of the split is in the first array and the second part of the split is in the second array?

4

2 回答 2

0
data_col = ["mm a", "nn b", "nn a"]
arr1, arr2 = data_col.map do |x| 
  a, b, c = x.partition(/(^|[[:space:]]+)[ab]$/i)
  [a + c, b.strip] # <=================== switched b.strip and a+b
end.transpose
#=> [["mm", "nn", "nn"], ["a", "b", "a"]]

I removed the if x condition, because it will not be possible to get false from if x in your mapping :)

于 2017-03-13T19:26:37.997 回答
0

I think you don't even need to use partition with regex to get the o/p, you can simply use map with split and transpose to get the same o/p and also it's very fast when compared to partition with regex. Below is the simple benchmark for 5 seconds,

require 'benchmark/ips'

data_col = ["mm a", "nn b", "nn a"]

Benchmark.ips do |x|
  x.config(time: 5, warmup: 2)
  x.report('REGEXP') do
    arr1, arr2 = data_col.map do |xx|
      a, b, c = xx.partition(/(^|[[:space:]]+)[ab]$/i)
      [a + c, b.strip]
    end.transpose 
  end

  x.report('MAP SPLIT') do
    arr1, arr2 = data_col.map do |xx|
      xx.split(' ').map(&:strip)
    end.transpose
  end
  x.compare!
end

And below is the IPS comparision,

Warming up --------------------------------------
              REGEXP    16.985k i/100ms
           MAP SPLIT    26.771k i/100ms
Calculating -------------------------------------
              REGEXP    190.220k (± 4.5%) i/s -    951.160k in   5.012963s
           MAP SPLIT    303.243k (± 3.5%) i/s -      1.526M in   5.040226s

Comparison:
           MAP SPLIT:   303243.1 i/s
              REGEXP:   190219.6 i/s - 1.59x  slower

You can see the map with split is executing 303243.1 i/s where partition with regex is executing 190219.6 i/s. Hence, the map with split is 1.59x faster than partition with regex.

于 2017-03-13T20:55:43.440 回答