collection

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2024 License: MIT Imports: 2 Imported by: 0

README

collection

基于泛型的集合操作,可以多补充些

Each

仅作用于基础类型指针类型的数据集
type Book struct {
    Name  string
    Price int
}
books := []*Book{
    &{Name: "a", Price: 2},
    &{Name: "b", Price: 3},
    &{Name: "c", Price: 4},
}
collection.New(books).Each(func(b *Book){
    b.Price = b.Price + 1
}).Value()

Map

比较万能
bookss := []Book{
    {Name: "a", Price: 2},
    {Name: "b", Price: 3},
    {Name: "c", Price: 4},
}
bbbss := collection.New(bookss).Map(func(b Book) Book {
    b.Price = b.Price + 1
    return b
}).Filter(func(i Book) bool {
    return i.Price >= 3
}).Value()

for _, b := range bbbss {
    fmt.Println(b.Price) // 3,4,5
}

Filter

books := []*Book{
    {Name: "a", Price: 2},
    {Name: "b", Price: 3},
    {Name: "c", Price: 4},
}
bb := collection.New(books).Each(func(b *Book) {
    b.Price = b.Price + 1
}).Filter(func(i *Book) bool {
    return i.Price >= 4
}).Value()

for _, b := range bb {
    fmt.Println(b.Name) // 3,4
}

Documentation

Overview

Package collection provide a collection type that can set generics type for any type or any pointer

Index

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 can be used to create a new collection you can use collection.New([]string{"a", "b", "c"}) or collection.New([]int{1, 2, 3}) or collection.New([]Book{{Name: "a", Price: 2}, {Name: "b", Price: 3}, {Name: "c", Price: 4}}) or collection.New([]*Book{{Name: "a", Price: 2}, {Name: "b", Price: 3}, {Name: "c", Price: 4}})

func (*Collection[T]) Each

func (c *Collection[T]) Each(f func(i T)) *Collection[T]

Each can be used to iterate over the collection

func (*Collection[T]) Filter

func (c *Collection[T]) Filter(f func(i T) bool) *Collection[T]

Filter can be used to filter the collection only when the function returns true

func (*Collection[T]) Len

func (c *Collection[T]) Len() int

Len can be used to get the length

func (*Collection[T]) Map

func (c *Collection[T]) Map(f func(i T) T) *Collection[T]

Map can be used to map the collection

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 can be used to get the value

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() []T

Value can be used to get the value slice

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL