Skip to content

Commit 95da2f8

Browse files
authored
Merge pull request #792 from renato145/L_first_last
Add first and last methods to L
2 parents a3c1ada + 1e51091 commit 95da2f8

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

fastcore/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,12 @@
390390
'fastcore.foundation.L.dropwhile': ('foundation.html#l.dropwhile', 'fastcore/foundation.py'),
391391
'fastcore.foundation.L.enumerate': ('foundation.html#l.enumerate', 'fastcore/foundation.py'),
392392
'fastcore.foundation.L.filter': ('foundation.html#l.filter', 'fastcore/foundation.py'),
393+
'fastcore.foundation.L.first': ('foundation.html#l.first', 'fastcore/foundation.py'),
393394
'fastcore.foundation.L.flatmap': ('foundation.html#l.flatmap', 'fastcore/foundation.py'),
394395
'fastcore.foundation.L.flatten': ('foundation.html#l.flatten', 'fastcore/foundation.py'),
395396
'fastcore.foundation.L.groupby': ('foundation.html#l.groupby', 'fastcore/foundation.py'),
396397
'fastcore.foundation.L.itemgot': ('foundation.html#l.itemgot', 'fastcore/foundation.py'),
398+
'fastcore.foundation.L.last': ('foundation.html#l.last', 'fastcore/foundation.py'),
397399
'fastcore.foundation.L.map': ('foundation.html#l.map', 'fastcore/foundation.py'),
398400
'fastcore.foundation.L.map_dict': ('foundation.html#l.map_dict', 'fastcore/foundation.py'),
399401
'fastcore.foundation.L.map_first': ('foundation.html#l.map_first', 'fastcore/foundation.py'),

fastcore/foundation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,20 @@ def rstarargwhere(self:L, f, negate=False):
343343
if negate: _f = not_(_f)
344344
return self._new(i for i,o in enumerate(self) if _f(o))
345345

346+
# %% ../nbs/02_foundation.ipynb #541e7eab-3267-4156-8574-5cff8eb1a4e1
347+
@patch
348+
@curryable
349+
def first(self:L, f, negate=False):
350+
"Return first matching item"
351+
return first(self, f, negate)
352+
353+
# %% ../nbs/02_foundation.ipynb #6fde986b-8d9b-469a-af0e-ebba11917375
354+
@patch
355+
@curryable
356+
def last(self:L, f, negate=False):
357+
"Return last matching item"
358+
return last(self, f, negate)
359+
346360
# %% ../nbs/02_foundation.ipynb #3e7fa556
347361
@patch
348362
@curryable

nbs/02_foundation.ipynb

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,102 @@
25102510
"test_eq(L((2,1),(1,3),(3,2)).rstarargwhere(lt, negate=True), [1])"
25112511
]
25122512
},
2513+
{
2514+
"cell_type": "code",
2515+
"execution_count": null,
2516+
"id": "541e7eab-3267-4156-8574-5cff8eb1a4e1",
2517+
"metadata": {},
2518+
"outputs": [],
2519+
"source": [
2520+
"#| export\n",
2521+
"@patch\n",
2522+
"@curryable\n",
2523+
"def first(self:L, f, negate=False):\n",
2524+
" \"Return first matching item\"\n",
2525+
" return first(self, f, negate)"
2526+
]
2527+
},
2528+
{
2529+
"cell_type": "code",
2530+
"execution_count": null,
2531+
"id": "966ffa22-7599-4ee8-ac54-d89eedda339e",
2532+
"metadata": {},
2533+
"outputs": [],
2534+
"source": [
2535+
"test_eq(t.first(lambda o:o>4), 99)\n",
2536+
"test_eq(t.first(lambda o:o>4,negate=True), 0)"
2537+
]
2538+
},
2539+
{
2540+
"cell_type": "code",
2541+
"execution_count": null,
2542+
"id": "1664acc6-45a4-4b00-aa20-e1bcc6e2a578",
2543+
"metadata": {},
2544+
"outputs": [
2545+
{
2546+
"data": {
2547+
"text/plain": [
2548+
"[8, 9, None]"
2549+
]
2550+
},
2551+
"execution_count": null,
2552+
"metadata": {},
2553+
"output_type": "execute_result"
2554+
}
2555+
],
2556+
"source": [
2557+
"nested = L([[1,2,8,4], [5,9,7], [1,1,1]])\n",
2558+
"nested.map(L.first(gt(5)))"
2559+
]
2560+
},
2561+
{
2562+
"cell_type": "code",
2563+
"execution_count": null,
2564+
"id": "6fde986b-8d9b-469a-af0e-ebba11917375",
2565+
"metadata": {},
2566+
"outputs": [],
2567+
"source": [
2568+
"#| export\n",
2569+
"@patch\n",
2570+
"@curryable\n",
2571+
"def last(self:L, f, negate=False):\n",
2572+
" \"Return last matching item\"\n",
2573+
" return last(self, f, negate)"
2574+
]
2575+
},
2576+
{
2577+
"cell_type": "code",
2578+
"execution_count": null,
2579+
"id": "6f2a7f52-c50b-4c98-a0d5-db300b3c7fb2",
2580+
"metadata": {},
2581+
"outputs": [],
2582+
"source": [
2583+
"test_eq(L(5,4,3,2,99,1).last(lambda o:o>4), 99)\n",
2584+
"test_eq(L(5,4,3,2,99,1).last(lambda o:o>4,negate=True), 1)"
2585+
]
2586+
},
2587+
{
2588+
"cell_type": "code",
2589+
"execution_count": null,
2590+
"id": "555824ef-b4b9-45eb-8e1e-24ded3490f1f",
2591+
"metadata": {},
2592+
"outputs": [
2593+
{
2594+
"data": {
2595+
"text/plain": [
2596+
"[8, 7, None]"
2597+
]
2598+
},
2599+
"execution_count": null,
2600+
"metadata": {},
2601+
"output_type": "execute_result"
2602+
}
2603+
],
2604+
"source": [
2605+
"nested = L([[1,2,8,4], [5,9,7], [1,1,1]])\n",
2606+
"nested.map(L.last(gt(5)))"
2607+
]
2608+
},
25132609
{
25142610
"cell_type": "code",
25152611
"execution_count": null,

0 commit comments

Comments
 (0)