Skip to content

Commit 4219476

Browse files
committed
iter: Better reading order
1 parent 16a697a commit 4219476

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

html/iter.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,22 @@ package html
88

99
import "iter"
1010

11+
// Parents returns an sequence yielding the node and its parents.
12+
//
13+
// Mutating a Node or its parents while iterating may have unexpected results.
14+
func (n *Node) Parents() iter.Seq[*Node] {
15+
return func(yield func(*Node) bool) {
16+
for p := n; p != nil; p = p.Parent {
17+
if !yield(p) {
18+
return
19+
}
20+
}
21+
}
22+
}
23+
1124
// ChildNodes returns a sequence yielding the immediate children of n.
1225
//
13-
// Mutating a Node while iterating through its ChildNodes may have unexpected results.
26+
// Mutating a Node or its ChildNodes while iterating may have unexpected results.
1427
func (n *Node) ChildNodes() iter.Seq[*Node] {
1528
return func(yield func(*Node) bool) {
1629
if n == nil {
@@ -25,22 +38,9 @@ func (n *Node) ChildNodes() iter.Seq[*Node] {
2538

2639
}
2740

28-
func (n *Node) all(yield func(*Node) bool) bool {
29-
if !yield(n) {
30-
return false
31-
}
32-
33-
for c := range n.ChildNodes() {
34-
if !c.all(yield) {
35-
return false
36-
}
37-
}
38-
return true
39-
}
40-
4141
// All returns a sequence yielding all descendents of n in depth-first pre-order.
4242
//
43-
// Mutating a Node while iterating through it or its descendents may have unexpected results.
43+
// Mutating a Node or its descendents while iterating may have unexpected results.
4444
func (n *Node) All() iter.Seq[*Node] {
4545
return func(yield func(*Node) bool) {
4646
if n == nil {
@@ -50,15 +50,15 @@ func (n *Node) All() iter.Seq[*Node] {
5050
}
5151
}
5252

53-
// Parents returns an sequence yielding the node and its parents.
54-
//
55-
// Mutating a Node while iterating through it or its parents may have unexpected results.
56-
func (n *Node) Parents() iter.Seq[*Node] {
57-
return func(yield func(*Node) bool) {
58-
for p := n; p != nil; p = p.Parent {
59-
if !yield(p) {
60-
return
61-
}
53+
func (n *Node) all(yield func(*Node) bool) bool {
54+
if !yield(n) {
55+
return false
56+
}
57+
58+
for c := range n.ChildNodes() {
59+
if !c.all(yield) {
60+
return false
6261
}
6362
}
63+
return true
6464
}

0 commit comments

Comments
 (0)