Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Bugfix handle edge case when we have string in list and want to flatten #410

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ class Float(float,ShowPrint):
def flatten(o):
"Concatenate all collections and items as a generator"
for item in o:
if isinstance(item, str): yield item; continue
try: yield from flatten(item)
except TypeError: yield item

Expand Down
23 changes: 22 additions & 1 deletion nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2904,6 +2904,7 @@
"def flatten(o):\n",
" \"Concatenate all collections and items as a generator\"\n",
" for item in o:\n",
" if isinstance(item, str): yield item; continue\n",
" try: yield from flatten(item)\n",
" except TypeError: yield item"
]
Expand Down Expand Up @@ -2940,6 +2941,26 @@
"concat([(o for o in range(2)),[2,3,4], 5])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['abc', 'xyz', 'foo', 'bar']"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"concat([[\"abc\", \"xyz\"], [\"foo\", \"bar\"]])"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -5306,7 +5327,7 @@
{
"data": {
"text/plain": [
"64"
"8"
]
},
"execution_count": null,
Expand Down