github.com/m4gshm/gollections@v0.0.10/collection/mutable/vector/api.go (about)

     1  // Package vector provides mutable.Vector constructors and helpers
     2  package vector
     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/mutable"
    10  	"github.com/m4gshm/gollections/loop"
    11  	"github.com/m4gshm/gollections/stream"
    12  )
    13  
    14  // Of instantiates a vector with the specified elements
    15  func Of[T any](elements ...T) *mutable.Vector[T] {
    16  	return mutable.NewVector(elements...)
    17  }
    18  
    19  // Empty instantiates Vector with zero capacity.
    20  func Empty[T any]() *mutable.Vector[T] {
    21  	return NewCap[T](0)
    22  }
    23  
    24  // NewCap creates a vector with a predefined capacity.
    25  func NewCap[T any](capacity int) *mutable.Vector[T] {
    26  	return mutable.NewVectorCap[T](capacity)
    27  }
    28  
    29  // From instantiates a vector with elements retrieved by the 'next' function.
    30  // The next returns an element with true or zero value with false if there are no more elements.
    31  func From[T any](next func() (T, bool)) *mutable.Vector[T] {
    32  	return mutable.WrapVector(loop.Slice(next))
    33  }
    34  
    35  // Sort sorts the specified vector in-place by a converter that thransforms an element to an Ordered (int, string and so on).
    36  func Sort[T any, F constraints.Ordered](v *mutable.Vector[T], by func(T) F) *mutable.Vector[T] {
    37  	return collection.Sort[*mutable.Vector[T]](v, by)
    38  }
    39  
    40  // Convert returns a stream that applies the 'converter' function to the collection elements
    41  func Convert[From, To any](vector *mutable.Vector[From], converter func(From) To) stream.Iter[To] {
    42  	return collection.Convert(vector, converter)
    43  }
    44  
    45  // Conv returns a breakable stream that applies the 'converter' function to the collection elements
    46  func Conv[From, To comparable](vector *mutable.Vector[From], converter func(From) (To, error)) breakStream.Iter[To] {
    47  	return collection.Conv(vector, converter)
    48  }
    49  
    50  // Flat returns a stream that converts the collection elements into slices and then flattens them to one level
    51  func Flat[From, To any](vector *mutable.Vector[From], flattener func(From) []To) stream.Iter[To] {
    52  	return collection.Flat(vector, flattener)
    53  }
    54  
    55  // Flatt returns a breakable stream that converts the collection elements into slices and then flattens them to one level
    56  func Flatt[From, To comparable](vector *mutable.Vector[From], flattener func(From) ([]To, error)) breakStream.Iter[To] {
    57  	return collection.Flatt(vector, flattener)
    58  }