def xkwargmap(iterable, callable, merge=False, *args, **kw): ''' Feed each items in `iterable` as a :func:`tuple` of :term:`positional argument`\s and :term:`keyword argument`\s to `callable`. :argument iterable: :term:`iterable` :argument callable: mapped :func:`callable` :keyword bool merge: merge global positional or keyword :meth:`params` with positional and keyword arguments derived from items in `iterable` into a single :func:`tuple` of wildcard positional and keyword arguments like ``(*iterable_args + global_args, **global_kwargs + iterable_kwargs)`` when passed to `callable` >>> from blade.xmap import xkwargmap >>> # default behavior >>> test = [((1, 2), {'a': 2}), ((2, 3), {'a': 2}), ((3, 4), {'a': 2})] >>> def tester(*args, **kw): ... return sum(args) * sum(kw.values()) >>> list(xkwargmap(test, tester)) [6, 10, 14] >>> # merging global and iterable derived positional and keyword args >>> list(xkwargmap(test, tester, True, 1, 2, 3, b=5, w=10, y=13)) [270, 330, 390] ''' if merge: def kwargmap(callable, *params): arg, kwarg = params kwarg.update(kw) return callable(*(arg + args), **kwarg) else: kwargmap = lambda f, x, y: f(*x, **y) return partstar(kwargmap, iterable, callable)
def xargmap(iterable, callable, merge=False, *args): ''' Feed each items in `iterable` to `callable` as :term:`positional argument`\s. :argument iterable: :term:`iterable` :argument callable: mapped :func:`callable` :keyword bool merge: merge global positional :meth:`params` with positional arguments derived from items in `iterable` when passed to `callable` >>> from blade.xmap import xargmap >>> # default behavior >>> list(xargmap([(1, 2), (2, 3), (3, 4)], lambda x, y: x * y)) [2, 6, 12] >>> # merge global positional arguments with iterable arguments >>> list(xargmap( ... [(1, 2), (2, 3), (3, 4)], ... lambda x, y, z, a, b: x * y * z * a * b, ... True, ... 7, 8, 9, ... )) [1008, 3024, 6048] ''' if merge: return partstar(lambda f, *arg: f(*(arg + args)), iterable, callable) return starmap(callable, iterable)