deltas.each_with_index.map { |val, idx| val + (idx % 2 == 0 ? x : y )}
Regardless of whether it is โless complexโ, it depends on the audience.
Reducing duplication and complexity should focus on macro behavior, rather than micro-refactoring short, readable methods.
Will this rewrite lead to a qualitatively more understandable system? Or are there more important issues of a higher level?
Is the improvement of documentation for applications, classes, and methods improved? Should these documents be in code or on a wiki? Will a picture cost a thousand lines?
Comparison of performance against @tokland (its gain is a significant amount). Assuming deltas is an array of a million elements 1-1 m in size. MRI, Ubuntu, an old pokey machine.
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
A 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
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
source share