def filter(self, f, *args, **kwargs): """ Keeps only the elements where f(el)==True """ _f=partial(f, *args, **kwargs) self[:]=filter(_f, self) return self
def find(self, f, *args, **kwargs): """ Returns the first element matching the predicate 'f' else returns None """ _f=partial(f, *args, **kwargs) for el in self: if _f(el): return el
def map(self, f, *args, **kwargs): """ Applies 'f' to all elements returns 'self' """ _f=partial(f, *args, **kwargs) self[:]=map(_f, self) return self
def count(self, f, *args, **kwargs): """ Count pairs matching predicate """ _f=partial(f, *args, **kwargs) count=0 for k,v in self.items(): if _f(k,v): count=count+1 return count
def any(self, f, *args, **kwargs): """ Applies 'f' to all [k:v] pairs and expects at least 1 'True' result """ _f = partial(f, *args, **kwargs) for x, y in self.items(): if _f(x, y): return True return False
def all(self, f, *args, **kwargs): """ Applies 'f' to all [k:v] pairs and expects 'True' for all results """ _f = partial(f, *args, **kwargs) for x, y in self.items(): if not _f(x, y): return False return True
def all(self, f, *args, **kwargs): """ Applies 'f' to all elements and expects 'True' for all results """ _f=partial(f, *args, **kwargs) for el in self: if not _f(el): return False return True
def count(self, f, *args, **kwargs): """ Counts elements in the list matching 'f' predicate """ _f=partial(f, *args, **kwargs) count=0 for el in self: if _f(el): count=count+1 return count
def any(self, f, *args, **kwargs): """ Applies 'f' to all elements and expects at least 1 'True' result """ _f=partial(f, *args, **kwargs) for el in self: if _f(el): return True return False
def map(self, f, *args, **kwargs): """ Applies 'f' for each [k,v] pair - 'f' must return (k,v) pair """ #_f = self._f(f, *args, **kwargs) _f=partial(f, *args, **kwargs) d=dic() for k,v in self.items(): nk, nv=_f(k,v) d[nk]=nv self.clear() return self.clear().update(d)
def tee(self, f, *args, **kwargs): """ Calls 'f' on self, returns self, keeps 'last value' """ self.last_value=partial(f, *args, **kwargs)(self) return self
def do(self, f, *args, **kwargs): """ Calls 'f' on self, returns result """ return partial(f, *args, **kwargs)(self)
def reduce(self, f, *args, **kwargs): """ Reduce """ _f=partial(f, *args, **kwargs) return reduce(_f, self)