提交 4aadd160 编写于 作者: K KD

Speed up Array#split

Ruby 2.0.0p247
Rehearsal ---------------------------------------
old  10.670000   0.150000  10.820000 ( 10.822651)
new   8.520000   0.050000   8.570000 (  8.571825)
----------------------------- total: 19.390000sec

          user     system      total        real
old  10.620000   0.170000  10.790000 ( 10.790409)
new   8.570000   0.110000   8.680000 (  8.686051)
上级 13339948
......@@ -83,10 +83,10 @@ def in_groups(number, fill_with = nil)
#
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
# (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
def split(value = nil, &block)
if block
def split(value = nil)
if block_given?
inject([[]]) do |results, element|
if block.call(element)
if yield(element)
results << []
else
results.last << element
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册