Skip to content

Commit c380c2f

Browse files
authored
Merge pull request #511 from Isaac-Flath/argfirst_bug
L argfirst bug fix
2 parents 4c3bfdb + 068cd10 commit c380c2f

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,4 @@
594594
'fastcore.xtras.truncstr': ('xtras.html#truncstr', 'fastcore/xtras.py'),
595595
'fastcore.xtras.untar_dir': ('xtras.html#untar_dir', 'fastcore/xtras.py'),
596596
'fastcore.xtras.utc2local': ('xtras.html#utc2local', 'fastcore/xtras.py'),
597-
'fastcore.xtras.walk': ('xtras.html#walk', 'fastcore/xtras.py')}}}
597+
'fastcore.xtras.walk': ('xtras.html#walk', 'fastcore/xtras.py')}}}

fastcore/foundation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ def range(cls, a, b=None, step=None): return cls(range_of(a, b=b, step=step))
155155

156156
def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))
157157
def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))
158-
def argfirst(self, f, negate=False): return first(i for i,o in self.enumerate() if f(o))
158+
def argfirst(self, f, negate=False):
159+
if negate: f = not_(f)
160+
return first(i for i,o in self.enumerate() if f(o))
159161
def filter(self, f=noop, negate=False, gen=False, **kwargs):
160162
return self._new(filter_ex(self, f=f, negate=negate, gen=gen, **kwargs))
161163

nbs/02_foundation.ipynb

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,9 @@
612612
"\n",
613613
" def map(self, f, *args, gen=False, **kwargs): return self._new(map_ex(self, f, *args, gen=gen, **kwargs))\n",
614614
" def argwhere(self, f, negate=False, **kwargs): return self._new(argwhere(self, f, negate, **kwargs))\n",
615-
" def argfirst(self, f, negate=False): return first(i for i,o in self.enumerate() if f(o))\n",
615+
" def argfirst(self, f, negate=False): \n",
616+
" if negate: f = not_(f)\n",
617+
" return first(i for i,o in self.enumerate() if f(o))\n",
616618
" def filter(self, f=noop, negate=False, gen=False, **kwargs):\n",
617619
" return self._new(filter_ex(self, f=f, negate=negate, gen=gen, **kwargs))\n",
618620
"\n",
@@ -1132,7 +1134,7 @@
11321134
"text/markdown": [
11331135
"---\n",
11341136
"\n",
1135-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L164){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1137+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L166){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
11361138
"\n",
11371139
"### L.unique\n",
11381140
"\n",
@@ -1143,7 +1145,7 @@
11431145
"text/plain": [
11441146
"---\n",
11451147
"\n",
1146-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L164){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1148+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L166){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
11471149
"\n",
11481150
"### L.unique\n",
11491151
"\n",
@@ -1180,7 +1182,7 @@
11801182
"text/markdown": [
11811183
"---\n",
11821184
"\n",
1183-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L165){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1185+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L167){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
11841186
"\n",
11851187
"### L.val2idx\n",
11861188
"\n",
@@ -1191,7 +1193,7 @@
11911193
"text/plain": [
11921194
"---\n",
11931195
"\n",
1194-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L165){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1196+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L167){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
11951197
"\n",
11961198
"### L.val2idx\n",
11971199
"\n",
@@ -1228,7 +1230,7 @@
12281230
"text/markdown": [
12291231
"---\n",
12301232
"\n",
1231-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L159){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1233+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L161){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
12321234
"\n",
12331235
"### L.filter\n",
12341236
"\n",
@@ -1239,7 +1241,7 @@
12391241
"text/plain": [
12401242
"---\n",
12411243
"\n",
1242-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L159){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1244+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L161){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
12431245
"\n",
12441246
"### L.filter\n",
12451247
"\n",
@@ -1380,7 +1382,8 @@
13801382
"metadata": {},
13811383
"outputs": [],
13821384
"source": [
1383-
"test_eq(t.argfirst(lambda o:o>4), 5)"
1385+
"test_eq(t.argfirst(lambda o:o>4), 5)\n",
1386+
"test_eq(t.argfirst(lambda o:o>4,negate=True),0)"
13841387
]
13851388
},
13861389
{
@@ -1490,7 +1493,7 @@
14901493
"text/markdown": [
14911494
"---\n",
14921495
"\n",
1493-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L167){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1496+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L169){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
14941497
"\n",
14951498
"### L.map_dict\n",
14961499
"\n",
@@ -1501,7 +1504,7 @@
15011504
"text/plain": [
15021505
"---\n",
15031506
"\n",
1504-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L167){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1507+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L169){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
15051508
"\n",
15061509
"### L.map_dict\n",
15071510
"\n",
@@ -1539,7 +1542,7 @@
15391542
"text/markdown": [
15401543
"---\n",
15411544
"\n",
1542-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L179){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1545+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L181){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
15431546
"\n",
15441547
"### L.zip\n",
15451548
"\n",
@@ -1550,7 +1553,7 @@
15501553
"text/plain": [
15511554
"---\n",
15521555
"\n",
1553-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L179){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1556+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L181){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
15541557
"\n",
15551558
"### L.zip\n",
15561559
"\n",
@@ -1599,7 +1602,7 @@
15991602
"text/markdown": [
16001603
"---\n",
16011604
"\n",
1602-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L181){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1605+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L183){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
16031606
"\n",
16041607
"### L.map_zip\n",
16051608
"\n",
@@ -1610,7 +1613,7 @@
16101613
"text/plain": [
16111614
"---\n",
16121615
"\n",
1613-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L181){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1616+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L183){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
16141617
"\n",
16151618
"### L.map_zip\n",
16161619
"\n",
@@ -1648,7 +1651,7 @@
16481651
"text/markdown": [
16491652
"---\n",
16501653
"\n",
1651-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L180){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1654+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L182){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
16521655
"\n",
16531656
"### L.zipwith\n",
16541657
"\n",
@@ -1659,7 +1662,7 @@
16591662
"text/plain": [
16601663
"---\n",
16611664
"\n",
1662-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L180){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1665+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L182){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
16631666
"\n",
16641667
"### L.zipwith\n",
16651668
"\n",
@@ -1698,7 +1701,7 @@
16981701
"text/markdown": [
16991702
"---\n",
17001703
"\n",
1701-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L182){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1704+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L184){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
17021705
"\n",
17031706
"### L.map_zipwith\n",
17041707
"\n",
@@ -1709,7 +1712,7 @@
17091712
"text/plain": [
17101713
"---\n",
17111714
"\n",
1712-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L182){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1715+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L184){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
17131716
"\n",
17141717
"### L.map_zipwith\n",
17151718
"\n",
@@ -1746,7 +1749,7 @@
17461749
"text/markdown": [
17471750
"---\n",
17481751
"\n",
1749-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L171){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1752+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L173){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
17501753
"\n",
17511754
"### L.itemgot\n",
17521755
"\n",
@@ -1757,7 +1760,7 @@
17571760
"text/plain": [
17581761
"---\n",
17591762
"\n",
1760-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L171){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1763+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L173){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
17611764
"\n",
17621765
"### L.itemgot\n",
17631766
"\n",
@@ -1794,7 +1797,7 @@
17941797
"text/markdown": [
17951798
"---\n",
17961799
"\n",
1797-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L175){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1800+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L177){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
17981801
"\n",
17991802
"### L.attrgot\n",
18001803
"\n",
@@ -1805,7 +1808,7 @@
18051808
"text/plain": [
18061809
"---\n",
18071810
"\n",
1808-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L175){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1811+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L177){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
18091812
"\n",
18101813
"### L.attrgot\n",
18111814
"\n",
@@ -1993,7 +1996,7 @@
19931996
"text/markdown": [
19941997
"---\n",
19951998
"\n",
1996-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L188){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
1999+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L190){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
19972000
"\n",
19982001
"### L.concat\n",
19992002
"\n",
@@ -2004,7 +2007,7 @@
20042007
"text/plain": [
20052008
"---\n",
20062009
"\n",
2007-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L188){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
2010+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L190){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
20082011
"\n",
20092012
"### L.concat\n",
20102013
"\n",
@@ -2090,7 +2093,7 @@
20902093
"text/markdown": [
20912094
"---\n",
20922095
"\n",
2093-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L168){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
2096+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L170){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
20942097
"\n",
20952098
"### L.map_first\n",
20962099
"\n",
@@ -2101,7 +2104,7 @@
21012104
"text/plain": [
21022105
"---\n",
21032106
"\n",
2104-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L168){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
2107+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L170){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
21052108
"\n",
21062109
"### L.map_first\n",
21072110
"\n",
@@ -2139,7 +2142,7 @@
21392142
"text/markdown": [
21402143
"---\n",
21412144
"\n",
2142-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L192){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
2145+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L194){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
21432146
"\n",
21442147
"### L.setattrs\n",
21452148
"\n",
@@ -2150,7 +2153,7 @@
21502153
"text/plain": [
21512154
"---\n",
21522155
"\n",
2153-
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L192){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
2156+
"[source](https://github.com/fastai/fastcore/blob/master/fastcore/foundation.py#L194){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
21542157
"\n",
21552158
"### L.setattrs\n",
21562159
"\n",

0 commit comments

Comments
 (0)