github.com/aristanetworks/gomap@v0.0.0-20240103001659-f6b0e31fb1a7/iter.go (about)

     1  // Modifications copyright (c) Arista Networks, Inc. 2024
     2  // Underlying
     3  // Copyright 2014 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  //go:build goexperiment.rangefunc
     8  
     9  package gomap
    10  
    11  import "iter"
    12  
    13  // All returns an iterator over key-value pairs from m.
    14  func (m *Map[K, E]) All() iter.Seq2[K, E] {
    15  	return func(yield func(K, E) bool) {
    16  		for it := m.Iter(); it.Next(); {
    17  			if !yield(it.Key(), it.Elem()) {
    18  				return
    19  			}
    20  		}
    21  	}
    22  }
    23  
    24  // Keys returns an iterator over keys in m.
    25  func (m *Map[K, E]) Keys() iter.Seq[K] {
    26  	return func(yield func(K) bool) {
    27  		for it := m.Iter(); it.Next(); {
    28  			if !yield(it.Key()) {
    29  				return
    30  			}
    31  		}
    32  	}
    33  }
    34  
    35  // Values returns an iterator over values in m.
    36  func (m *Map[K, E]) Values() iter.Seq[E] {
    37  	return func(yield func(E) bool) {
    38  		for it := m.Iter(); it.Next(); {
    39  			if !yield(it.Elem()) {
    40  				return
    41  			}
    42  		}
    43  	}
    44  }