chan.go 529 B

123456789101112131415161718192021222324252627
  1. package iters
  2. // The type describes pair of key and value.
  3. type Pair[K any, V any] struct {
  4. V V
  5. K K
  6. }
  7. // The type describes channel of pairs.
  8. type PairChan[K any, V any] chan Pair[K, V]
  9. // Slice of pairs.
  10. type Pairs[K any, V any] []Pair[K, V]
  11. // ForEach for channels, like in JS.
  12. // Be careful since the function does not close the
  13. // channel so if fn breaks the loop then there can
  14. // be values left.
  15. func (pc PairChan[K, V]) ForEach(fn func(k K, v V) bool) {
  16. for p := range pc {
  17. if !fn(p.K, p.V) {
  18. break
  19. }
  20. }
  21. }