Skip to content

Commit

Permalink
iter: Better reading order
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Jul 25, 2024
1 parent 16a697a commit 4219476
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions html/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ package html

import "iter"

// Parents returns an sequence yielding the node and its parents.
//
// Mutating a Node or its parents while iterating may have unexpected results.
func (n *Node) Parents() iter.Seq[*Node] {
return func(yield func(*Node) bool) {
for p := n; p != nil; p = p.Parent {
if !yield(p) {
return
}
}
}
}

// ChildNodes returns a sequence yielding the immediate children of n.
//
// Mutating a Node while iterating through its ChildNodes may have unexpected results.
// Mutating a Node or its ChildNodes while iterating may have unexpected results.
func (n *Node) ChildNodes() iter.Seq[*Node] {
return func(yield func(*Node) bool) {
if n == nil {
Expand All @@ -25,22 +38,9 @@ func (n *Node) ChildNodes() iter.Seq[*Node] {

}

func (n *Node) all(yield func(*Node) bool) bool {
if !yield(n) {
return false
}

for c := range n.ChildNodes() {
if !c.all(yield) {
return false
}
}
return true
}

// All returns a sequence yielding all descendents of n in depth-first pre-order.
//
// Mutating a Node while iterating through it or its descendents may have unexpected results.
// Mutating a Node or its descendents while iterating may have unexpected results.
func (n *Node) All() iter.Seq[*Node] {
return func(yield func(*Node) bool) {
if n == nil {
Expand All @@ -50,15 +50,15 @@ func (n *Node) All() iter.Seq[*Node] {
}
}

// Parents returns an sequence yielding the node and its parents.
//
// Mutating a Node while iterating through it or its parents may have unexpected results.
func (n *Node) Parents() iter.Seq[*Node] {
return func(yield func(*Node) bool) {
for p := n; p != nil; p = p.Parent {
if !yield(p) {
return
}
func (n *Node) all(yield func(*Node) bool) bool {
if !yield(n) {
return false
}

for c := range n.ChildNodes() {
if !c.all(yield) {
return false
}
}
return true
}

0 comments on commit 4219476

Please sign in to comment.