vim-jp/vital.vim — pull request #62 by rhysd (2013-06-05 15:51:49+00:00)
Title: Data.List: add with_index()

hi.
I suggest adding with_index() function which is inspired by Ruby's
with_index method.

with_index({list} [, {offset}])

Returns {list} with index. {offset} means the base of index.
If you specify {offset}, index starts with {offset}.

:echo List.with_index(['a', 'b', 'c'])
[['a', 0], ['b', 1], ['c', 2]]

This function is useful when used with |:for|.
For example, when you have lines as a list of string and you want to
output a line with a line number to each line, you may write as below.

for idx in range(1, len(lines))
echo idx.': '.lines[idx]
endfor

This procedure can be rewritten using with_index() as below.

for [line, idx] in List.with_index(lines, 1)
echo idx.': '.line
endfor