-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
57 lines (46 loc) · 2.27 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package vector provides mutable.Vector constructors and helpers
package vector
import (
"golang.org/x/exp/constraints"
breakLoop "github.com/m4gshm/gollections/break/loop"
"github.com/m4gshm/gollections/collection"
"github.com/m4gshm/gollections/collection/mutable"
"github.com/m4gshm/gollections/loop"
)
// Of instantiates a vector with the specified elements
func Of[T any](elements ...T) *mutable.Vector[T] {
return mutable.NewVector(elements...)
}
// Empty instantiates Vector with zero capacity.
func Empty[T any]() *mutable.Vector[T] {
return NewCap[T](0)
}
// NewCap creates a vector with a predefined capacity.
func NewCap[T any](capacity int) *mutable.Vector[T] {
return mutable.NewVectorCap[T](capacity)
}
// From instantiates a vector with elements retrieved by the 'next' function.
// The next returns an element with true or zero value with false if there are no more elements.
func From[T any](next func() (T, bool)) *mutable.Vector[T] {
return mutable.WrapVector(loop.Slice(next))
}
// Sort sorts the specified vector in-place by a converter that thransforms an element to an Ordered (int, string and so on).
func Sort[T any, F constraints.Ordered](v *mutable.Vector[T], by func(T) F) *mutable.Vector[T] {
return collection.Sort[*mutable.Vector[T]](v, by)
}
// Convert returns a loop that applies the 'converter' function to the collection elements
func Convert[From, To any](vector *mutable.Vector[From], converter func(From) To) loop.Loop[To] {
return collection.Convert(vector, converter)
}
// Conv returns a breakable loop that applies the 'converter' function to the collection elements
func Conv[From, To comparable](vector *mutable.Vector[From], converter func(From) (To, error)) breakLoop.Loop[To] {
return collection.Conv(vector, converter)
}
// Flat returns a loop that converts the collection elements into slices and then flattens them to one level
func Flat[From, To any](vector *mutable.Vector[From], flattener func(From) []To) loop.Loop[To] {
return collection.Flat(vector, flattener)
}
// Flatt returns a breakable loop that converts the collection elements into slices and then flattens them to one level
func Flatt[From, To comparable](vector *mutable.Vector[From], flattener func(From) ([]To, error)) breakLoop.Loop[To] {
return collection.Flatt(vector, flattener)
}