github.com/m4gshm/gollections@v0.0.10/collection/immutable/keys.go (about)

     1  package immutable
     2  
     3  import (
     4  	"fmt"
     5  
     6  	breakLoop "github.com/m4gshm/gollections/break/loop"
     7  	breakStream "github.com/m4gshm/gollections/break/stream"
     8  	"github.com/m4gshm/gollections/c"
     9  	"github.com/m4gshm/gollections/collection"
    10  	"github.com/m4gshm/gollections/loop"
    11  	"github.com/m4gshm/gollections/map_"
    12  	"github.com/m4gshm/gollections/slice"
    13  	"github.com/m4gshm/gollections/stream"
    14  )
    15  
    16  // WrapKeys instantiates MapKeys using the elements as internal storage
    17  func WrapKeys[K comparable, V any](elements map[K]V) MapKeys[K, V] {
    18  	return MapKeys[K, V]{elements}
    19  }
    20  
    21  // MapKeys is the container reveal keys of a map and hides values
    22  type MapKeys[K comparable, V any] struct {
    23  	elements map[K]V
    24  }
    25  
    26  var (
    27  	_ c.Collection[int]                         = (*MapKeys[int, any])(nil)
    28  	_ c.Collection[int]                         = MapKeys[int, any]{}
    29  	_ loop.Looper[int, *map_.KeyIter[int, any]] = (*MapKeys[int, any])(nil)
    30  	_ loop.Looper[int, *map_.KeyIter[int, any]] = MapKeys[int, any]{}
    31  	_ fmt.Stringer                              = (*MapKeys[int, any])(nil)
    32  	_ fmt.Stringer                              = MapKeys[int, any]{}
    33  )
    34  
    35  // Iter creates an iterator and returns as interface
    36  func (m MapKeys[K, V]) Iter() c.Iterator[K] {
    37  	h := m.Head()
    38  	return &h
    39  }
    40  
    41  // Loop creates an iterator and returns as implementation type reference
    42  func (m MapKeys[K, V]) Loop() *map_.KeyIter[K, V] {
    43  	h := m.Head()
    44  	return &h
    45  }
    46  
    47  // Head creates an iterator and returns as implementation type value
    48  func (m MapKeys[K, V]) Head() map_.KeyIter[K, V] {
    49  	return map_.NewKeyIter(m.elements)
    50  }
    51  
    52  // First returns the first element of the collection, an iterator to iterate over the remaining elements, and true\false marker of availability next elements.
    53  // If no more elements then ok==false.
    54  func (m MapKeys[K, V]) First() (map_.KeyIter[K, V], K, bool) {
    55  	var (
    56  		iterator  = m.Head()
    57  		first, ok = iterator.Next()
    58  	)
    59  	return iterator, first, ok
    60  }
    61  
    62  // Len returns amount of elements
    63  func (m MapKeys[K, V]) Len() int {
    64  	return len(m.elements)
    65  }
    66  
    67  // IsEmpty returns true if the collection is empty
    68  func (m MapKeys[K, V]) IsEmpty() bool {
    69  	return m.Len() == 0
    70  }
    71  
    72  // Slice collects the elements to a slice
    73  func (m MapKeys[K, V]) Slice() []K {
    74  	return map_.Keys(m.elements)
    75  }
    76  
    77  // Append collects the values to the specified 'out' slice
    78  func (m MapKeys[K, V]) Append(out []K) []K {
    79  	return map_.AppendKeys(m.elements, out)
    80  }
    81  
    82  // For applies the 'walker' function for every key. Return the c.ErrBreak to stop.
    83  func (m MapKeys[K, V]) For(walker func(K) error) error {
    84  	return map_.ForKeys(m.elements, walker)
    85  }
    86  
    87  // ForEach applies the 'walker' function for every key
    88  func (m MapKeys[K, V]) ForEach(walker func(K)) {
    89  	map_.ForEachKey(m.elements, walker)
    90  }
    91  
    92  // Filter returns a stream consisting of elements that satisfy the condition of the 'predicate' function
    93  func (m MapKeys[K, V]) Filter(filter func(K) bool) stream.Iter[K] {
    94  	h := m.Head()
    95  	return stream.New(loop.Filter(h.Next, filter).Next)
    96  }
    97  
    98  // Filt returns a breakable stream consisting of elements that satisfy the condition of the 'predicate' function
    99  func (m MapKeys[K, V]) Filt(predicate func(K) (bool, error)) breakStream.Iter[K] {
   100  	h := m.Head()
   101  	return breakStream.New(breakLoop.Filt(breakLoop.From(h.Next), predicate).Next)
   102  }
   103  
   104  // Convert returns a stream that applies the 'converter' function to the collection elements
   105  func (m MapKeys[K, V]) Convert(converter func(K) K) stream.Iter[K] {
   106  	return collection.Convert(m, converter)
   107  }
   108  
   109  // Conv returns a breakable stream that applies the 'converter' function to the collection elements
   110  func (m MapKeys[K, V]) Conv(converter func(K) (K, error)) breakStream.Iter[K] {
   111  	return collection.Conv(m, converter)
   112  }
   113  
   114  // Reduce reduces the elements into an one using the 'merge' function
   115  func (m MapKeys[K, V]) Reduce(merge func(K, K) K) K {
   116  	k, _ := map_.Reduce(m.elements, func(k1, k2 K, _, _ V) (rk K, rv V) {
   117  		return merge(k1, k2), rv
   118  	})
   119  	return k
   120  }
   121  
   122  // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful
   123  func (m MapKeys[K, V]) HasAny(predicate func(K) bool) bool {
   124  	return map_.HasAny(m.elements, func(k K, v V) bool {
   125  		return predicate(k)
   126  	})
   127  }
   128  
   129  func (m MapKeys[K, V]) String() string {
   130  	return slice.ToString(m.Slice())
   131  }