Documentation
¶
Overview ¶
Package collection provides a generic collection type that can hold any type or any pointer type.
This package allows the creation of a collection of any type, including custom types and pointers to custom types.
Example usage:
package main
import (
"fmt"
"github.com/yourusername/collection"
)
type Book struct {
Name string
Price int
}
func main() {
c := collection.New([]string{"a", "b", "c"})
c.Each(func(i string) {
fmt.Println(i)
})
books := collection.New([]Book{{Name: "a", Price: 2}, {Name: "b", Price: 3}, {Name: "c", Price: 4}})
books.Each(func(b Book) {
fmt.Printf("Book: %s, Price: %d\n", b.Name, b.Price)
})
books.Merge(
collection.New(
[]*Book{
{Name: "d", Price: 5},
{Name: "e", Price: 6},
},
),
collection.New(
[]*Book{
{Name: "f", Price: 7},
{Name: "g", Price: 8},
},
),
)
books.Sort(func(i, j *Book) bool {
return i.Price > j.Price
})
fmt.Println(books.Peek(0))
}
Index ¶
- type Collection
- func (c *Collection[T]) All() iter.Seq2[int, T]
- func (c *Collection[T]) Clone() *Collection[T]
- func (c *Collection[T]) Each(f func(i T)) *Collection[T]
- func (c *Collection[T]) Filter(f func(i T) bool) *Collection[T]
- func (c *Collection[T]) Len() int
- func (c *Collection[T]) Map(f func(i T) T) *Collection[T]
- func (c *Collection[T]) Merge(other ...*Collection[T])
- func (c *Collection[T]) Peek(i int) T
- func (c *Collection[T]) Reverse() *Collection[T]
- func (c *Collection[T]) Sort(f func(i, j T) bool) *Collection[T]
- func (c *Collection[T]) Value() iter.Seq[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection[T item] struct {
// contains filtered or unexported fields
}
Collection can set generics for any type or any pointer you must choose using pointer or not
func New ¶
func New[T item](d []T) *Collection[T]
New creates a new collection from a slice of any type or pointer type.
Example usage:
collection.New([]string{"a", "b", "c"})
collection.New([]int{1, 2, 3})
collection.New([]Book{{Name: "a", Price: 2}, {Name: "b", Price: 3}, {Name: "c", Price: 4}})
collection.New([]*Book{{Name: "a", Price: 2}, {Name: "b", Price: 3}, {Name: "c", Price: 4}})
func (*Collection[T]) All ¶ added in v0.1.0
func (c *Collection[T]) All() iter.Seq2[int, T]
All returns a interator that yields index their values in the collection.
func (*Collection[T]) Clone ¶ added in v0.1.0
func (c *Collection[T]) Clone() *Collection[T]
Clone returns a new collection that is a copy of the current collection.
func (*Collection[T]) Each ¶
func (c *Collection[T]) Each(f func(i T)) *Collection[T]
Each iterates over the collection and applies the given function to each item.
func (*Collection[T]) Filter ¶
func (c *Collection[T]) Filter(f func(i T) bool) *Collection[T]
Filter filters the collection using the given function and retains only the items for which the function returns true.
func (*Collection[T]) Len ¶
func (c *Collection[T]) Len() int
Len returns the length of the collection.
func (*Collection[T]) Map ¶
func (c *Collection[T]) Map(f func(i T) T) *Collection[T]
Map applies the given function to each item in the collection and replaces the item with the result.
func (*Collection[T]) Merge ¶
func (c *Collection[T]) Merge(other ...*Collection[T])
Merge can be used to merge two collections
func (*Collection[T]) Peek ¶
func (c *Collection[T]) Peek(i int) T
Peek returns the item at the specified index. If the index is negative, it returns the item from the end of the collection.
func (*Collection[T]) Reverse ¶ added in v0.1.0
func (c *Collection[T]) Reverse() *Collection[T]
Reverse reverses the order of the collection.
func (*Collection[T]) Sort ¶
func (c *Collection[T]) Sort(f func(i, j T) bool) *Collection[T]
Sort sorts the elements in the Collection using the provided function.
func (*Collection[T]) Value ¶
func (c *Collection[T]) Value() iter.Seq[T]
Value returns a interator that yields all the items value in the collection.