MCPcopy Create free account
hub / github.com/ActiveState/code / apply

Function apply

recipes/Python/577515_interval_mapping/recipe-577515.py:252–289  ·  view source on GitHub ↗

Return a new IntervalMapping applying func to all values of mappings. For example, apply(lambda x, y: x + y, m1, m2) returns a new matching with the sum of values for m1 and m2. IntervalMapping.nothing is passed to func when the value is undefined. >>> m1 = IntervalMapping(

(func, *mappings)

Source from the content-addressed store, hash-verified

250 values[i] = nothing
251
252def apply(func, *mappings):
253 """Return a new IntervalMapping applying func to all values of mappings.
254
255 For example, apply(lambda x, y: x + y, m1, m2) returns a new
256 matching with the sum of values for m1 and
257 m2. IntervalMapping.nothing is passed to func when the value is
258 undefined.
259
260 >>> m1 = IntervalMapping()
261 >>> m2 = IntervalMapping()
262 >>> m1[:] = m2[:] = 0 # avoid problems with undefined values
263 >>> m1[0:2] = 1
264 >>> m2[1:3] = 2
265 >>> m3 = apply(lambda a, b: a + b, m1, m2)
266 >>> m3[-1], m3[0], m3[1], m3[2], m3[3]
267 (0, 1, 3, 2, 0)
268 """
269
270 values = [m.leftmost() for m in mappings]
271
272 def changes():
273
274 def start_i_value(i_m):
275 i, m = i_m
276 res = ((k.start, i, v) for k, v in m.iteritems(True))
277 next(res)
278 return res
279 change_points = merge(*map(start_i_value, enumerate(mappings)))
280
281 lastbound = None
282 for bound, i, v in change_points:
283 values[i] = v
284 if bound != lastbound:
285 yield bound, func(*values)
286 lastbound = bound
287 yield bound, func(*values)
288
289 return IntervalMapping.from_changes(func(*values), changes())
290
291if __name__ == '__main__':
292 import doctest

Callers 15

__call__Method · 0.50
__init__Method · 0.50
recipe-121965.pyFile · 0.50
__call__Method · 0.50
writeMethod · 0.50
registerMethod · 0.50
__call__Method · 0.50
RunScriptMethod · 0.50
__init__Method · 0.50
_functionProxyFunction · 0.50
__call__Method · 0.50
_scrollMethod · 0.50

Calls 4

changesFunction · 0.85
leftmostMethod · 0.80
from_changesMethod · 0.80
funcFunction · 0.50

Tested by

no test coverage detected