Skip to content

Commit df6efe2

Browse files
committed
fixes #716
1 parent 723f492 commit df6efe2

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
'fastcore.foundation.L.zipwith': ('foundation.html#l.zipwith', 'fastcore/foundation.py'),
402402
'fastcore.foundation._L_Meta': ('foundation.html#_l_meta', 'fastcore/foundation.py'),
403403
'fastcore.foundation._L_Meta.__call__': ('foundation.html#_l_meta.__call__', 'fastcore/foundation.py'),
404+
'fastcore.foundation._batched': ('foundation.html#_batched', 'fastcore/foundation.py'),
404405
'fastcore.foundation._f': ('foundation.html#_f', 'fastcore/foundation.py'),
405406
'fastcore.foundation.add_docs': ('foundation.html#add_docs', 'fastcore/foundation.py'),
406407
'fastcore.foundation.coll_repr': ('foundation.html#coll_repr', 'fastcore/foundation.py'),

fastcore/foundation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,11 +479,23 @@ def pairwise(self:L):
479479
"Same as `itertools.pairwise`"
480480
return self._new(itertools.pairwise(self))
481481

482+
# %% ../nbs/02_foundation.ipynb
483+
def _batched(iterable, n):
484+
"Batch data into tuples of length n. The last batch may be shorter."
485+
if n < 1: raise ValueError('n must be at least one')
486+
it = iter(iterable)
487+
while batch := tuple(itertools.islice(it, n)):
488+
yield batch
489+
490+
# %% ../nbs/02_foundation.ipynb
491+
try: from itertools import batched
492+
except ImportError: batched = _batched
493+
482494
# %% ../nbs/02_foundation.ipynb
483495
@patch
484496
def batched(self:L, n):
485497
"Same as `itertools.batched`"
486-
return self._new(itertools.batched(self, n))
498+
return self._new(batched(self, n))
487499

488500
# %% ../nbs/02_foundation.ipynb
489501
@patch

nbs/02_foundation.ipynb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,15 +2672,43 @@
26722672
{
26732673
"cell_type": "code",
26742674
"execution_count": null,
2675-
"id": "c5984ee1",
2675+
"id": "1f2a370c",
2676+
"metadata": {},
2677+
"outputs": [],
2678+
"source": [
2679+
"#| export\n",
2680+
"def _batched(iterable, n):\n",
2681+
" \"Batch data into tuples of length n. The last batch may be shorter.\"\n",
2682+
" if n < 1: raise ValueError('n must be at least one')\n",
2683+
" it = iter(iterable)\n",
2684+
" while batch := tuple(itertools.islice(it, n)):\n",
2685+
" yield batch"
2686+
]
2687+
},
2688+
{
2689+
"cell_type": "code",
2690+
"execution_count": null,
2691+
"id": "41aefe58",
2692+
"metadata": {},
2693+
"outputs": [],
2694+
"source": [
2695+
"#| export\n",
2696+
"try: from itertools import batched\n",
2697+
"except ImportError: batched = _batched"
2698+
]
2699+
},
2700+
{
2701+
"cell_type": "code",
2702+
"execution_count": null,
2703+
"id": "d9f7ffb9",
26762704
"metadata": {},
26772705
"outputs": [],
26782706
"source": [
26792707
"#| export\n",
26802708
"@patch\n",
26812709
"def batched(self:L, n):\n",
26822710
" \"Same as `itertools.batched`\"\n",
2683-
" return self._new(itertools.batched(self, n))"
2711+
" return self._new(batched(self, n))"
26842712
]
26852713
},
26862714
{
@@ -2952,6 +2980,14 @@
29522980
"test_eq(L([1,2,3]).flatten(), [1,2,3]) # already flat"
29532981
]
29542982
},
2983+
{
2984+
"cell_type": "markdown",
2985+
"id": "88fa0ef8",
2986+
"metadata": {},
2987+
"source": [
2988+
"##### User"
2989+
]
2990+
},
29552991
{
29562992
"cell_type": "markdown",
29572993
"id": "8cf19e29",

0 commit comments

Comments
 (0)