Skip to content

Commit

Permalink
Merge pull request #410 from Salehbigdeli/master
Browse files Browse the repository at this point in the history
Bugfix handle edge case when we have string in list and want to flatten
  • Loading branch information
jph00 authored May 16, 2022
2 parents cced1ec + e96545e commit f230bc1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit f230bc1

Please # to comment.