github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/mutable/set/api.go (about)

     1  // Package set provides unordered mutable.Set constructors and helpers
     2  package set
     3  
     4  import (
     5  	"golang.org/x/exp/constraints"
     6  
     7  	breakLoop "github.com/m4gshm/gollections/break/loop"
     8  	"github.com/m4gshm/gollections/collection"
     9  	"github.com/m4gshm/gollections/collection/mutable"
    10  	"github.com/m4gshm/gollections/collection/mutable/ordered"
    11  	"github.com/m4gshm/gollections/loop"
    12  )
    13  
    14  // Of instantiates Set with predefined elements.
    15  func Of[T comparable](elements ...T) *mutable.Set[T] {
    16  	return mutable.NewSet(elements...)
    17  }
    18  
    19  // From instantiates a set with elements retrieved by the 'next' function
    20  func From[T comparable](next func() (T, bool)) *mutable.Set[T] {
    21  	return mutable.SetFromLoop(next)
    22  }
    23  
    24  // Empty instantiates Set with zero capacity.
    25  func Empty[T comparable]() *mutable.Set[T] {
    26  	return NewCap[T](0)
    27  }
    28  
    29  // NewCap instantiates Set with a predefined capacity.
    30  func NewCap[T comparable](capacity int) *mutable.Set[T] {
    31  	return mutable.NewSetCap[T](capacity)
    32  }
    33  
    34  // Sort sorts a Set in-place by a converter that thransforms an element to an Ordered (int, string and so on).
    35  func Sort[T comparable, F constraints.Ordered](s *mutable.Set[T], by func(T) F) *ordered.Set[T] {
    36  	return collection.Sort[*ordered.Set[T]](s, by)
    37  }
    38  
    39  // Convert returns a loop that applies the 'converter' function to the collection elements
    40  func Convert[From, To comparable](set *mutable.Set[From], converter func(From) To) loop.Loop[To] {
    41  	return collection.Convert(set, converter)
    42  }
    43  
    44  // Conv returns a breakable loop that applies the 'converter' function to the collection elements
    45  func Conv[From, To comparable](set *mutable.Set[From], converter func(From) (To, error)) breakLoop.Loop[To] {
    46  	return collection.Conv(set, converter)
    47  }
    48  
    49  // Flat returns a loop that converts the collection elements into slices and then flattens them to one level
    50  func Flat[From, To comparable](set *mutable.Set[From], flattener func(From) []To) loop.Loop[To] {
    51  	return collection.Flat(set, flattener)
    52  }
    53  
    54  // Flatt returns a breakable loop that converts the collection elements into slices and then flattens them to one level
    55  func Flatt[From, To comparable](set *mutable.Set[From], flattener func(From) ([]To, error)) breakLoop.Loop[To] {
    56  	return collection.Flatt(set, flattener)
    57  }