Skip to content

Commit

Permalink
fixes #161
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 31, 2020
1 parent 57aed26 commit aabe94b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 5 additions & 2 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ def argwhere(iterable, f, negate=False, **kwargs):
# Cell
def filter_ex(iterable, f=noop, negate=False, gen=False, **kwargs):
"Like `filter`, but passing `kwargs` to `f`, defaulting `f` to `noop`, and adding `negate` and `gen`"
if f is None: f = lambda _: True
if kwargs: f = partial(f,**kwargs)
if negate: f = negate_func(f)
res = filter(f, iterable)
Expand All @@ -434,9 +435,11 @@ def renumerate(iterable, start=0):
return ((o,i) for i,o in enumerate(iterable, start=start))

# Cell
def first(x):
def first(x, f=None, negate=False, **kwargs):
"First element of `x`, or None if missing"
try: return next(iter(x))
x = iter(x)
if f: x = filter_ex(x, f=f, negate=negate, gen=True, **kwargs)
try: return next(x)
except StopIteration: return None

# Cell
Expand Down
10 changes: 7 additions & 3 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,7 @@
"#export\n",
"def filter_ex(iterable, f=noop, negate=False, gen=False, **kwargs):\n",
" \"Like `filter`, but passing `kwargs` to `f`, defaulting `f` to `noop`, and adding `negate` and `gen`\"\n",
" if f is None: f = lambda _: True\n",
" if kwargs: f = partial(f,**kwargs)\n",
" if negate: f = negate_func(f)\n",
" res = filter(f, iterable)\n",
Expand Down Expand Up @@ -2348,9 +2349,11 @@
"outputs": [],
"source": [
"#export\n",
"def first(x):\n",
"def first(x, f=None, negate=False, **kwargs):\n",
" \"First element of `x`, or None if missing\"\n",
" try: return next(iter(x))\n",
" x = iter(x)\n",
" if f: x = filter_ex(x, f=f, negate=negate, gen=True, **kwargs)\n",
" try: return next(x)\n",
" except StopIteration: return None"
]
},
Expand All @@ -2361,7 +2364,8 @@
"outputs": [],
"source": [
"test_eq(first(['a', 'b', 'c', 'd', 'e']), 'a')\n",
"test_eq(first([]), None)"
"test_eq(first([False]), False)\n",
"test_eq(first([False], noop), None)"
]
},
{
Expand Down
7 changes: 3 additions & 4 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@
{
"data": {
"text/plain": [
"['a', 'g', 'd', 'e', 'c', 'b', 'h', 'f']"
"['b', 'd', 'g', 'h', 'f', 'c', 'e', 'a']"
]
},
"execution_count": null,
Expand Down Expand Up @@ -940,10 +940,9 @@
"source": [
"def what(f):\n",
" with maybe_open(f, 'rb') as f:\n",
" location = f.tell()\n",
" h = f.read(32)\n",
" location,h = f.tell(),f.read(32)\n",
" f.seek(location)\n",
" return first(filter(None, (tf(h, f) for tf in imghdr.tests)))"
" return first((tf(h,f) for tf in imghdr.tests), noop)"
]
},
{
Expand Down

0 comments on commit aabe94b

Please # to comment.