C++ ranges comparison¶
rangeslib is inspired by modern C++ ranges, but it is designed for Python’s
iterator and collection model. The goal is familiar naming and useful behavior,
not a direct port of C++ view machinery.
Close conceptual matches¶
C++ ranges idea |
rangeslib API |
Notes |
|---|---|---|
|
`iterable |
views.all()` |
|
|
Keeps values for which the predicate is true. |
|
|
Maps each value through a callable. |
|
|
Uses Python slice semantics, including negative counts. |
|
|
Aliases also exist as |
|
|
Accepts any number of companion iterables and stops at the shortest input. |
|
|
Produces ordinary Python tuples. |
|
|
Produces |
|
|
Produces nested eager |
|
|
Flattens one level. |
|
|
Supports scalar delimiters and separator patterns. |
Important differences¶
Eager results¶
C++ views are usually lazy. rangeslib is currently eager: most operations
materialize and return a Range immediately. This keeps returned values simple
and reusable, but it means full-materialization operations should not be applied
directly to infinite iterables.
Iterators and sentinels¶
C++ ranges expose iterator/sentinel concepts directly. Python exposes the
iterator protocol through iter() and next(), with StopIteration marking the
end. rangeslib follows Python here.
References versus values¶
C++ views often yield references into underlying ranges. rangeslib stores the
values yielded by Python iteration. Mutating a mutable object contained in a
Range still mutates that object, but the Range itself is an eager container.
Type system limits¶
Python type checkers can preserve many useful public types, including mixed
zip, cartesian_product, typed keys / values, and exact pairwise tuples.
They cannot express every C++ tuple-like constraint or variadic callable rule
with the same precision. zip and cartesian_product provide precise public
types for zero, one, and two companion iterables; larger runtime arities remain
supported but are typed less precisely.
Naming¶
The public facade uses lowercase Python functions:
from rangeslib import ranges, views
This preserves a C++-like vocabulary while staying natural for Python code.