You can just walk through the output of chunk
and track starting index yourself. Simply increment it by length of each chunk. Here's my quick attempt.
chunked = [nil,nil,1,2,nil,3].chunk { |e| !e.nil? }
_, chunks_with_embedded_index = chunked.each_with_object([0, []]) do |(k, chunk), memo|
memo[1] << [k, memo[0], chunk]
memo[0] += chunk.length
end
chunks_with_embedded_index.each {|c| p c }
# >> [false, 0, [nil, nil]]
# >> [true, 2, [1, 2]]
# >> [false, 4, [nil]]
# >> [true, 5, [3]]
You can extract the temp var for last index into a full local variable and have somewhat cleaner implementation:
chunked = [nil,nil,1,2,nil,3].chunk { |e| !e.nil? }
last_index = 0
chunks_with_embedded_index = chunked.reduce([]) do |memo, (k, chunk)|
memo << [k, last_index, chunk]
last_index += chunk.length
memo
end
chunks_with_embedded_index.each {|c| p c }
# >> [false, 0, [nil, nil]]
# >> [true, 2, [1, 2]]
# >> [false, 4, [nil]]
# >> [true, 5, [3]]