this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

internal/core/adt: turn Vertex.VisitAllConjuncts into an iterator

Much like we already did with Vertex.VisitLeafConjuncts.
Note that this method has no callers yet.

While here, improve the docs for these iterator APIs a bit.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I367d2ebae4a971d530af5f6878b01575c732b87f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1224305
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+23 -10
+23 -10
internal/core/adt/composite.go
··· 231 231 // the final value of this Vertex. 232 232 // 233 233 // TODO: all access to Conjuncts should go through functions like 234 - // LeafConjuncts and VisitAllConjuncts. We should probably make this 235 - // an unexported field. 234 + // [Vertex.LeafConjuncts] and [Vertex.AllConjuncts]. 235 + // We should probably make this an unexported field. 236 236 Conjuncts ConjunctGroup 237 237 238 238 // Structs is a slice of struct literals that contributed to this value. ··· 561 561 } 562 562 } 563 563 564 - // LeafConjuncts iterates over all conjuncts that are leafs of the ConjunctGroup tree. 564 + // LeafConjuncts iterates over all conjuncts that are leaves of the [ConjunctGroup] tree. 565 565 func (v *Vertex) LeafConjuncts() iter.Seq[Conjunct] { 566 566 return func(yield func(Conjunct) bool) { 567 567 _ = iterConjuncts(v.Conjuncts, yield) ··· 569 569 } 570 570 571 571 func iterConjuncts(a []Conjunct, yield func(Conjunct) bool) bool { 572 + // TODO: note that this is iterAllConjuncts but without yielding ConjunctGroups. 573 + // Can we reuse the code in a simple enough way? 572 574 for _, c := range a { 573 575 switch x := c.x.(type) { 574 576 case *ConjunctGroup: ··· 584 586 return true 585 587 } 586 588 589 + // ConjunctsSeq iterates over all conjuncts that are leafs in the list of trees given. 587 590 func ConjunctsSeq(a []Conjunct) iter.Seq[Conjunct] { 588 591 return func(yield func(Conjunct) bool) { 589 592 _ = iterConjuncts(a, yield) 590 593 } 591 594 } 592 595 593 - // VisitAllConjuncts visits all conjuncts of v, including ConjunctGroups. 596 + // AllConjuncts iterates through all conjuncts of v, including [ConjunctGroup]s. 594 597 // Note that ConjunctGroups do not have an Environment associated with them. 595 - func (v *Vertex) VisitAllConjuncts(f func(c Conjunct, isLeaf bool)) { 596 - visitAllConjuncts(v.Conjuncts, f) 598 + // The boolean reports whether the conjunct is a leaf. 599 + func (v *Vertex) AllConjuncts() iter.Seq2[Conjunct, bool] { 600 + return func(yield func(Conjunct, bool) bool) { 601 + _ = iterAllConjuncts(v.Conjuncts, yield) 602 + } 597 603 } 598 604 599 - func visitAllConjuncts(a []Conjunct, f func(c Conjunct, isLeaf bool)) { 605 + func iterAllConjuncts(a []Conjunct, yield func(c Conjunct, isLeaf bool) bool) bool { 600 606 for _, c := range a { 601 607 switch x := c.x.(type) { 602 608 case *ConjunctGroup: 603 - f(c, false) 604 - visitAllConjuncts(*x, f) 609 + if !yield(c, false) { 610 + return false 611 + } 612 + if !iterAllConjuncts(*x, yield) { 613 + return false 614 + } 605 615 default: 606 - f(c, true) 616 + if !yield(c, true) { 617 + return false 618 + } 607 619 } 608 620 } 621 + return true 609 622 } 610 623 611 624 // HasConjuncts reports whether v has any conjuncts.