@@ -464,7 +464,7 @@ def rec_map_reduce_array_container(
464
464
465
465
or any other such traversal.
466
466
"""
467
- def rec (_ary : ArrayOrContainerT ) -> ArrayOrContainerT :
467
+ def rec (_ary : ArrayOrContainerT ) -> Optional [ ArrayOrContainerT ] :
468
468
if type (_ary ) is leaf_class :
469
469
return map_func (_ary )
470
470
else :
@@ -473,11 +473,22 @@ def rec(_ary: ArrayOrContainerT) -> ArrayOrContainerT:
473
473
except NotAnArrayContainerError :
474
474
return map_func (_ary )
475
475
else :
476
- return reduce_func ([
477
- rec (subary ) for _ , subary in iterable
478
- ])
476
+ subary_results = [
477
+ rec (subary ) for _ , subary in iterable ]
478
+ filtered_subary_results = [
479
+ result for result in subary_results
480
+ if result is not None ]
481
+ if len (filtered_subary_results ) > 0 :
482
+ return reduce_func (filtered_subary_results )
483
+ else :
484
+ return None
479
485
480
- return rec (ary )
486
+ result = rec (ary )
487
+
488
+ if result is None :
489
+ raise ValueError ("cannot reduce empty array container" )
490
+
491
+ return result
481
492
482
493
483
494
def rec_multimap_reduce_array_container (
@@ -503,12 +514,23 @@ def rec_multimap_reduce_array_container(
503
514
# NOTE: this wrapper matches the signature of `deserialize_container`
504
515
# to make plugging into `_multimap_array_container_impl` easier
505
516
def _reduce_wrapper (ary : ContainerT , iterable : Iterable [Tuple [Any , Any ]]) -> Any :
506
- return reduce_func ([subary for _ , subary in iterable ])
517
+ filtered_subary_results = [
518
+ result for _ , result in iterable
519
+ if result is not None ]
520
+ if len (filtered_subary_results ) > 0 :
521
+ return reduce_func (filtered_subary_results )
522
+ else :
523
+ return None
507
524
508
- return _multimap_array_container_impl (
525
+ result = _multimap_array_container_impl (
509
526
map_func , * args ,
510
527
reduce_func = _reduce_wrapper , leaf_cls = leaf_class , recursive = True )
511
528
529
+ if result is None :
530
+ raise ValueError ("cannot reduce empty array container" )
531
+
532
+ return result
533
+
512
534
# }}}
513
535
514
536
0 commit comments