@@ -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 )
207223def 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
219251def 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
238286def 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
250304def 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
300366def 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
326406def 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
355542def save_config_file (file , d , ** kwargs ):
356543 "Write settings dict to a new config file, or overwrite the existing one."
0 commit comments