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)
| 250 | values[i] = nothing |
| 251 | |
| 252 | def 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 | |
| 291 | if __name__ == '__main__': |
| 292 | import doctest |
no test coverage detected