You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-2Lines changed: 28 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1012,14 +1012,15 @@ because ``(g, x, y)`` is just a tuple of ``g``, ``x`` and ``y``. This is by desi
1012
1012
- Only applicable to monotonic divergent inputs (such as``primes``). Increasing/decreasing is auto-detected from the first non-zero diff, but the function may fail to terminate if the inputis actually not monotonic, or has an upper/lower bound.
1013
1013
-`iindex`: like ``list.index``, but for a general iterable. Consumes the iterable, so only makes sense for memoized inputs. *Added in v0.13.1.*
1014
1014
-`prod`: like the builtin `sum`, but compute the product. Oddly missing from the standard library. *Added in v0.13.1.*
1015
+
-`window`: sliding length-n window iterator for general iterables. Acts like the well-known [n-gram zip trick](http://www.locallyoptimal.com/blog/2013/01/20/elegant-n-gram-generation-in-python/), but the input can be any iterable. *Added in v0.14.1.*
@@ -1064,6 +1065,13 @@ assert not inn(1337, primes())
1064
1065
assert iindex(2, (1, 2, 3)) ==1
1065
1066
assert iindex(31337, primes()) ==3378
1066
1067
1068
+
# window: length-n sliding window iterator for general iterables
1069
+
lst= (x for x inrange(5))
1070
+
out= []
1071
+
for a, b, c in window(lst, n=3):
1072
+
out.append((a, b, c))
1073
+
assert out == [(0, 1, 2), (1, 2, 3), (2, 3, 4)]
1074
+
1067
1075
# flatmap
1068
1076
def msqrt(x): # multivalued sqrt
1069
1077
if x ==0.:
@@ -2613,7 +2621,25 @@ assert inp == deque([])
2613
2621
assert out == [0, 10, 1, 11, 2, 12]
2614
2622
```
2615
2623
2616
-
A real use case for this is to split sequences of items, stored as lists in a deque, into shorter sequences where some condition is contiguously ``True``or``False``. When the condition changes state, just commit the current subsequence, and push the rest of that input sequence (still requiring analysis) back to the input deque, to be dealt with later.
2624
+
``Popper`` comboes with other iterable utilities, such as``window``:
(Although ``window`` invokes ``iter()`` on the ``Popper``, this works because the ``Popper`` never invokes ``iter()`` on the underlying container. Any mutations to the input container performed by the loop body will be understood by ``Popper``and thus also seen by the ``window``. The first ``n`` elements, though, are read before the loop body gets control, because the window needs them to initialize itself.)
2641
+
2642
+
One possible real use case for``Popper``is to split sequences of items, stored as lists in a deque, into shorter sequences where some condition is contiguously ``True``or``False``. When the condition changes state, just commit the current subsequence, and push the rest of that input sequence (still requiring analysis) back to the input deque, to be dealt with later.
2617
2643
2618
2644
The argument to ``Popper`` (here ``lst``) contains the **remaining** items. Each iteration pops an element **from the left**. The loop terminates when ``lst``is empty.
0 commit comments