Skip to content

proposal: iter: Add functions for traversing several sequences. #70277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stanislav-pyatykh opened this issue Nov 11, 2024 · 1 comment
Closed
Labels
Milestone

Comments

@stanislav-pyatykh
Copy link

Proposal Details

The proposal is to add functions for traversing several sequences without creating intermediate containers:

// Concat returns an iterator that yields elements of the given sequences in order.
func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V] {
  return func(yield func(V) bool) {
    for _, seq := range seqs {
      for v := range seq {
        if !yield(v) {
          return
        }
      }
    }
  }
}

// Concat2 returns an iterator that yields elements of the given sequences of pairs in order.
func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V] {
  return func(yield func(K, V) bool) {
    for _, seq := range seqs {
      for k, v := range seq {
        if !yield(k, v) {
          return
        }
      }
    }
  }
}

Example usage:

func print(a, b []int) {
  for v := range iter.Concat(slices.Values(a), slices.Values(b)) {
    fmt.Println(v)
  }
}
@gopherbot gopherbot added this to the Proposal milestone Nov 11, 2024
@gabyhelp
Copy link

Related Issues

Related Discussions

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants