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

     1  // Package set provides unordered immutable.Set constructors and helpers
     2  package set
     3  
     4  import (
     5  	"golang.org/x/exp/constraints"
     6  
     7  	breakStream "github.com/m4gshm/gollections/break/stream"
     8  	"github.com/m4gshm/gollections/collection"
     9  	"github.com/m4gshm/gollections/collection/immutable"
    10  	"github.com/m4gshm/gollections/collection/immutable/ordered"
    11  	"github.com/m4gshm/gollections/stream"
    12  )
    13  
    14  // Of instantiates Set with predefined elements.
    15  func Of[T comparable](elements ...T) immutable.Set[T] {
    16  	return immutable.NewSet(elements...)
    17  }
    18  
    19  // New instantiates Set and copies elements to it.
    20  func New[T comparable](elements []T) immutable.Set[T] {
    21  	return immutable.NewSet(elements...)
    22  }
    23  
    24  // From instantiates a map with key/values retrieved by the 'next' function.
    25  // The next returns a key/value pairs with true or zero values with false if there are no more elements.
    26  func From[T comparable](next func() (T, bool)) immutable.Set[T] {
    27  	return immutable.SetFromLoop(next)
    28  }
    29  
    30  // Sort instantiates Set and puts sorted elements to it.
    31  func Sort[T comparable, f constraints.Ordered](s immutable.Set[T], by func(T) f) ordered.Set[T] {
    32  	return collection.Sort[ordered.Set[T]](s, by)
    33  }
    34  
    35  // Convert returns a stream that applies the 'converter' function to the collection elements
    36  func Convert[From, To comparable](set immutable.Set[From], converter func(From) To) stream.Iter[To] {
    37  	return collection.Convert(set, converter)
    38  }
    39  
    40  // Conv returns a breakable stream that applies the 'converter' function to the collection elements
    41  func Conv[From, To comparable](set immutable.Set[From], converter func(From) (To, error)) breakStream.Iter[To] {
    42  	return collection.Conv(set, converter)
    43  }
    44  
    45  // Flat returns a stream that converts the collection elements into slices and then flattens them to one level
    46  func Flat[From, To comparable](set immutable.Set[From], flattener func(From) []To) stream.Iter[To] {
    47  	return collection.Flat(set, flattener)
    48  }
    49  
    50  // Flatt returns a breakable stream that converts the collection elements into slices and then flattens them to one level
    51  func Flatt[From, To comparable](set immutable.Set[From], flattener func(From) ([]To, error)) breakStream.Iter[To] {
    52  	return collection.Flatt(set, flattener)
    53  }