Python-like enumerate()
authorGiuseppe Ottaviano <ott@fb.com>
Wed, 6 Jul 2016 23:24:04 +0000 (16:24 -0700)
committerFacebook Github Bot 6 <facebook-github-bot-6-bot@fb.com>
Wed, 6 Jul 2016 23:25:33 +0000 (16:25 -0700)
commitd2549d035ac7b7f333f1e349ba20f3ebe8bd4609
tree98639abbb70cb3eebf9b1736ba2841c191e163f1
parent6b66499c48ba83883c6922326cd5d618e7e3a05d
Python-like enumerate()

Summary:
Range-based for cannot be used if the element index is needed along
with the element. In these situations, it is often necessary to fall
back to the standard for loop, which is easy to get wrong, or maintain
an extra count variable, which is error-prone when control flow is
nontrivial (for example in the presence of `continue`).

This diff introduces a simple implementation of Python's
`enumerate()`, with the same signature. Since in C++ tuple-unpacking
is verbose, the iteration variable returned is a proxy object `it`
where the iteration index can be retrieved with `it.idx`, and the
value with `*it` or `it->...`, like a normal iterator.

Differential Revision: D3477877

fbshipit-source-id: 376af7f559e8b60f02a3f81f0c026a901e23ddcf
folly/Enumerate.h [new file with mode: 0644]
folly/Makefile.am
folly/test/EnumerateTest.cpp [new file with mode: 0644]