github.com/m4gshm/gollections@v0.0.10/kv/iter/api.go (about)

     1  // Package iter provides generic constructors and helpers for key/value iterators
     2  package iter
     3  
     4  import (
     5  	"github.com/m4gshm/gollections/c"
     6  	"github.com/m4gshm/gollections/iter"
     7  	"github.com/m4gshm/gollections/kv"
     8  	kvLoop "github.com/m4gshm/gollections/kv/loop"
     9  	"github.com/m4gshm/gollections/kv/loop/group"
    10  	"github.com/m4gshm/gollections/kv/stream"
    11  	"github.com/m4gshm/gollections/loop"
    12  	"github.com/m4gshm/gollections/map_/filter"
    13  	"github.com/m4gshm/gollections/slice"
    14  )
    15  
    16  // OfPairs instantiates KVIterator of predefined key\value pairs
    17  func OfPairs[K, V any](pairs ...c.KV[K, V]) loop.KeyValuer[c.KV[K, V], K, V] {
    18  	return WrapPairs(pairs)
    19  }
    20  
    21  // WrapPairs instantiates KVIterator using slice as the key\value pairs source
    22  func WrapPairs[K, V any, P ~[]c.KV[K, V]](pairs P) loop.KeyValuer[c.KV[K, V], K, V] {
    23  	return FromPairs[K, V](slice.NewIter(pairs))
    24  }
    25  
    26  // FromPairs converts an iterator of key\value pair elements to a KVIterator
    27  func FromPairs[K, V any, I c.Iterator[c.KV[K, V]]](elements I) loop.KeyValuer[c.KV[K, V], K, V] {
    28  	return FromIter(elements, (c.KV[K, V]).Key, (c.KV[K, V]).Value)
    29  }
    30  
    31  // FromIter converts a c.Iterator to a kv.KVIterator using key and value extractors
    32  func FromIter[T, K, V any, I c.Iterator[T]](elements I, keyExtractor func(T) K, valExtractor func(T) V) loop.KeyValuer[T, K, V] {
    33  	return iter.KeyValue(elements, keyExtractor, valExtractor)
    34  }
    35  
    36  // Group collects sets of values grouped by keys obtained by passing a key/value iterator
    37  func Group[K comparable, V any, I kv.Iterator[K, V]](elements I) map[K][]V {
    38  	return group.Of(elements.Next)
    39  }
    40  
    41  // Map instantiates key/value iterator that converts elements with a converter and returns them
    42  func Map[K comparable, V any, KOUT comparable, VOUT any, I kv.Iterator[K, V]](elements I, by func(K, V) (KOUT, VOUT)) stream.Iter[KOUT, VOUT, map[KOUT]VOUT] {
    43  	return stream.New(kvLoop.Convert(elements.Next, by).Next, kvLoop.ToMap[KOUT, VOUT])
    44  }
    45  
    46  // Filter instantiates key/value iterator that iterates only over filtered elements
    47  func Filter[K comparable, V any, I kv.Iterator[K, V]](elements I, filter func(K, V) bool) stream.Iter[K, V, map[K]V] {
    48  	return stream.New(kvLoop.Filter(elements.Next, filter).Next, kvLoop.ToMap[K, V])
    49  }
    50  
    51  // FilterKey instantiates key/value iterator that iterates only over elements that filtered by the key
    52  func FilterKey[K comparable, V any, I kv.Iterator[K, V]](elements I, fit func(K) bool) stream.Iter[K, V, map[K]V] {
    53  	return Filter(elements, filter.Key[V](fit))
    54  }
    55  
    56  // FilterValue instantiates key/value iterator that iterates only over elements that filtered by the value
    57  func FilterValue[K comparable, V any, I kv.Iterator[K, V]](elements I, fit func(V) bool) stream.Iter[K, V, map[K]V] {
    58  	return Filter(elements, filter.Value[K](fit))
    59  }
    60  
    61  // Reduce reduces keys/value pairs to an one pair
    62  func Reduce[K comparable, V any, I kv.Iterator[K, V]](elements I, merge func(K, K, V, V) (K, V)) (K, V) {
    63  	return kvLoop.Reduce(elements.Next, merge)
    64  }