|
285 | 285 | "\n", |
286 | 286 | "### is_iter\n", |
287 | 287 | "\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", |
289 | 296 | "\n", |
290 | 297 | "*Test whether `o` can be used in a `for` loop*" |
291 | 298 | ], |
292 | 299 | "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", |
294 | 308 | "\n", |
295 | 309 | "*Test whether `o` can be used in a `for` loop*" |
296 | 310 | ] |
|
591 | 605 | "outputs": [], |
592 | 606 | "source": [ |
593 | 607 | "#| export\n", |
594 | | - "def flatmap(f, xs):\n", |
| 608 | + "def flatmap(f, xs, **kwargs):\n", |
595 | 609 | " \"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)]" |
597 | 611 | ] |
598 | 612 | }, |
599 | 613 | { |
|
830 | 844 | "flatmap(factpairs, [6,10])" |
831 | 845 | ] |
832 | 846 | }, |
| 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 | + }, |
833 | 876 | { |
834 | 877 | "cell_type": "markdown", |
835 | 878 | "id": "d8a026cf", |
|
1303 | 1346 | "\n", |
1304 | 1347 | "### L.__getitem__\n", |
1305 | 1348 | "\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", |
1307 | 1357 | "\n", |
1308 | 1358 | "*Retrieve `idx` (can be list of indices, or mask, or int) items*" |
1309 | 1359 | ], |
1310 | 1360 | "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", |
1312 | 1369 | "\n", |
1313 | 1370 | "*Retrieve `idx` (can be list of indices, or mask, or int) items*" |
1314 | 1371 | ] |
|
1352 | 1409 | "\n", |
1353 | 1410 | "### L.__setitem__\n", |
1354 | 1411 | "\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", |
1356 | 1420 | "\n", |
1357 | 1421 | "*Set `idx` (can be list of indices, or mask, or int) items to `o` (which is broadcast if not iterable)*" |
1358 | 1422 | ], |
1359 | 1423 | "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", |
1361 | 1432 | "\n", |
1362 | 1433 | "*Set `idx` (can be list of indices, or mask, or int) items to `o` (which is broadcast if not iterable)*" |
1363 | 1434 | ] |
|
3089 | 3160 | "source": [ |
3090 | 3161 | "#| export\n", |
3091 | 3162 | "@patch\n", |
3092 | | - "def flatmap(self:L, f):\n", |
| 3163 | + "def flatmap(self:L, f, **kwargs):\n", |
3093 | 3164 | " \"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))" |
3095 | 3166 | ] |
3096 | 3167 | }, |
3097 | 3168 | { |
|
3112 | 3183 | "test_eq(L(\"a,b,c\", \"d,e\").flatmap(Self.split(',')), ['a', 'b', 'c', 'd', 'e'])" |
3113 | 3184 | ] |
3114 | 3185 | }, |
| 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 | + }, |
3115 | 3204 | { |
3116 | 3205 | "cell_type": "markdown", |
3117 | 3206 | "id": "98e3c09e", |
|
3141 | 3230 | "L(\"a,b,c\", \"d,e\").map(Self.split(',')).concat()" |
3142 | 3231 | ] |
3143 | 3232 | }, |
| 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 | + }, |
3144 | 3254 | { |
3145 | 3255 | "cell_type": "markdown", |
3146 | 3256 | "id": "d0e47eb0", |
|
4319 | 4429 | ] |
4320 | 4430 | } |
4321 | 4431 | ], |
4322 | | - "metadata": { |
4323 | | - "kernelspec": { |
4324 | | - "display_name": "python3", |
4325 | | - "language": "python", |
4326 | | - "name": "python3" |
4327 | | - } |
4328 | | - }, |
| 4432 | + "metadata": {}, |
4329 | 4433 | "nbformat": 4, |
4330 | 4434 | "nbformat_minor": 5 |
4331 | 4435 | } |
0 commit comments