Documentation ¶
Index ¶
- type Collection
- func (c *Collection[T]) All(f func(i int, item T) bool) bool
- func (c *Collection[T]) At(index int) (T, bool)
- func (c *Collection[T]) AtFirst() (T, bool)
- func (c *Collection[T]) AtLast() (T, bool)
- func (c *Collection[T]) Batch(f func(int, int, T), batchSize int) *Collection[T]
- func (c *Collection[T]) Concat(items []T) *Collection[T]
- func (c *Collection[T]) Contains(item T) (found bool)
- func (c *Collection[T]) ContainsBy(f func(i int, item T) bool) (found bool)
- func (c *Collection[T]) Count(item T) (count int)
- func (c *Collection[T]) CountBy(f func(T) bool) (count int)
- func (c *Collection[T]) Each(f func(int, T) bool) *Collection[T]
- func (c *Collection[T]) Empty() *Collection[T]
- func (c *Collection[T]) Filter(f func(T) bool) (out Collection[T])
- func (c *Collection[T]) Find(f func(i int, item T) bool) (item T)
- func (c *Collection[T]) FindIndex(f func(i int, item T) bool) int
- func (c *Collection[T]) InsertAfter(item T, index int) *Collection[T]
- func (c *Collection[T]) InsertAt(item T, index int) *Collection[T]
- func (c *Collection[T]) InsertBefore(item T, index int) *Collection[T]
- func (c *Collection[T]) IsEmpty() bool
- func (c *Collection[T]) Items() []T
- func (c *Collection[T]) LastIndexOf(item T) int
- func (c *Collection[T]) Length() int
- func (c *Collection[T]) Map(f func(int, T) T) (out Collection[T])
- func (c *Collection[T]) MarshalJSON() ([]byte, error)
- func (c *Collection[T]) None(f func(i int, item T) bool) bool
- func (c *Collection[T]) Pop() (out T, found bool)
- func (c *Collection[T]) Push(items ...T) int
- func (c *Collection[T]) PushDistinct(items ...T) int
- func (c *Collection[T]) Random() (T, bool)
- func (c *Collection[T]) RandomIndex() int
- func (c *Collection[T]) Reduce(f func(i int, item, accumulator T) T) (out T)
- func (c *Collection[T]) Reverse() *Collection[T]
- func (c *Collection[T]) Shift() T
- func (c *Collection[T]) Slice(from, to int) *Collection[T]
- func (c *Collection[T]) Some(f func(i int, item T) bool) bool
- func (c *Collection[T]) Sort(less func(i, j int) bool) *Collection[T]
- func (c *Collection[T]) Unshift(item T) int
Examples ¶
- Collection.All
- Collection.At
- Collection.AtFirst
- Collection.AtLast
- Collection.Batch
- Collection.Concat
- Collection.Contains
- Collection.ContainsBy
- Collection.Count
- Collection.CountBy
- Collection.Each
- Collection.Empty
- Collection.Filter
- Collection.Find
- Collection.FindIndex
- Collection.InsertAfter
- Collection.InsertAt
- Collection.InsertBefore
- Collection.IsEmpty
- Collection.Items
- Collection.LastIndexOf
- Collection.Length
- Collection.Map
- Collection.MarshalJSON
- Collection.None
- Collection.Pop
- Collection.Push
- Collection.PushDistinct
- Collection.Random
- Collection.RandomIndex
- Collection.Reduce
- Collection.Reverse
- Collection.Shift
- Collection.Slice
- Collection.Some
- Collection.Sort (Alpha)
- Collection.Sort (Numeric)
- Collection.Unshift
- New
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection[T any] struct { // contains filtered or unexported fields }
func New ¶
func New[T any](items ...T) *Collection[T]
New returns a new collection of type T containing the specified items and their types. ( Chainable )
Example ¶
fruits := collection.New("apple", "orange", "strawberry", "cherry", "banana", "apricot") fmt.Println("Fruits:", fruits.Length()) fruits.Each(func(index int, item string) bool { fmt.Println("-", item) return false })
Output: Fruits: 6 - apple - orange - strawberry - cherry - banana - apricot
func (*Collection[T]) All ¶
func (c *Collection[T]) All(f func(i int, item T) bool) bool
All returns a true value if all items within the current collection resolve to true as defined by the predicate function f.
Example ¶
c := collection.New("apple", "orange", "strawberry") fmt.Println("Contains all items?", c.All(func(i int, item string) bool { return c.Contains(item) }))
Output: Contains all items? true
func (*Collection[T]) At ¶
func (c *Collection[T]) At(index int) (T, bool)
At attempts to return the item associated with the specified index for the current collection along with a boolean value stating whether or not an item could be found.
Example ¶
item, ok := collection.New("apple", "orange", "strawberry").At(1) fmt.Println(item, ok)
Output: orange true
func (*Collection[T]) AtFirst ¶
func (c *Collection[T]) AtFirst() (T, bool)
AtFirst attempts to return the first item of the collection along with a boolean value stating whether or not an item could be found.
Example ¶
first, ok := collection.New("apple", "orange", "strawberry").AtFirst() fmt.Println(first, ok)
Output: apple true
func (*Collection[T]) AtLast ¶
func (c *Collection[T]) AtLast() (T, bool)
AtLast attempts to return the last item of the collection along with a boolean value stating whether or not an item could be found.
Example ¶
last, ok := collection.New("apple", "orange", "strawberry").AtLast() fmt.Println(last, ok)
Output: strawberry true
func (*Collection[T]) Batch ¶ added in v1.0.9
func (c *Collection[T]) Batch(f func(int, int, T), batchSize int) *Collection[T]
Batch exposes a very simple parallelized batch processor for a collection. Defining a `batchSize` will break the current collection into groups of jobs that will be processed in parallel by Goroutines managed by an error group. The specified function `f` will be executed for each job in each batch. The signature for this function is `func(currentBatchIndex, currentJobIndex int, job T)`. ( Chainable )
Example ¶
type Job struct { Timestamp int64 Processed bool } jobs := make([]*Job, 0) for i := 1; i <= 100; i++ { jobs = append(jobs, &Job{time.Now().UnixNano(), false}) } c1 := collection.New(jobs...) c1.Batch(func(b, j int, job *Job) { job.Processed = true }, 5) processed := c1.All(func(i int, job *Job) bool { return job.Processed == true }) fmt.Printf("processed %d/%d jobs:%v\n", c1.Length(), c1.Length(), processed)
Output: processed 100/100 jobs:true
func (*Collection[T]) Concat ¶
func (c *Collection[T]) Concat(items []T) *Collection[T]
Concat merges two slices of items. This method returns the current instance collection with the specified slice of items appended to it. ( Chainable )
Example ¶
collection.New("apple", "orange", "strawberry").Concat([]string{"dog", "cat", "horse"}).Each(func(index int, item string) bool { fmt.Println(item) return false })
Output: apple orange strawberry dog cat horse
func (*Collection[T]) Contains ¶
func (c *Collection[T]) Contains(item T) (found bool)
Contains returns true if an item is present in the current collection. This method makes use of `reflect.DeepEqual` to ensure an absolute match. If you wish to check by a specific field within a slice of objects, use `collection.ContainsBy` instead.
Example ¶
fmt.Println(collection.New("apple", "orange", "strawberry").Contains("horse"))
Output: false
func (*Collection[T]) ContainsBy ¶ added in v1.0.8
func (c *Collection[T]) ContainsBy(f func(i int, item T) bool) (found bool)
ContainsBy returns true if an item in the current collection matches the specified predicate function. This is useful if you have a slice of objects and you wish to check the existence of a specific field value.
Example ¶
type Person struct { Name string Age int } people := []Person{ {"wilhelm", 31}, {"luke", 42}, {"rob", 17}, {"peter", 26}, {"josh", 26}, } fmt.Println(collection.New(people...).ContainsBy(func(i int, p Person) bool { return p.Age < 20 }))
Output: true
func (*Collection[T]) Count ¶
func (c *Collection[T]) Count(item T) (count int)
Count counts the number of items in the collection that compare equal to value.
Example ¶
count := collection.New("apple", "orange", "orange", "strawberry").Count("orange") fmt.Println("Orange Count:", count)
Output: Orange Count: 2
func (*Collection[T]) CountBy ¶
func (c *Collection[T]) CountBy(f func(T) bool) (count int)
CountBy counts the number of items in the collection for which predicate is true.
Example ¶
count := collection.New("apple", "orange", "strawberry", "blueberry").CountBy(func(item string) bool { return strings.HasSuffix(item, "berry") }) fmt.Println("Berry Types:", count)
Output: Berry Types: 2
func (*Collection[T]) Each ¶
func (c *Collection[T]) Each(f func(int, T) bool) *Collection[T]
Each iterates through the specified list of items executes the specified callback on each item. This method returns the current instance of collection. ( Chainable )
Example ¶
collection.New("apple", "orange", "strawberry").Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: 0 apple 1 orange 2 strawberry
func (*Collection[T]) Empty ¶
func (c *Collection[T]) Empty() *Collection[T]
Empty will reset the current collection to zero items. ( Chainable )
Example ¶
fmt.Println(collection.New("apple", "orange", "strawberry").Empty().Length())
Output: 0
func (*Collection[T]) Filter ¶
func (c *Collection[T]) Filter(f func(T) bool) (out Collection[T])
Filter returns a new collection with items that have passed predicate check. ( Chainable )
Example ¶
c := collection.New("apple", "orange", "strawberry").Filter(func(item string) bool { return item == "apple" }) c.Each(func(index int, item string) bool { fmt.Println(item) return false })
Output: apple
func (*Collection[T]) Find ¶
func (c *Collection[T]) Find(f func(i int, item T) bool) (item T)
Find returns the first item in the provided current collectionthat satisfies the provided testing function. If no items satisfy the testing function, a <nil> value is returned.
Example ¶
fmt.Println(collection.New("apple", "orange", "strawberry").Find(func(i int, item string) bool { return item == "orange" }))
Output: orange
func (*Collection[T]) FindIndex ¶
func (c *Collection[T]) FindIndex(f func(i int, item T) bool) int
FindIndex returns the index of the first item in the specified collection that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
Example ¶
fmt.Println(collection.New("apple", "orange", "strawberry").FindIndex(func(i int, item string) bool { return item == "orange" }))
Output: 1
func (*Collection[T]) InsertAfter ¶
func (c *Collection[T]) InsertAfter(item T, index int) *Collection[T]
InsertAfter inserts the specified item after the specified index and returns the current collection. If the specified index is less than 0, 0 is used. If an index greater than the size of the collectio nis specified, c.Push is used instead. ( Chainable )
Example ¶
collection.New("apple", "orange", "strawberry").InsertAfter("banana", 1).Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: 0 apple 1 orange 2 banana 3 strawberry
func (*Collection[T]) InsertAt ¶
func (c *Collection[T]) InsertAt(item T, index int) *Collection[T]
InsertAt inserts the specified item at the specified index and returns the current collection. If the specified index is less than 0, 0 is used. If an index greater than the size of the collectio nis specified, c.Push is used instead. ( Chainable )
Example ¶
collection.New("apple", "orange", "strawberry").InsertAt("banana", 2).Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: 0 apple 1 orange 2 banana 3 strawberry
func (*Collection[T]) InsertBefore ¶
func (c *Collection[T]) InsertBefore(item T, index int) *Collection[T]
InsertBefore inserts the specified item before the specified index and returns the current collection. If the specified index is less than 0, c.Unshift is used. If an index greater than the size of the collection is specified, c.Push is used instead. ( Chainable )
Example ¶
collection.New("apple", "orange", "strawberry").InsertBefore("banana", 2).Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: 0 apple 1 banana 2 orange 3 strawberry
func (*Collection[T]) IsEmpty ¶
func (c *Collection[T]) IsEmpty() bool
IsEmpty returns a boolean value describing the empty state of the current collection.
Example ¶
c := collection.New("lonely") fmt.Println(c.IsEmpty()) c.Empty() fmt.Println(c.IsEmpty())
Output: false true
func (*Collection[T]) Items ¶ added in v1.0.3
func (c *Collection[T]) Items() []T
Items returns the current collection's set of items.
Example ¶
c := collection.New("apple", "orange", "strawberry") for i, item := range c.Items() { fmt.Println(i, item) }
Output: 0 apple 1 orange 2 strawberry
func (*Collection[T]) LastIndexOf ¶
func (c *Collection[T]) LastIndexOf(item T) int
LastIndexOf returns the last index at which a given item can be found in the current collection, or -1 if it is not present.
Example ¶
fmt.Println(collection.New("apple", "orange", "orange", "strawberry").LastIndexOf("orange"))
Output: 2
func (*Collection[T]) Length ¶
func (c *Collection[T]) Length() int
Length returns number of items associated with the current collection.
Example ¶
fmt.Println("Collection Length:", collection.New("apple", "orange", "strawberry").Length())
Output: Collection Length: 3
func (*Collection[T]) Map ¶
func (c *Collection[T]) Map(f func(int, T) T) (out Collection[T])
Map method creates to a new collection by using callback invocation result on each array item. On each iteration f is invoked with arguments: index and current item. It should return the new collection. ( Chainable )
Example ¶
c := collection.New("apple", "orange", "strawberry").Map(func(i int, item string) string { return fmt.Sprintf("The %s is yummo!", item) }) c.Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: 0 The apple is yummo! 1 The orange is yummo! 2 The strawberry is yummo!
func (*Collection[T]) MarshalJSON ¶ added in v1.0.6
func (c *Collection[T]) MarshalJSON() ([]byte, error)
MarshalJSON implements the Marshaler interface so the current collection's items can be marshalled into valid JSON.
Example ¶
var buffer strings.Builder encoder := json.NewEncoder(&buffer) encoder.Encode(collection.New("apple", "orange", "strawberry")) fmt.Println(buffer.String())
Output: ["apple","orange","strawberry"]
func (*Collection[T]) None ¶
func (c *Collection[T]) None(f func(i int, item T) bool) bool
None returns a true value if no items within the current collection resolve to true as defined by the predicate function f.
Example ¶
found := collection.New("apple", "orange", "strawberry").Some(func(i int, item string) bool { return item == "blackberry" }) fmt.Println("Found \"blackberry\"?", found)
Output: Found "blackberry"? false
func (*Collection[T]) Pop ¶
func (c *Collection[T]) Pop() (out T, found bool)
Pop method removes the last item from the current collection and then returns that item.
Example ¶
item, ok := collection.New("apple", "orange", "strawberry").Pop() fmt.Println(item, ok)
Output: strawberry true
func (*Collection[T]) Push ¶
func (c *Collection[T]) Push(items ...T) int
Push method appends one or more items to the end of a collection, returning the new length.
Example ¶
c := collection.New("apple", "orange", "strawberry") fmt.Println("Collection Length:", c.Push("blueberry", "watermelon")) c.Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: Collection Length: 5 0 apple 1 orange 2 strawberry 3 blueberry 4 watermelon
func (*Collection[T]) PushDistinct ¶ added in v1.0.4
func (c *Collection[T]) PushDistinct(items ...T) int
PushDistinct method appends one or more distinct items to the current collection, returning the new length. Items that already exist within the current collection will be ignored. You can check for this by comparing old v.s. new collection lengths.
Example ¶
c := collection.New("apple", "orange", "strawberry") c.PushDistinct("orange", "orange", "watermelon") c.Each(func(index int, item string) bool { fmt.Println(item) return false })
Output: apple orange strawberry watermelon
func (*Collection[T]) Random ¶
func (c *Collection[T]) Random() (T, bool)
Random returns a random item from the current collection.
Example ¶
item, ok := collection.New("apple", "orange", "strawberry").Random() if ok { fmt.Println("My random item is:", item) }
Output:
func (*Collection[T]) RandomIndex ¶
func (c *Collection[T]) RandomIndex() int
RandomIndex returns the index associated with a random item from the current collection.
Example ¶
index := collection.New("apple", "orange", "strawberry").RandomIndex() fmt.Println("My random index is:", index)
Output:
func (*Collection[T]) Reduce ¶
func (c *Collection[T]) Reduce(f func(i int, item, accumulator T) T) (out T)
Reduce reduces a collection to a single value. The value is calculated by accumulating the result of running each item in the collection through an accumulator function. Each successive invocation is supplied with the return value returned by the previous call.
Example ¶
acc := collection.New("apple", "orange", "strawberry").Reduce(func(i int, item, accumulator string) string { return accumulator + item }) fmt.Println(acc)
Output: appleorangestrawberry
func (*Collection[T]) Reverse ¶
func (c *Collection[T]) Reverse() *Collection[T]
Reverse the current collection so that the first item becomes the last, the second item becomes the second to last, and so on. ( Chainable )
Example ¶
collection.New("apple", "orange", "orange", "strawberry").Reverse().Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: 0 strawberry 1 orange 2 orange 3 apple
func (*Collection[T]) Shift ¶
func (c *Collection[T]) Shift() T
Shift method removes the first item from the current collection, then returns that item.
Example ¶
fmt.Println(collection.New("apple", "orange", "strawberry").Shift())
Output: apple
func (*Collection[T]) Slice ¶
func (c *Collection[T]) Slice(from, to int) *Collection[T]
Slice returns a new collection containing a slice of the current collection starting with `from` and `to` indexes. ( Chainable )
Example ¶
collection.New("apple", "orange", "strawberry").Slice(0, 2).Each(func(i int, item string) bool { fmt.Println(item) return false })
Output: apple orange
func (*Collection[T]) Some ¶
func (c *Collection[T]) Some(f func(i int, item T) bool) bool
Some returns a true value if at least one item within the current collection resolves to true as defined by the predicate function f.
Example ¶
found := collection.New("apple", "orange", "strawberry").Some(func(i int, item string) bool { return item == "orange" }) fmt.Println("Found \"orange\"?", found)
Output: Found "orange"? true
func (*Collection[T]) Sort ¶ added in v1.0.7
func (c *Collection[T]) Sort(less func(i, j int) bool) *Collection[T]
Sort sorts the collection given the provided less function. ( Chainable )
Example (Alpha) ¶
names := collection.New("wilhelm", "peter", "josh", "luke", "rob") fmt.Println("Unsorted:") fmt.Println(strings.Join(names.Items(), ",")) names.Sort(func(i, j int) bool { left, _ := names.At(i) right, _ := names.At(j) return left < right }) fmt.Println("Sorted:") fmt.Println(strings.Join(names.Items(), ","))
Output: Unsorted: wilhelm,peter,josh,luke,rob Sorted: josh,luke,peter,rob,wilhelm
Example (Numeric) ¶
numbers := collection.New(1, 4, 2, 3) fmt.Println("Unsorted:") numbers.Each(func(i, n int) bool { fmt.Print(" ", n) return false }) fmt.Println("\nSorted:") numbers.Sort(func(i, j int) bool { left, _ := numbers.At(i) right, _ := numbers.At(j) return left < right }).Each(func(i, n int) bool { fmt.Print(" ", n) return false })
Output: Unsorted: 1 4 2 3 Sorted: 1 2 3 4
func (*Collection[T]) Unshift ¶
func (c *Collection[T]) Unshift(item T) int
Unshift method appends one item to the beginning of the current collection, returning the new length of the collection.
Example ¶
c := collection.New("apple", "orange", "strawberry") fmt.Println("Length Current:", c.Length()) fmt.Println("Length New: ", c.Unshift("horse")) c.Each(func(i int, item string) bool { fmt.Println(i, item) return false })
Output: Length Current: 3 Length New: 4 0 horse 1 apple 2 orange 3 strawberry