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
Showing
folly/Enumerate.h
0 → 100644
folly/test/EnumerateTest.cpp
0 → 100644
Please register or sign in to comment