|
50 | 50 | from tree import flatten_with_path_up_to
|
51 | 51 | from tree import is_nested
|
52 | 52 | from tree import map_structure as dm_tree_map_structure
|
53 |
| -from tree import map_structure_up_to |
54 |
| -from tree import map_structure_with_path |
55 |
| -from tree import map_structure_with_path_up_to |
| 53 | +from tree import map_structure_with_path_up_to as dm_tree_map_structure_with_path_up_to |
56 | 54 | from tree import unflatten_as
|
57 | 55 | # pylint: enable=unused-import
|
58 | 56 |
|
@@ -213,6 +211,35 @@ def flatten_with_tuple_paths_up_to(shallow_structure,
|
213 | 211 | check_types)
|
214 | 212 |
|
215 | 213 |
|
| 214 | +def map_structure_up_to(shallow_structure, func, *structures, **kwargs): |
| 215 | + return map_structure_with_path_up_to( |
| 216 | + shallow_structure, |
| 217 | + lambda _, *args: func(*args), # Discards path. |
| 218 | + *structures, |
| 219 | + **kwargs) |
| 220 | + |
| 221 | + |
| 222 | +def map_structure_with_path(func, *structures, **kwargs): |
| 223 | + return map_structure_with_path_up_to(structures[0], func, *structures, |
| 224 | + **kwargs) |
| 225 | + |
| 226 | + |
| 227 | +def map_structure_with_path_up_to(shallow_structure, func, *structures, |
| 228 | + **kwargs): |
| 229 | + """Wraps nest.map_structure_with_path_up_to, with structure/type checking.""" |
| 230 | + if not structures: |
| 231 | + raise ValueError('Cannot map over no sequences') |
| 232 | + |
| 233 | + check_types = kwargs.get('check_types', True) |
| 234 | + |
| 235 | + for input_tree in structures: |
| 236 | + _assert_shallow_structure( |
| 237 | + shallow_structure, input_tree, check_types=check_types) |
| 238 | + |
| 239 | + return dm_tree_map_structure_with_path_up_to( |
| 240 | + shallow_structure, func, *structures, **kwargs) |
| 241 | + |
| 242 | + |
216 | 243 | def map_structure_with_tuple_paths(func, *structures, **kwargs):
|
217 | 244 | return map_structure_with_path(func, *structures, **kwargs)
|
218 | 245 |
|
|
0 commit comments