Ruby:在Ruby中优雅的数组初始化和返回

时间:2021-09-18 22:04:13

I have a method:

我有一个方法:

def deltas_to_board_locations(deltas, x, y)
    board_coords = []
    deltas.each_slice(2) do |slice|
      board_coords << x + slice[0] 
      board_coords << y + slice[1]
    end
    board_coords
  end 

where deltas is an array, and x,y are fixnums.

其中deltas是一个数组,x,y是fixnums。

Is there a way to eliminate the first and last line to make the method more elegant?

有没有办法消除第一行和最后一行,使方法更优雅?

Like:

def deltas_to_board_locations(deltas, x, y)
    deltas.each_slice(2) do |slice|
      board_coords << x + slice[0] 
      board_coords << y + slice[1]
    end
  end 

3 个解决方案

#1


4  

deltas.each_slice(2).flat_map { |dx, dy|
  [x + dx, y + dy]
}

The above works for Ruby 1.9 , but I agree with Renaud. The obvious solution is to be preferred, and in this case is faster than mine, too.

以上适用于Ruby 1.9,但我同意Renaud。显而易见的解决方案是首选,在这种情况下也比我的快。

Edit: Incorporated @tokland's comments.

编辑:纳入@tokland的评论。

#2


7  

deltas.each_slice(2).flat_map do |dx, dy|
  [x + dx, y + dy]
end

#3


6  

deltas.each_with_index.map { |val, idx| val + (idx % 2 == 0 ? x : y )}

Whether or not this is "less complex" depends on the audience.

这是否“不那么复杂”取决于观众。


Reduction of duplication and complexity should focus on macro-behavior rather than micro-refactoring short, already-readable methods.

减少重复和复杂性应该侧重于宏观行为,而不是微观重构短的,已经可读的方法。

Will this rewrite lead to a quantifiably easier-to-understand system? Or are there more important, higher-level issues?

这种重写是否会导致量化的易于理解的系统?还是有更重要,更高层次的问题?

Would enhancing app, class, and method documentation be better? Should those docs be in the code, or in a wiki? Would a picture be worth a thousand lines?

增强应用程序,类和方法文档会更好吗?这些文档应该在代码中还是在wiki中?一张图片会值一千行吗?


Performance comparison vs. @tokland's (his wins by a significant amount). Assuming deltas is a million-element array 1-1m. MRI, Ubuntu, old pokey machine.

与@ tokland相比的表现比较(他的胜利数量很大)。假设增量是一个百万元素阵列1-1m。 MRI,Ubuntu,老pokey机器。

My version

deltas.each_with_index.map { |val, idx| val + (idx % 2 == 0 ? x : y )}

Total: 1.764807

 %self     total     self     wait    child    calls  name
100.00      1.76     1.76     0.00     0.00        1  Array#each
  0.00      1.76     0.00     0.00     1.76        1  Global#[No method]
  0.00      1.76     0.00     0.00     1.76        2  Enumerable#each_with_index
  0.00      1.76     0.00     0.00     1.76        1  Enumerable#map
  0.00      1.76     0.00     0.00     1.76        1  Enumerator#each

Better, shorter, more communicative version

更好,更短,更具沟通性的版本

deltas.each_slice(2).flat_map { |dx, dy| [x + dx, y + dy] }

Total: 1.236144

 %self     total     self     wait    child    calls  name
100.00      1.24     1.24     0.00     0.00        1  Array#each
  0.00      1.24     0.00     0.00     1.24        1  Global#[No method]
  0.00      1.24     0.00     0.00     1.24        2  Enumerable#each_slice
  0.00      1.24     0.00     0.00     1.24        1  Enumerable#flat_map
  0.00      1.24     0.00     0.00     1.24        1  Enumerator#each

Original version (fastest):

原始版本(最快):

Total: 0.899122

 %self     total     self     wait    child    calls  name
100.00      0.90     0.90     0.00     0.00        1  Array#each
  0.00      0.90     0.00     0.00     0.90        1  Global#[No method]
  0.00      0.90     0.00     0.00     0.90        1  Enumerable#each_slice

#1


4  

deltas.each_slice(2).flat_map { |dx, dy|
  [x + dx, y + dy]
}

The above works for Ruby 1.9 , but I agree with Renaud. The obvious solution is to be preferred, and in this case is faster than mine, too.

以上适用于Ruby 1.9,但我同意Renaud。显而易见的解决方案是首选,在这种情况下也比我的快。

Edit: Incorporated @tokland's comments.

编辑:纳入@tokland的评论。

#2


7  

deltas.each_slice(2).flat_map do |dx, dy|
  [x + dx, y + dy]
end

#3


6  

deltas.each_with_index.map { |val, idx| val + (idx % 2 == 0 ? x : y )}

Whether or not this is "less complex" depends on the audience.

这是否“不那么复杂”取决于观众。


Reduction of duplication and complexity should focus on macro-behavior rather than micro-refactoring short, already-readable methods.

减少重复和复杂性应该侧重于宏观行为,而不是微观重构短的,已经可读的方法。

Will this rewrite lead to a quantifiably easier-to-understand system? Or are there more important, higher-level issues?

这种重写是否会导致量化的易于理解的系统?还是有更重要,更高层次的问题?

Would enhancing app, class, and method documentation be better? Should those docs be in the code, or in a wiki? Would a picture be worth a thousand lines?

增强应用程序,类和方法文档会更好吗?这些文档应该在代码中还是在wiki中?一张图片会值一千行吗?


Performance comparison vs. @tokland's (his wins by a significant amount). Assuming deltas is a million-element array 1-1m. MRI, Ubuntu, old pokey machine.

与@ tokland相比的表现比较(他的胜利数量很大)。假设增量是一个百万元素阵列1-1m。 MRI,Ubuntu,老pokey机器。

My version

deltas.each_with_index.map { |val, idx| val + (idx % 2 == 0 ? x : y )}

Total: 1.764807

 %self     total     self     wait    child    calls  name
100.00      1.76     1.76     0.00     0.00        1  Array#each
  0.00      1.76     0.00     0.00     1.76        1  Global#[No method]
  0.00      1.76     0.00     0.00     1.76        2  Enumerable#each_with_index
  0.00      1.76     0.00     0.00     1.76        1  Enumerable#map
  0.00      1.76     0.00     0.00     1.76        1  Enumerator#each

Better, shorter, more communicative version

更好,更短,更具沟通性的版本

deltas.each_slice(2).flat_map { |dx, dy| [x + dx, y + dy] }

Total: 1.236144

 %self     total     self     wait    child    calls  name
100.00      1.24     1.24     0.00     0.00        1  Array#each
  0.00      1.24     0.00     0.00     1.24        1  Global#[No method]
  0.00      1.24     0.00     0.00     1.24        2  Enumerable#each_slice
  0.00      1.24     0.00     0.00     1.24        1  Enumerable#flat_map
  0.00      1.24     0.00     0.00     1.24        1  Enumerator#each

Original version (fastest):

原始版本(最快):

Total: 0.899122

 %self     total     self     wait    child    calls  name
100.00      0.90     0.90     0.00     0.00        1  Array#each
  0.00      0.90     0.00     0.00     0.90        1  Global#[No method]
  0.00      0.90     0.00     0.00     0.90        1  Enumerable#each_slice