Skip to content

Commit 31819c9

Browse files
committed
export
1 parent 953d2af commit 31819c9

4 files changed

Lines changed: 369 additions & 453 deletions

File tree

fastcore/_modidx.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,32 +342,59 @@
342342
'fastcore.foundation.L._new': ('foundation.html#l._new', 'fastcore/foundation.py'),
343343
'fastcore.foundation.L._repr_pretty_': ('foundation.html#l._repr_pretty_', 'fastcore/foundation.py'),
344344
'fastcore.foundation.L._xtra': ('foundation.html#l._xtra', 'fastcore/foundation.py'),
345+
'fastcore.foundation.L.accumulate': ('foundation.html#l.accumulate', 'fastcore/foundation.py'),
345346
'fastcore.foundation.L.argfirst': ('foundation.html#l.argfirst', 'fastcore/foundation.py'),
346347
'fastcore.foundation.L.argwhere': ('foundation.html#l.argwhere', 'fastcore/foundation.py'),
347348
'fastcore.foundation.L.attrgot': ('foundation.html#l.attrgot', 'fastcore/foundation.py'),
349+
'fastcore.foundation.L.batched': ('foundation.html#l.batched', 'fastcore/foundation.py'),
350+
'fastcore.foundation.L.combinations': ('foundation.html#l.combinations', 'fastcore/foundation.py'),
351+
'fastcore.foundation.L.compress': ('foundation.html#l.compress', 'fastcore/foundation.py'),
348352
'fastcore.foundation.L.concat': ('foundation.html#l.concat', 'fastcore/foundation.py'),
349353
'fastcore.foundation.L.copy': ('foundation.html#l.copy', 'fastcore/foundation.py'),
350354
'fastcore.foundation.L.cycle': ('foundation.html#l.cycle', 'fastcore/foundation.py'),
355+
'fastcore.foundation.L.dropwhile': ('foundation.html#l.dropwhile', 'fastcore/foundation.py'),
351356
'fastcore.foundation.L.enumerate': ('foundation.html#l.enumerate', 'fastcore/foundation.py'),
352357
'fastcore.foundation.L.filter': ('foundation.html#l.filter', 'fastcore/foundation.py'),
358+
'fastcore.foundation.L.flatten': ('foundation.html#l.flatten', 'fastcore/foundation.py'),
353359
'fastcore.foundation.L.groupby': ('foundation.html#l.groupby', 'fastcore/foundation.py'),
354360
'fastcore.foundation.L.itemgot': ('foundation.html#l.itemgot', 'fastcore/foundation.py'),
355361
'fastcore.foundation.L.map': ('foundation.html#l.map', 'fastcore/foundation.py'),
356362
'fastcore.foundation.L.map_dict': ('foundation.html#l.map_dict', 'fastcore/foundation.py'),
357363
'fastcore.foundation.L.map_first': ('foundation.html#l.map_first', 'fastcore/foundation.py'),
358364
'fastcore.foundation.L.map_zip': ('foundation.html#l.map_zip', 'fastcore/foundation.py'),
359365
'fastcore.foundation.L.map_zipwith': ('foundation.html#l.map_zipwith', 'fastcore/foundation.py'),
366+
'fastcore.foundation.L.pairwise': ('foundation.html#l.pairwise', 'fastcore/foundation.py'),
367+
'fastcore.foundation.L.partition': ('foundation.html#l.partition', 'fastcore/foundation.py'),
368+
'fastcore.foundation.L.permutations': ('foundation.html#l.permutations', 'fastcore/foundation.py'),
360369
'fastcore.foundation.L.product': ('foundation.html#l.product', 'fastcore/foundation.py'),
361370
'fastcore.foundation.L.range': ('foundation.html#l.range', 'fastcore/foundation.py'),
362371
'fastcore.foundation.L.reduce': ('foundation.html#l.reduce', 'fastcore/foundation.py'),
363372
'fastcore.foundation.L.renumerate': ('foundation.html#l.renumerate', 'fastcore/foundation.py'),
373+
'fastcore.foundation.L.rstarargfirst': ('foundation.html#l.rstarargfirst', 'fastcore/foundation.py'),
374+
'fastcore.foundation.L.rstarargwhere': ('foundation.html#l.rstarargwhere', 'fastcore/foundation.py'),
375+
'fastcore.foundation.L.rstardropwhile': ('foundation.html#l.rstardropwhile', 'fastcore/foundation.py'),
376+
'fastcore.foundation.L.rstarfilter': ('foundation.html#l.rstarfilter', 'fastcore/foundation.py'),
377+
'fastcore.foundation.L.rstarmap': ('foundation.html#l.rstarmap', 'fastcore/foundation.py'),
378+
'fastcore.foundation.L.rstarpartition': ('foundation.html#l.rstarpartition', 'fastcore/foundation.py'),
379+
'fastcore.foundation.L.rstarreduce': ('foundation.html#l.rstarreduce', 'fastcore/foundation.py'),
380+
'fastcore.foundation.L.rstarsorted': ('foundation.html#l.rstarsorted', 'fastcore/foundation.py'),
381+
'fastcore.foundation.L.rstartakewhile': ('foundation.html#l.rstartakewhile', 'fastcore/foundation.py'),
364382
'fastcore.foundation.L.setattrs': ('foundation.html#l.setattrs', 'fastcore/foundation.py'),
365383
'fastcore.foundation.L.shuffle': ('foundation.html#l.shuffle', 'fastcore/foundation.py'),
366384
'fastcore.foundation.L.sorted': ('foundation.html#l.sorted', 'fastcore/foundation.py'),
367385
'fastcore.foundation.L.split': ('foundation.html#l.split', 'fastcore/foundation.py'),
368386
'fastcore.foundation.L.splitlines': ('foundation.html#l.splitlines', 'fastcore/foundation.py'),
387+
'fastcore.foundation.L.starargfirst': ('foundation.html#l.starargfirst', 'fastcore/foundation.py'),
388+
'fastcore.foundation.L.starargwhere': ('foundation.html#l.starargwhere', 'fastcore/foundation.py'),
389+
'fastcore.foundation.L.stardropwhile': ('foundation.html#l.stardropwhile', 'fastcore/foundation.py'),
390+
'fastcore.foundation.L.starfilter': ('foundation.html#l.starfilter', 'fastcore/foundation.py'),
369391
'fastcore.foundation.L.starmap': ('foundation.html#l.starmap', 'fastcore/foundation.py'),
392+
'fastcore.foundation.L.starpartition': ('foundation.html#l.starpartition', 'fastcore/foundation.py'),
393+
'fastcore.foundation.L.starreduce': ('foundation.html#l.starreduce', 'fastcore/foundation.py'),
394+
'fastcore.foundation.L.starsorted': ('foundation.html#l.starsorted', 'fastcore/foundation.py'),
395+
'fastcore.foundation.L.startakewhile': ('foundation.html#l.startakewhile', 'fastcore/foundation.py'),
370396
'fastcore.foundation.L.sum': ('foundation.html#l.sum', 'fastcore/foundation.py'),
397+
'fastcore.foundation.L.takewhile': ('foundation.html#l.takewhile', 'fastcore/foundation.py'),
371398
'fastcore.foundation.L.unique': ('foundation.html#l.unique', 'fastcore/foundation.py'),
372399
'fastcore.foundation.L.val2idx': ('foundation.html#l.val2idx', 'fastcore/foundation.py'),
373400
'fastcore.foundation.L.zip': ('foundation.html#l.zip', 'fastcore/foundation.py'),

fastcore/foundation.py

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ def filter(self:L, f=noop, negate=False, **kwargs):
202202
"Create new `L` filtered by predicate `f`, passing `args` and `kwargs` to `f`"
203203
return self._new(filter_ex(self, f=f, negate=negate, gen=False, **kwargs))
204204

205+
# %% ../nbs/02_foundation.ipynb
206+
@patch
207+
def starfilter(self:L, f, negate=False, **kwargs):
208+
"Like `filter`, but unpacks elements as args to `f`"
209+
_f = lambda x: f(*x, **kwargs)
210+
if negate: _f = not_(_f)
211+
return self._new(filter(_f, self))
212+
213+
# %% ../nbs/02_foundation.ipynb
214+
@patch
215+
def rstarfilter(self:L, f, negate=False, **kwargs):
216+
"Like `starfilter`, but reverse the order of args"
217+
_f = lambda x: f(*x[::-1], **kwargs)
218+
if negate: _f = not_(_f)
219+
return self._new(filter(_f, self))
220+
205221
# %% ../nbs/02_foundation.ipynb
206222
@patch(cls_method=True)
207223
def range(cls:L, a, b=None, step=None):
@@ -214,6 +230,22 @@ def argwhere(self:L, f, negate=False, **kwargs):
214230
"Like `filter`, but return indices for matching items"
215231
return self._new(argwhere(self, f, negate, **kwargs))
216232

233+
# %% ../nbs/02_foundation.ipynb
234+
@patch
235+
def starargwhere(self:L, f, negate=False):
236+
"Like `argwhere`, but unpacks elements as args to `f`"
237+
_f = lambda x: f(*x)
238+
if negate: _f = not_(_f)
239+
return self._new(i for i,o in enumerate(self) if _f(o))
240+
241+
# %% ../nbs/02_foundation.ipynb
242+
@patch
243+
def rstarargwhere(self:L, f, negate=False):
244+
"Like `starargwhere`, but reverse the order of args"
245+
_f = lambda x: f(*x[::-1])
246+
if negate: _f = not_(_f)
247+
return self._new(i for i,o in enumerate(self) if _f(o))
248+
217249
# %% ../nbs/02_foundation.ipynb
218250
@patch
219251
def enumerate(self:L):
@@ -233,6 +265,22 @@ def argfirst(self:L, f, negate=False):
233265
if negate: f = not_(f)
234266
return first(i for i,o in self.enumerate() if f(o))
235267

268+
# %% ../nbs/02_foundation.ipynb
269+
@patch
270+
def starargfirst(self:L, f, negate=False):
271+
"Like `argfirst`, but unpacks elements as args to `f`"
272+
_f = lambda x: f(*x)
273+
if negate: _f = not_(_f)
274+
return first(i for i,o in self.enumerate() if _f(o))
275+
276+
# %% ../nbs/02_foundation.ipynb
277+
@patch
278+
def rstarargfirst(self:L, f, negate=False):
279+
"Like `starargfirst`, but reverse the order of args"
280+
_f = lambda x: f(*x[::-1])
281+
if negate: _f = not_(_f)
282+
return first(i for i,o in self.enumerate() if _f(o))
283+
236284
# %% ../nbs/02_foundation.ipynb
237285
@patch
238286
def map(self:L, f, *args, **kwargs):
@@ -245,6 +293,12 @@ def starmap(self:L, f, *args, **kwargs):
245293
"Like `map`, but use `itertools.starmap`"
246294
return self._new(itertools.starmap(partial(f,*args,**kwargs), self))
247295

296+
# %% ../nbs/02_foundation.ipynb
297+
@patch
298+
def rstarmap(self:L, f, *args, **kwargs):
299+
"Like `starmap`, but reverse the order of args"
300+
return self._new(itertools.starmap(lambda *x: f(*x[::-1], *args, **kwargs), self))
301+
248302
# %% ../nbs/02_foundation.ipynb
249303
@patch
250304
def map_dict(self:L, f=noop, *args, **kwargs):
@@ -295,6 +349,18 @@ def sorted(self:L, key=None, reverse=False, cmp=None, **kwargs):
295349
"New `L` sorted by `key`, using `sort_ex`. If key is str use `attrgetter`; if int use `itemgetter`"
296350
return self._new(sorted_ex(self, key=key, reverse=reverse, cmp=cmp, **kwargs))
297351

352+
# %% ../nbs/02_foundation.ipynb
353+
@patch
354+
def starsorted(self:L, key, reverse=False):
355+
"Like `sorted`, but unpacks elements as args to `key`"
356+
return self._new(sorted(self, key=lambda x: key(*x), reverse=reverse))
357+
358+
# %% ../nbs/02_foundation.ipynb
359+
@patch
360+
def rstarsorted(self:L, key, reverse=False):
361+
"Like `starsorted`, but reverse the order of args"
362+
return self._new(sorted(self, key=lambda x: key(*x[::-1]), reverse=reverse))
363+
298364
# %% ../nbs/02_foundation.ipynb
299365
@patch
300366
def concat(self:L):
@@ -321,6 +387,20 @@ def reduce(self:L, f, initial=None):
321387
"Wrapper for `functools.reduce`"
322388
return reduce(f, self) if initial is None else reduce(f, self, initial)
323389

390+
# %% ../nbs/02_foundation.ipynb
391+
@patch
392+
def starreduce(self:L, f, initial=None):
393+
"Like `reduce`, but unpacks elements as args to `f`"
394+
_f = lambda acc, x: f(acc, *x)
395+
return reduce(_f, self) if initial is None else reduce(_f, self, initial)
396+
397+
# %% ../nbs/02_foundation.ipynb
398+
@patch
399+
def rstarreduce(self:L, f, initial=None):
400+
"Like `starreduce`, but reverse the order of unpacked args"
401+
_f = lambda acc, x: f(acc, *x[::-1])
402+
return reduce(_f, self) if initial is None else reduce(_f, self, initial)
403+
324404
# %% ../nbs/02_foundation.ipynb
325405
@patch
326406
def sum(self:L):
@@ -351,6 +431,113 @@ def cycle(self:L):
351431
"Same as `itertools.cycle`"
352432
return cycle(self)
353433

434+
# %% ../nbs/02_foundation.ipynb
435+
@patch
436+
def takewhile(self:L, f):
437+
"Same as `itertools.takewhile`"
438+
return self._new(itertools.takewhile(f, self))
439+
440+
# %% ../nbs/02_foundation.ipynb
441+
@patch
442+
def dropwhile(self:L, f):
443+
"Same as `itertools.dropwhile`"
444+
return self._new(itertools.dropwhile(f, self))
445+
446+
# %% ../nbs/02_foundation.ipynb
447+
@patch
448+
def startakewhile(self:L, f):
449+
"Like `takewhile`, but unpacks elements as args to `f`"
450+
return self._new(itertools.takewhile(lambda x: f(*x), self))
451+
452+
# %% ../nbs/02_foundation.ipynb
453+
@patch
454+
def rstartakewhile(self:L, f):
455+
"Like `startakewhile`, but reverse the order of args"
456+
return self._new(itertools.takewhile(lambda x: f(*x[::-1]), self))
457+
458+
# %% ../nbs/02_foundation.ipynb
459+
@patch
460+
def stardropwhile(self:L, f):
461+
"Like `dropwhile`, but unpacks elements as args to `f`"
462+
return self._new(itertools.dropwhile(lambda x: f(*x), self))
463+
464+
# %% ../nbs/02_foundation.ipynb
465+
@patch
466+
def rstardropwhile(self:L, f):
467+
"Like `stardropwhile`, but reverse the order of args"
468+
return self._new(itertools.dropwhile(lambda x: f(*x[::-1]), self))
469+
470+
# %% ../nbs/02_foundation.ipynb
471+
@patch
472+
def accumulate(self:L, f=operator.add, initial=None):
473+
"Same as `itertools.accumulate`"
474+
return self._new(itertools.accumulate(self, f, initial=initial))
475+
476+
# %% ../nbs/02_foundation.ipynb
477+
@patch
478+
def pairwise(self:L):
479+
"Same as `itertools.pairwise`"
480+
return self._new(itertools.pairwise(self))
481+
482+
# %% ../nbs/02_foundation.ipynb
483+
@patch
484+
def batched(self:L, n):
485+
"Same as `itertools.batched`"
486+
return self._new(itertools.batched(self, n))
487+
488+
# %% ../nbs/02_foundation.ipynb
489+
@patch
490+
def compress(self:L, selectors):
491+
"Same as `itertools.compress`"
492+
return self._new(itertools.compress(self, selectors))
493+
494+
# %% ../nbs/02_foundation.ipynb
495+
@patch
496+
def permutations(self:L, r=None):
497+
"Same as `itertools.permutations`"
498+
return self._new(itertools.permutations(self, r))
499+
500+
# %% ../nbs/02_foundation.ipynb
501+
@patch
502+
def combinations(self:L, r):
503+
"Same as `itertools.combinations`"
504+
return self._new(itertools.combinations(self, r))
505+
506+
# %% ../nbs/02_foundation.ipynb
507+
@patch
508+
def partition(self:L, f=noop, **kwargs):
509+
"Split into two `L`s based on predicate `f`: (true_items, false_items)"
510+
a,b = [],[]
511+
for o in self: (a if f(o, **kwargs) else b).append(o)
512+
return self._new(a),self._new(b)
513+
514+
515+
# %% ../nbs/02_foundation.ipynb
516+
@patch
517+
def starpartition(self:L, f, **kwargs):
518+
"Like `partition`, but unpacks elements as args to `f`"
519+
a,b = [],[]
520+
for o in self: (a if f(*o, **kwargs) else b).append(o)
521+
return self._new(a),self._new(b)
522+
523+
# %% ../nbs/02_foundation.ipynb
524+
@patch
525+
def rstarpartition(self:L, f, **kwargs):
526+
"Like `starpartition`, but reverse the order of args"
527+
a,b = [],[]
528+
for o in self: (a if f(*o[::-1], **kwargs) else b).append(o)
529+
return self._new(a),self._new(b)
530+
531+
# %% ../nbs/02_foundation.ipynb
532+
@patch
533+
def flatten(self:L):
534+
"Recursively flatten nested iterables (except strings)"
535+
def _flatten(o):
536+
for item in o:
537+
if isinstance(item, (str,bytes)) or not hasattr(item,'__iter__'): yield item
538+
else: yield from _flatten(item)
539+
return self._new(_flatten(self))
540+
354541
# %% ../nbs/02_foundation.ipynb
355542
def save_config_file(file, d, **kwargs):
356543
"Write settings dict to a new config file, or overwrite the existing one."

0 commit comments

Comments
 (0)