Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compose(*fs):
"""Composes passed functions."""
if fs:
pair = lambda f, g: lambda *a, **kw: f(g(*a, **kw))
return reduce(pair, map(make_func, fs))
else:
return identity
def map(f, *seqs):
"""An extended version of builtin imap().
Derives a mapper from string, int, slice, dict or set."""
return _map(make_func(f, builtin=PY2), *seqs)
def group_by(f, seq):
"""Groups given sequence items into a mapping f(item) -> [item, ...]."""
f = make_func(f)
result = defaultdict(list)
for item in seq:
result[f(item)].append(item)
return result
def partition_by(f, seq):
"""Lazily partition seq into continuous chunks with constant value of f."""
f = make_func(f)
for _, items in groupby(seq, f):
yield items
def walk_keys(f, coll):
"""Walks keys of the collection, mapping them with f."""
f = make_func(f)
# NOTE: we use this awkward construct instead of lambda to be Python 3 compatible
def pair_f(pair):
k, v = pair
return f(k), v
return walk(pair_f, coll)
def iffy(pred, action=EMPTY, default=identity):
"""Creates a function, which conditionally applies action or default."""
if action is EMPTY:
return iffy(bool, pred, default)
else:
pred = make_pred(pred)
action = make_func(action)
return lambda v: action(v) if pred(v) else \
default(v) if callable(default) else \
default
def xmap(f, *seqs):
return _map(make_func(f), *seqs)
else: