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

     1  // Package set provides 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/immutable/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  // New instantiates Set and copies elements to it.
    19  func New[T comparable](elements []T) ordered.Set[T] {
    20  	return ordered.NewSet(elements...)
    21  }
    22  
    23  // From instantiates a set with elements retrieved by the 'next' function
    24  func From[T comparable](next func() (T, bool)) ordered.Set[T] {
    25  	return ordered.SetFromLoop(next)
    26  }
    27  
    28  // Sort copy the specified set with sorted elements
    29  func Sort[T comparable, f constraints.Ordered](s ordered.Set[T], by func(T) f) ordered.Set[T] {
    30  	return collection.Sort[ordered.Set[T]](s, by)
    31  }
    32  
    33  // Convert returns a loop that applies the 'converter' function to the collection elements
    34  func Convert[From, To comparable](set ordered.Set[From], converter func(From) To) loop.Loop[To] {
    35  	return collection.Convert(set, converter)
    36  }
    37  
    38  // Conv returns a breakable loop that applies the 'converter' function to the collection elements
    39  func Conv[From, To comparable](set ordered.Set[From], converter func(From) (To, error)) breakLoop.Loop[To] {
    40  	return collection.Conv(set, converter)
    41  }
    42  
    43  // Flat returns a loop that converts the collection elements into slices and then flattens them to one level
    44  func Flat[From, To comparable](set ordered.Set[From], flattener func(From) []To) loop.Loop[To] {
    45  	return collection.Flat(set, flattener)
    46  }
    47  
    48  // Flatt returns a breakable loop that converts the collection elements into slices and then flattens them to one level
    49  func Flatt[From, To comparable](set ordered.Set[From], flattener func(From) ([]To, error)) breakLoop.Loop[To] {
    50  	return collection.Flatt(set, flattener)
    51  }