collection

package
v2.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 2 Imported by: 0

README

Collection

Example

package main

import "github.com/go-kratos-ecosystem/components/v2/collection"

func main() {
	// int
	c := collection.New([]int{1, 2, 3})
	c.Add(4) //nolint:gomnd
	c.Items()
	c.All()
	c.Len()
	c.Map(func(i int, _ int) int {
		return i * 2
	})
	c.Filter(func(v int, _ int) bool {
		return v > 4
	})
	c.Where(func(v int, _ int) bool {
		return v > 4
	})
	c.Reduce(func(a, b int) int {
		return a + b
	})
	// ....

	// string
	c2 := collection.New([]string{"a", "b", "c"})
	c2.Add("d")

	// and so on
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection[T comparable] struct {
	// contains filtered or unexported fields
}

func New

func New[T comparable](items []T) *Collection[T]

func (*Collection[T]) Add

func (c *Collection[T]) Add(items ...T)

func (*Collection[T]) All

func (c *Collection[T]) All() []T

func (*Collection[T]) Contains

func (c *Collection[T]) Contains(item T) bool

func (*Collection[T]) Dump added in v2.6.1

func (c *Collection[T]) Dump()

func (*Collection[T]) Each

func (c *Collection[T]) Each(fn func(T, int))

func (*Collection[T]) Exists

func (c *Collection[T]) Exists(item T) bool

func (*Collection[T]) Filter

func (c *Collection[T]) Filter(fn func(T, int) bool) *Collection[T]

func (*Collection[T]) Find

func (c *Collection[T]) Find(fn func(T, int) bool) (T, bool)

func (*Collection[T]) First

func (c *Collection[T]) First() (T, bool)

func (*Collection[T]) Has

func (c *Collection[T]) Has(item T) bool

func (*Collection[T]) Index

func (c *Collection[T]) Index(fn func(T, int) bool) (int, bool)

func (*Collection[T]) IsEmpty

func (c *Collection[T]) IsEmpty() bool

func (*Collection[T]) IsNotEmpty

func (c *Collection[T]) IsNotEmpty() bool

func (*Collection[T]) Items

func (c *Collection[T]) Items() []T

func (*Collection[T]) Last

func (c *Collection[T]) Last() (T, bool)

func (*Collection[T]) Len

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

func (*Collection[T]) Map

func (c *Collection[T]) Map(fn func(T, int) T) *Collection[T]

func (*Collection[T]) Missing

func (c *Collection[T]) Missing(item T) bool

func (*Collection[T]) Reduce

func (c *Collection[T]) Reduce(fn func(T, T) T) T

func (*Collection[T]) Reverse

func (c *Collection[T]) Reverse() *Collection[T]

func (*Collection[T]) SortBy

func (c *Collection[T]) SortBy(fn func(T, T) bool) *Collection[T]

func (*Collection[T]) Unique

func (c *Collection[T]) Unique() *Collection[T]

func (*Collection[T]) Unless added in v2.8.0

func (c *Collection[T]) Unless(condition bool, fns ...func(*Collection[T])) *Collection[T]

func (*Collection[T]) When added in v2.8.0

func (c *Collection[T]) When(condition bool, fns ...func(*Collection[T])) *Collection[T]

func (*Collection[T]) Where

func (c *Collection[T]) Where(fn func(T, int) bool) *Collection[T]

Jump to

Keyboard shortcuts

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