@@ -406,7 +406,7 @@ extension Graph {
406
406
///
407
407
/// - Parameters:
408
408
/// - keyPath: The key path to use for the root node when passing it to
409
- /// `body`.
409
+ /// `body`.
410
410
/// - body: A closure that is invoked once per element in the graph. The
411
411
/// key path and leaf value of each node are passed to the closure.
412
412
///
@@ -485,18 +485,18 @@ extension Graph {
485
485
///
486
486
/// - Parameters:
487
487
/// - transform: A closure that is invoked once per element in the graph.
488
- /// The leaf value of each node is passed to this closure and its result
489
- /// is used as the corresponding value in the new graph. If the result is
490
- /// `nil`, the node and all of its child nodes are omitted from the new
491
- /// graph.
488
+ /// The key path and leaf value of each node are passed to this closure
489
+ /// and its result is used as the corresponding value in the new graph. If
490
+ /// the result is `nil`, the node and all of its child nodes are omitted
491
+ /// from the new graph.
492
492
///
493
493
/// - Returns: A graph containing the transformed nodes of this graph at the
494
494
/// same key paths, with `nil` values omitted.
495
495
///
496
496
/// - Throws: Whatever is thrown by `transform`.
497
497
///
498
498
/// This function iterates depth-first.
499
- func compactMapValues< U> ( _ transform: ( V ) throws -> U ? ) rethrows -> Graph < K , U > ? {
499
+ func compactMapValues< U> ( _ transform: ( Element ) throws -> U ? ) rethrows -> Graph < K , U > ? {
500
500
try compactMapValues {
501
501
try transform ( $0) . map { ( $0, false ) }
502
502
}
@@ -507,18 +507,18 @@ extension Graph {
507
507
///
508
508
/// - Parameters:
509
509
/// - transform: A closure that is invoked once per element in the graph.
510
- /// The leaf value of each node is passed to this closure and its result
511
- /// is used as the corresponding value in the new graph. If the result is
512
- /// `nil`, the node and all of its child nodes are omitted from the new
513
- /// graph.
510
+ /// The key path and leaf value of each node are passed to this closure
511
+ /// and its result is used as the corresponding value in the new graph. If
512
+ /// the result is `nil`, the node and all of its child nodes are omitted
513
+ /// from the new graph.
514
514
///
515
515
/// - Returns: A graph containing the transformed nodes of this graph at the
516
516
/// same key paths, with `nil` values omitted.
517
517
///
518
518
/// - Throws: Whatever is thrown by `transform`.
519
519
///
520
520
/// This function iterates depth-first.
521
- func compactMapValues< U> ( _ transform: ( V ) async throws -> U ? ) async rethrows -> Graph < K , U > ? {
521
+ func compactMapValues< U> ( _ transform: ( Element ) async throws -> U ? ) async rethrows -> Graph < K , U > ? {
522
522
try await compactMapValues {
523
523
try await transform ( $0) . map { ( $0, false ) }
524
524
}
@@ -530,31 +530,54 @@ extension Graph {
530
530
///
531
531
/// - Parameters:
532
532
/// - transform: A closure that is invoked once per element in the graph.
533
- /// The leaf value of each node is passed to this closure. The result of
534
- /// the closure is a tuple containing the new value and specifying whether
535
- /// or not the new value should also be applied to each descendant node.
536
- /// If `true`, `transform` is not invoked for those descendant nodes. If
537
- /// the result is `nil`, the node and all of its child nodes are omitted
538
- /// from the new graph.
533
+ /// The key path and leaf value of each node are passed to this closure.
534
+ /// The result of the closure is a tuple containing the new value and
535
+ /// specifying whether or not the new value should also be applied to each
536
+ /// descendant node. If `true`, `transform` is not invoked for those
537
+ /// descendant nodes. If the result is `nil`, the node and all of its
538
+ /// child nodes are omitted from the new graph.
539
539
///
540
540
/// - Returns: A graph containing the transformed nodes of this graph at the
541
541
/// same key paths, with `nil` values omitted.
542
542
///
543
543
/// - Throws: Whatever is thrown by `transform`.
544
544
///
545
545
/// This function iterates depth-first.
546
- func compactMapValues< U> ( _ transform: ( V ) throws -> ( U , recursivelyApply: Bool ) ? ) rethrows -> Graph < K , U > ? {
547
- guard let ( newValue, recursivelyApply) = try transform ( value) else {
546
+ func compactMapValues< U> ( _ transform: ( Element ) throws -> ( U , recursivelyApply: Bool ) ? ) rethrows -> Graph < K , U > ? {
547
+ try _compactMapValues ( keyPath: [ ] ) {
548
+ try transform ( ( $0, $1) )
549
+ }
550
+ }
551
+
552
+ /// The recursive implementation of `compactMapValues(_:)`.
553
+ ///
554
+ /// - Parameters:
555
+ /// - keyPath: The key path to use for the root node when passing it to
556
+ /// `transform`.
557
+ /// - transform: A closure that is invoked once per element in the graph.
558
+ /// The key path and leaf value of each node are passed to this closure.
559
+ /// The result of the closure is a tuple containing the new value and
560
+ /// specifying whether or not the new value should also be applied to each
561
+ /// descendant node. If `true`, `transform` is not invoked for those
562
+ /// descendant nodes. If the result is `nil`, the node and all of its
563
+ /// child nodes are omitted from the new graph.
564
+ ///
565
+ /// - Throws: Whatever is thrown by `transform`.
566
+ private func _compactMapValues< U> ( keyPath: [ K ] , _ transform: ( Element ) throws -> ( U , recursivelyApply: Bool ) ? ) rethrows -> Graph < K , U > ? {
567
+ guard let ( newValue, recursivelyApply) = try transform ( ( keyPath, value) ) else {
548
568
return nil
549
569
}
550
570
551
571
var newChildren = [ K : Graph < K , U > ] ( )
552
572
newChildren. reserveCapacity ( children. count)
553
573
for (key, child) in children {
574
+ var childKeyPath = keyPath
575
+ childKeyPath. append ( key)
576
+
554
577
if recursivelyApply {
555
- newChildren [ key] = child. compactMapValues { _ in ( newValue, true ) }
578
+ newChildren [ key] = child. _compactMapValues ( keyPath : childKeyPath ) { _ in ( newValue, true ) }
556
579
} else {
557
- newChildren [ key] = try child. compactMapValues ( transform)
580
+ newChildren [ key] = try child. _compactMapValues ( keyPath : childKeyPath , transform)
558
581
}
559
582
}
560
583
@@ -567,31 +590,54 @@ extension Graph {
567
590
///
568
591
/// - Parameters:
569
592
/// - transform: A closure that is invoked once per element in the graph.
570
- /// The leaf value of each node is passed to this closure. The result of
571
- /// the closure is a tuple containing the new value and specifying whether
572
- /// or not the new value should also be applied to each descendant node.
573
- /// If `true`, `transform` is not invoked for those descendant nodes. If
574
- /// the result is `nil`, the node and all of its child nodes are omitted
575
- /// from the new graph.
593
+ /// The key path and leaf value of each node are passed to this closure.
594
+ /// The result of the closure is a tuple containing the new value and
595
+ /// specifying whether or not the new value should also be applied to each
596
+ /// descendant node. If `true`, `transform` is not invoked for those
597
+ /// descendant nodes. If the result is `nil`, the node and all of its
598
+ /// child nodes are omitted from the new graph.
576
599
///
577
600
/// - Returns: A graph containing the transformed nodes of this graph at the
578
601
/// same key paths, with `nil` values omitted.
579
602
///
580
603
/// - Throws: Whatever is thrown by `transform`.
581
604
///
582
605
/// This function iterates depth-first.
583
- func compactMapValues< U> ( _ transform: ( V ) async throws -> ( U , recursivelyApply: Bool ) ? ) async rethrows -> Graph < K , U > ? {
584
- guard let ( newValue, recursivelyApply) = try await transform ( value) else {
606
+ func compactMapValues< U> ( _ transform: ( Element ) async throws -> ( U , recursivelyApply: Bool ) ? ) async rethrows -> Graph < K , U > ? {
607
+ try await _compactMapValues ( keyPath: [ ] ) {
608
+ try await transform ( ( $0, $1) )
609
+ }
610
+ }
611
+
612
+ /// The recursive implementation of `compactMapValues(_:)`.
613
+ ///
614
+ /// - Parameters:
615
+ /// - keyPath: The key path to use for the root node when passing it to
616
+ /// `transform`.
617
+ /// - transform: A closure that is invoked once per element in the graph.
618
+ /// The key path and leaf value of each node are passed to this closure.
619
+ /// The result of the closure is a tuple containing the new value and
620
+ /// specifying whether or not the new value should also be applied to each
621
+ /// descendant node. If `true`, `transform` is not invoked for those
622
+ /// descendant nodes. If the result is `nil`, the node and all of its
623
+ /// child nodes are omitted from the new graph.
624
+ ///
625
+ /// - Throws: Whatever is thrown by `transform`.
626
+ private func _compactMapValues< U> ( keyPath: [ K ] , _ transform: ( Element ) async throws -> ( U , recursivelyApply: Bool ) ? ) async rethrows -> Graph < K , U > ? {
627
+ guard let ( newValue, recursivelyApply) = try await transform ( ( keyPath, value) ) else {
585
628
return nil
586
629
}
587
630
588
631
var newChildren = [ K : Graph < K , U > ] ( )
589
632
newChildren. reserveCapacity ( children. count)
590
633
for (key, child) in children {
634
+ var childKeyPath = keyPath
635
+ childKeyPath. append ( key)
636
+
591
637
if recursivelyApply {
592
- newChildren [ key] = child. compactMapValues { _ in ( newValue, true ) }
638
+ newChildren [ key] = child. _compactMapValues ( keyPath : childKeyPath ) { _ in ( newValue, true ) }
593
639
} else {
594
- newChildren [ key] = try await child. compactMapValues ( transform)
640
+ newChildren [ key] = try await child. _compactMapValues ( keyPath : childKeyPath , transform)
595
641
}
596
642
}
597
643
@@ -603,16 +649,16 @@ extension Graph {
603
649
///
604
650
/// - Parameters:
605
651
/// - transform: A closure that is invoked once per element in the graph.
606
- /// The leaf value of each node is passed to this closure and its result
607
- /// is used as the corresponding value in the new graph.
652
+ /// The key path and leaf value of each node are passed to this closure
653
+ /// and its result is used as the corresponding value in the new graph.
608
654
///
609
655
/// - Returns: A graph containing the transformed nodes of this graph at the
610
656
/// same key paths.
611
657
///
612
658
/// - Throws: Whatever is thrown by `transform`.
613
659
///
614
660
/// This function iterates depth-first.
615
- func mapValues< U> ( _ transform: ( V ) throws -> U ) rethrows -> Graph < K , U > {
661
+ func mapValues< U> ( _ transform: ( Element ) throws -> U ) rethrows -> Graph < K , U > {
616
662
try compactMapValues ( transform) !
617
663
}
618
664
@@ -621,16 +667,16 @@ extension Graph {
621
667
///
622
668
/// - Parameters:
623
669
/// - transform: A closure that is invoked once per element in the graph.
624
- /// The leaf value of each node is passed to this closure and its result
625
- /// is used as the corresponding value in the new graph.
670
+ /// The key path and leaf value of each node are passed to this closure
671
+ /// and its result is used as the corresponding value in the new graph.
626
672
///
627
673
/// - Returns: A graph containing the transformed nodes of this graph at the
628
674
/// same key paths.
629
675
///
630
676
/// - Throws: Whatever is thrown by `transform`.
631
677
///
632
678
/// This function iterates depth-first.
633
- func mapValues< U> ( _ transform: ( V ) async throws -> U ) async rethrows -> Graph < K , U > {
679
+ func mapValues< U> ( _ transform: ( Element ) async throws -> U ) async rethrows -> Graph < K , U > {
634
680
try await compactMapValues ( transform) !
635
681
}
636
682
@@ -640,18 +686,19 @@ extension Graph {
640
686
///
641
687
/// - Parameters:
642
688
/// - transform: A closure that is invoked once per element in the graph.
643
- /// The leaf value of each node is passed to this closure. The result of
644
- /// the closure is a tuple containing the new value and specifying whether
645
- /// or not the new value should also be applied to each descendant node.
646
- /// If `true`, `transform` is not invoked for those descendant nodes.
689
+ /// The key path and leaf value of each node are passed to this closure.
690
+ /// The result of the closure is a tuple containing the new value and
691
+ /// specifying whether or not the new value should also be applied to each
692
+ /// descendant node. If `true`, `transform` is not invoked for those
693
+ /// descendant nodes.
647
694
///
648
695
/// - Returns: A graph containing the transformed nodes of this graph at the
649
696
/// same key paths.
650
697
///
651
698
/// - Throws: Whatever is thrown by `transform`.
652
699
///
653
700
/// This function iterates depth-first.
654
- func mapValues< U> ( _ transform: ( V ) throws -> ( U , recursivelyApply: Bool ) ) rethrows -> Graph < K , U > {
701
+ func mapValues< U> ( _ transform: ( Element ) throws -> ( U , recursivelyApply: Bool ) ) rethrows -> Graph < K , U > {
655
702
try compactMapValues ( transform) !
656
703
}
657
704
@@ -661,18 +708,19 @@ extension Graph {
661
708
///
662
709
/// - Parameters:
663
710
/// - transform: A closure that is invoked once per element in the graph.
664
- /// The leaf value of each node is passed to this closure. The result of
665
- /// the closure is a tuple containing the new value and specifying whether
666
- /// or not the new value should also be applied to each descendant node.
667
- /// If `true`, `transform` is not invoked for those descendant nodes.
711
+ /// The key path and leaf value of each node are passed to this closure.
712
+ /// The result of the closure is a tuple containing the new value and
713
+ /// specifying whether or not the new value should also be applied to each
714
+ /// descendant node. If `true`, `transform` is not invoked for those
715
+ /// descendant nodes.
668
716
///
669
717
/// - Returns: A graph containing the transformed nodes of this graph at the
670
718
/// same key paths.
671
719
///
672
720
/// - Throws: Whatever is thrown by `transform`.
673
721
///
674
722
/// This function iterates depth-first.
675
- func mapValues< U> ( _ transform: ( V ) async throws -> ( U , recursivelyApply: Bool ) ) async rethrows -> Graph < K , U > {
723
+ func mapValues< U> ( _ transform: ( Element ) async throws -> ( U , recursivelyApply: Bool ) ) async rethrows -> Graph < K , U > {
676
724
try await compactMapValues ( transform) !
677
725
}
678
726
0 commit comments