collection

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: MIT Imports: 3 Imported by: 0

README

collection

Generic 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()

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

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 >= 4
}).Value()

for b := range bbbss {
    fmt.Println(b.Price) // 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
}

Merge

bb := collection.New([]*Book{
    {Name: "a", Price: 2},
    {Name: "b", Price: 3},
    {Name: "c", Price: 4},
})

bb.Merge(
    collection.New(
        []*Book{
            {Name: "d", Price: 5},
            {Name: "e", Price: 6},
        },
    ),
    collection.New(
        []*Book{
            {Name: "f", Price: 7},
            {Name: "g", Price: 8},
        },
    ),
)


for b := range bb.Value() {
    fmt.Println(b.Price) // 2,3,4,5,6,7,8
}

Sort

books := []*Book{
    {Name: "a", Price: 2},
    {Name: "b", Price: 3},
    {Name: "c", Price: 4},
}

bb := collection.New(books).Sort(func(i, j *Book) bool {
    return i.Price > j.Price
}).Value()

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

Peek

books := []*Book{
    {Name: "a", Price: 2},
    {Name: "b", Price: 3},
    {Name: "c", Price: 4},
}

bb := collection.New(books).Sort(func(i, j *Book) bool {
    return i.Price > j.Price
})

fmt.Println(bb.Peek(0)) // 4
fmt.Println(bb.Peekz(-1)) // 2



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

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.

Jump to

Keyboard shortcuts

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