Skip to content

Commit 6e02b6b

Browse files
committed
fixes #746
1 parent 99a56d4 commit 6e02b6b

File tree

2 files changed

+125
-21
lines changed

2 files changed

+125
-21
lines changed

fastcore/foundation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ def product(xs):
9292
return reduce(operator.mul, [o for o in xs if o is not None], 1)
9393

9494
# %% ../nbs/02_foundation.ipynb
95-
def flatmap(f, xs):
95+
def flatmap(f, xs, **kwargs):
9696
"Apply f to each element and flatten the results into a single list."
97-
return [y for x in xs for y in f(x)]
97+
return [y for x in xs for y in f(x, **kwargs)]
9898

9999
# %% ../nbs/02_foundation.ipynb
100100
class CollBase:
@@ -473,9 +473,9 @@ def setattrs(self:L, attr, val):
473473

474474
# %% ../nbs/02_foundation.ipynb
475475
@patch
476-
def flatmap(self:L, f):
476+
def flatmap(self:L, f, **kwargs):
477477
"Apply f to each element and flatten the results into a single L."
478-
return L(flatmap(f, self))
478+
return L(flatmap(f, self, **kwargs))
479479

480480
# %% ../nbs/02_foundation.ipynb
481481
@patch

nbs/02_foundation.ipynb

Lines changed: 121 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,26 @@
285285
"\n",
286286
"### is_iter\n",
287287
"\n",
288-
"> is_iter (o)\n",
288+
"```python\n",
289+
"\n",
290+
"def is_iter(\n",
291+
" o\n",
292+
"):\n",
293+
"\n",
294+
"\n",
295+
"```\n",
289296
"\n",
290297
"*Test whether `o` can be used in a `for` loop*"
291298
],
292299
"text/plain": [
293-
"> is_iter (o)\n",
300+
"```python\n",
301+
"\n",
302+
"def is_iter(\n",
303+
" o\n",
304+
"):\n",
305+
"\n",
306+
"\n",
307+
"```\n",
294308
"\n",
295309
"*Test whether `o` can be used in a `for` loop*"
296310
]
@@ -591,9 +605,9 @@
591605
"outputs": [],
592606
"source": [
593607
"#| export\n",
594-
"def flatmap(f, xs):\n",
608+
"def flatmap(f, xs, **kwargs):\n",
595609
" \"Apply f to each element and flatten the results into a single list.\"\n",
596-
" return [y for x in xs for y in f(x)]"
610+
" return [y for x in xs for y in f(x, **kwargs)]"
597611
]
598612
},
599613
{
@@ -830,6 +844,35 @@
830844
"flatmap(factpairs, [6,10])"
831845
]
832846
},
847+
{
848+
"cell_type": "markdown",
849+
"id": "f0026d6c",
850+
"metadata": {},
851+
"source": [
852+
"You can also use kwargs, for instance to apply `str.split` with a custom separator:"
853+
]
854+
},
855+
{
856+
"cell_type": "code",
857+
"execution_count": null,
858+
"id": "754fff88",
859+
"metadata": {},
860+
"outputs": [
861+
{
862+
"data": {
863+
"text/plain": [
864+
"['a', 'b', 'c', 'd', 'e']"
865+
]
866+
},
867+
"execution_count": null,
868+
"metadata": {},
869+
"output_type": "execute_result"
870+
}
871+
],
872+
"source": [
873+
"flatmap(str.split, [\"a-b-c\", \"d-e\"], sep=\"-\")"
874+
]
875+
},
833876
{
834877
"cell_type": "markdown",
835878
"id": "d8a026cf",
@@ -1303,12 +1346,26 @@
13031346
"\n",
13041347
"### L.__getitem__\n",
13051348
"\n",
1306-
"> L.__getitem__ (idx)\n",
1349+
"```python\n",
1350+
"\n",
1351+
"def __getitem__(\n",
1352+
" idx\n",
1353+
"):\n",
1354+
"\n",
1355+
"\n",
1356+
"```\n",
13071357
"\n",
13081358
"*Retrieve `idx` (can be list of indices, or mask, or int) items*"
13091359
],
13101360
"text/plain": [
1311-
"> L.__getitem__ (idx)\n",
1361+
"```python\n",
1362+
"\n",
1363+
"def __getitem__(\n",
1364+
" idx\n",
1365+
"):\n",
1366+
"\n",
1367+
"\n",
1368+
"```\n",
13121369
"\n",
13131370
"*Retrieve `idx` (can be list of indices, or mask, or int) items*"
13141371
]
@@ -1352,12 +1409,26 @@
13521409
"\n",
13531410
"### L.__setitem__\n",
13541411
"\n",
1355-
"> L.__setitem__ (idx, o)\n",
1412+
"```python\n",
1413+
"\n",
1414+
"def __setitem__(\n",
1415+
" idx, o\n",
1416+
"):\n",
1417+
"\n",
1418+
"\n",
1419+
"```\n",
13561420
"\n",
13571421
"*Set `idx` (can be list of indices, or mask, or int) items to `o` (which is broadcast if not iterable)*"
13581422
],
13591423
"text/plain": [
1360-
"> L.__setitem__ (idx, o)\n",
1424+
"```python\n",
1425+
"\n",
1426+
"def __setitem__(\n",
1427+
" idx, o\n",
1428+
"):\n",
1429+
"\n",
1430+
"\n",
1431+
"```\n",
13611432
"\n",
13621433
"*Set `idx` (can be list of indices, or mask, or int) items to `o` (which is broadcast if not iterable)*"
13631434
]
@@ -3089,9 +3160,9 @@
30893160
"source": [
30903161
"#| export\n",
30913162
"@patch\n",
3092-
"def flatmap(self:L, f):\n",
3163+
"def flatmap(self:L, f, **kwargs):\n",
30933164
" \"Apply f to each element and flatten the results into a single L.\"\n",
3094-
" return L(flatmap(f, self))"
3165+
" return L(flatmap(f, self, **kwargs))"
30953166
]
30963167
},
30973168
{
@@ -3112,6 +3183,24 @@
31123183
"test_eq(L(\"a,b,c\", \"d,e\").flatmap(Self.split(',')), ['a', 'b', 'c', 'd', 'e'])"
31133184
]
31143185
},
3186+
{
3187+
"cell_type": "markdown",
3188+
"id": "94aebdb0",
3189+
"metadata": {},
3190+
"source": [
3191+
"or alternatively use kwargs:"
3192+
]
3193+
},
3194+
{
3195+
"cell_type": "code",
3196+
"execution_count": null,
3197+
"id": "083f30df",
3198+
"metadata": {},
3199+
"outputs": [],
3200+
"source": [
3201+
"test_eq(L(\"a-b-c\", \"d-e\").flatmap(str.split, sep='-'), ['a', 'b', 'c', 'd', 'e'])"
3202+
]
3203+
},
31153204
{
31163205
"cell_type": "markdown",
31173206
"id": "98e3c09e",
@@ -3141,6 +3230,27 @@
31413230
"L(\"a,b,c\", \"d,e\").map(Self.split(',')).concat()"
31423231
]
31433232
},
3233+
{
3234+
"cell_type": "code",
3235+
"execution_count": null,
3236+
"id": "ed763699",
3237+
"metadata": {},
3238+
"outputs": [
3239+
{
3240+
"data": {
3241+
"text/plain": [
3242+
"['a', 'b', 'c', 'd', 'e']"
3243+
]
3244+
},
3245+
"execution_count": null,
3246+
"metadata": {},
3247+
"output_type": "execute_result"
3248+
}
3249+
],
3250+
"source": [
3251+
"L(\"a-b-c\", \"d-e\").map(str.split, sep='-').concat()"
3252+
]
3253+
},
31443254
{
31453255
"cell_type": "markdown",
31463256
"id": "d0e47eb0",
@@ -4319,13 +4429,7 @@
43194429
]
43204430
}
43214431
],
4322-
"metadata": {
4323-
"kernelspec": {
4324-
"display_name": "python3",
4325-
"language": "python",
4326-
"name": "python3"
4327-
}
4328-
},
4432+
"metadata": {},
43294433
"nbformat": 4,
43304434
"nbformat_minor": 5
43314435
}

0 commit comments

Comments
 (0)