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

     1  // Package set provides mutable ordered.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/ordered"
    10  	"github.com/m4gshm/gollections/loop"
    11  )
    12  
    13  // Of instantiates Set with predefined elements.
    14  func Of[T comparable](elements ...T) *ordered.Set[T] {
    15  	return ordered.NewSet(elements...)
    16  }
    17  
    18  // From instantiates a set with elements retrieved by the 'next' function
    19  func From[T comparable](next func() (T, bool)) *ordered.Set[T] {
    20  	return ordered.SetFromLoop(next)
    21  }
    22  
    23  // Empty instantiates Set with zero capacity.
    24  func Empty[T comparable]() *ordered.Set[T] {
    25  	return NewCap[T](0)
    26  }
    27  
    28  // NewCap instantiates Set with a predefined capacity.
    29  func NewCap[T comparable](capacity int) *ordered.Set[T] {
    30  	return ordered.NewSetCap[T](capacity)
    31  }
    32  
    33  // Sort copy the specified set with sorted elements
    34  func Sort[T comparable, O constraints.Ordered](s *ordered.Set[T], by func(T) O) *ordered.Set[T] {
    35  	return collection.Sort[*ordered.Set[T]](s, by)
    36  }
    37  
    38  // Convert returns a loop that applies the 'converter' function to the collection elements
    39  func Convert[From, To comparable](set *ordered.Set[From], converter func(From) To) loop.Loop[To] {
    40  	return collection.Convert(set, converter)
    41  }
    42  
    43  // Conv returns a breakable loop that applies the 'converter' function to the collection elements
    44  func Conv[From, To comparable](set *ordered.Set[From], converter func(From) (To, error)) breakLoop.Loop[To] {
    45  	return collection.Conv(set, converter)
    46  }
    47  
    48  // Flat returns a loop that converts the collection elements into slices and then flattens them to one level
    49  func Flat[From, To comparable](set *ordered.Set[From], flattener func(From) []To) loop.Loop[To] {
    50  	return collection.Flat(set, flattener)
    51  }
    52  
    53  // Flatt returns a breakable loop that converts the collection elements into slices and then flattens them to one level
    54  func Flatt[From, To comparable](set *ordered.Set[From], flattener func(From) ([]To, error)) breakLoop.Loop[To] {
    55  	return collection.Flatt(set, flattener)
    56  }