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

     1  // Package vector provides ordered immutable.Vector constructors and helpers
     2  package vector
     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"
    10  	"github.com/m4gshm/gollections/loop"
    11  )
    12  
    13  // Of instantiates a vector with the specified elements
    14  func Of[T any](elements ...T) immutable.Vector[T] {
    15  	return immutable.NewVector(elements...)
    16  }
    17  
    18  // New instantiates a vector with the specified elements
    19  func New[T any](elements []T) immutable.Vector[T] {
    20  	return immutable.NewVector(elements...)
    21  }
    22  
    23  // Wrap instantiates Vector using a slise as internal storage.
    24  func Wrap[T any](elements []T) immutable.Vector[T] {
    25  	return immutable.WrapVector(elements)
    26  }
    27  
    28  // From instantiates a vector with elements retrieved by the 'next' function.
    29  // The next returns an element with true or zero value with false if there are no more elements.
    30  func From[T any](next func() (T, bool)) immutable.Vector[T] {
    31  	return immutable.VectorFromLoop(next)
    32  }
    33  
    34  // Sort copy the specified vector with sorted elements
    35  func Sort[T any, F constraints.Ordered](v immutable.Vector[T], by func(T) F) immutable.Vector[T] {
    36  	return collection.Sort[immutable.Vector[T]](v, by)
    37  }
    38  
    39  // Convert returns a loop that applies the 'converter' function to the collection elements
    40  func Convert[From, To any](vector immutable.Vector[From], converter func(From) To) loop.Loop[To] {
    41  	return collection.Convert(vector, converter)
    42  }
    43  
    44  // Conv returns a breakable loop that applies the 'converter' function to the collection elements
    45  func Conv[From, To comparable](vector immutable.Vector[From], converter func(From) (To, error)) breakLoop.Loop[To] {
    46  	return collection.Conv(vector, 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 any, To any](vector immutable.Vector[From], flattener func(From) []To) loop.Loop[To] {
    51  	return collection.Flat(vector, 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](vector immutable.Vector[From], flattener func(From) ([]To, error)) breakLoop.Loop[To] {
    56  	return collection.Flatt(vector, flattener)
    57  }