Recursively apply a function to every pair of elements in two containers with the same nested structure.
(data1, data2, fn, *args, **kwargs)
| 1136 | |
| 1137 | |
| 1138 | def recursive_apply_pair(data1, data2, fn, *args, **kwargs): |
| 1139 | """Recursively apply a function to every pair of elements in two containers with the |
| 1140 | same nested structure. |
| 1141 | """ |
| 1142 | if isinstance(data1, Mapping) and isinstance(data2, Mapping): |
| 1143 | return { |
| 1144 | k: recursive_apply_pair(data1[k], data2[k], fn, *args, **kwargs) |
| 1145 | for k in data1.keys() |
| 1146 | } |
| 1147 | elif isinstance(data1, tuple) and isinstance(data2, tuple): |
| 1148 | return tuple( |
| 1149 | recursive_apply_pair(x, y, fn, *args, **kwargs) |
| 1150 | for x, y in zip(data1, data2) |
| 1151 | ) |
| 1152 | elif is_listlike(data1) and is_listlike(data2): |
| 1153 | return [ |
| 1154 | recursive_apply_pair(x, y, fn, *args, **kwargs) |
| 1155 | for x, y in zip(data1, data2) |
| 1156 | ] |
| 1157 | else: |
| 1158 | return fn(data1, data2, *args, **kwargs) |
| 1159 | |
| 1160 | |
| 1161 | def context_of(data): |
no test coverage detected