Source file src/cmd/vendor/golang.org/x/tools/internal/moreiters/iters.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package moreiters
     6  
     7  import "iter"
     8  
     9  // First returns the first value of seq and true.
    10  // If seq is empty, it returns the zero value of T and false.
    11  func First[T any](seq iter.Seq[T]) (z T, ok bool) {
    12  	for t := range seq {
    13  		return t, true
    14  	}
    15  	return z, false
    16  }
    17  
    18  // Contains reports whether x is an element of the sequence seq.
    19  func Contains[T comparable](seq iter.Seq[T], x T) bool {
    20  	for cand := range seq {
    21  		if cand == x {
    22  			return true
    23  		}
    24  	}
    25  	return false
    26  }
    27  
    28  // Every reports whether every pred(t) for t in seq returns true,
    29  // stopping at the first false element.
    30  func Every[T any](seq iter.Seq[T], pred func(T) bool) bool {
    31  	for t := range seq {
    32  		if !pred(t) {
    33  			return false
    34  		}
    35  	}
    36  	return true
    37  }
    38  
    39  // Any reports whether any pred(t) for t in seq returns true.
    40  func Any[T any](seq iter.Seq[T], pred func(T) bool) bool {
    41  	for t := range seq {
    42  		if pred(t) {
    43  			return true
    44  		}
    45  	}
    46  	return false
    47  }
    48  

View as plain text