blade.xmap¶
blade mapping operations
- blade.xmap.xargmap(iterable, callable, merge=False, *args)¶
Feed each items in iterable to callable as positional arguments.
Parameters: >>> 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]
- blade.xmap.xcopy(iterable)¶
Duplicate each items in iterable.
Parameters: iterable – iterable >>> from blade.xmap import xcopy >>> list(xcopy([[1, [2, 3]], [4, [5, 6]]])) [[1, [2, 3]], [4, [5, 6]]]
- blade.xmap.xinvoke(iterable, name, *args, **kw)¶
Feed global positional arguments and keyword arguments to each items in iterable‘s name method.
Parameters: >>> from blade.xmap import xinvoke >>> # invoke list.index() >>> list(xinvoke([[5, 1, 7], [3, 2, 1]], 'index', 1)) [1, 2] >>> # invoke list.sort() but return sorted list instead of None >>> list(xinvoke([[5, 1, 7], [3, 2, 1]], 'sort')) [[1, 5, 7], [1, 2, 3]]
- blade.xmap.xkeyvalue(iterable, callable=None, keys=False, values=False)¶
Run callable on incoming mapping things.
Parameters: >>> from blade.xmap import xkeyvalue >>> # filter items >>> list(xkeyvalue( ... [dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])], ... lambda x, y: x * y)) [2, 6, 12, 2, 6, 12] >>> # mapping keys only >>> list(xkeyvalue( ... [dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])], ... keys=True ... )) [1, 2, 3, 1, 2, 3] >>> # mapping values only >>> list(xkeyvalue( ... [dict([(1, 2), (2, 3), (3, 4)]), dict([(1, 2), (2, 3), (3, 4)])], ... values=True ... )) [2, 3, 4, 2, 3, 4]
- blade.xmap.xkwargmap(iterable, callable, merge=False, *args, **kw)¶
Feed each items in iterable as a tuple of positional arguments and keyword arguments to callable.
Parameters: - iterable – iterable
- callable – mapped callable
- merge (bool) – merge global positional or keyword params with positional and keyword arguments derived from items in iterable into a single 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]
- blade.xmap.xrepeat(iterable, callable=None, n=None)¶
Repeat items in iterable n times or invoke callable n times.
Parameters: >>> from blade.xmap import xrepeat >>> # repeat iterable >>> list(xrepeat([40, 50, 60], n=3)) [(40, 50, 60), (40, 50, 60), (40, 50, 60)] >>> # with callable >>> list(xrepeat([40, 50, 60], lambda *args: list(args), 3)) [[40, 50, 60], [40, 50, 60], [40, 50, 60]]