collect

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: MIT Imports: 10 Imported by: 3

README

go-collection

English | 简体中文

go-collection provides developers with a convenient set of functions for working with common slices, maps, and arrays data. These functions are based on the generic types of Go 1.18, which makes it easier to use them without annoying type assertions. In addition to using these functions directly, it also supports method chaining.

collect.Reduce(collect.Filter(collect.Map([]int{1, 2, 3}, fn), fn), fn)

Equivalent to:

collect.UseSlice([]int{1, 2, 3}).Map(fn).Filter(fn).Reduce(fn)

Installation

go get -u github.com/sxyazi/go-collection

Then import it

import collect "github.com/sxyazi/go-collection"

API

Its API is very simple and if you have used other similar packages, you should be able to get started with it in a few minutes. For convenience, they are described below in function form.

Slice

The corresponding chained function is collect.UseSlice()

  • Len gets the length of the slice

    Examples
    d1 := []int{1, 2, 3}
    collect.Len(d1) // 3
    
    d2 := []string{"a", "b", "c"}
    collect.Len(d2) // 3
    
  • Each iterates over each element in the slice

    Examples
    d := []float64{1, 2, 3}
    collect.Each(d, func(value float64, index int) {
      fmt.Println(index, value)
    })
    
  • Empty checks if the slice is empty

    Examples
    var d []int
    collect.Empty(d) // true
    
  • Same checks if the contents of two slices are the same

    Examples
    d1 := []int{1, 2, 3}
    d2 := []int{1, 2, 3}
    collect.Same(d1, d2) // true
    
    d3 := [][]int{{1, 2, 3}, {4, 5, 6}}
    d4 := [][]int{{1, 2, 3}, {4, 5, 6}}
    collect.Same(d3, d4) // true
    
  • First gets the first element of the slice

    Examples
    d1 := []int{1, 2, 3}
    value, ok := collect.First(d1) // 1, true
    
    var d2 []int
    value, ok = collect.First(d2) // 0, false
    
  • Last gets the last element of the slice

    Examples
    d1 := []int{1, 2, 3}
    value, ok := collect.Last(d1) // 3, true
    
    var d2 []int
    value, ok = collect.Last(d2) // 0, false
    
  • Index gets the index of the specified element in the slice, and returns -1 if it does not exist.

    Examples
    d1 := []int{1, 2, 3}
    collect.Index(d1, 2) // 1
    
    s1 := []string{"a", "b", "c"}
    s2 := []string{"d", "e", "f"}
    collect.Index([][]string{s1, s2}, s2) // 1
    
  • Contains checks if the slice contains the specified element

    Examples
    d1 := []int{1, 2, 3}
    collect.Contains(d1, 2) // true
    
    s1 := []string{"a", "b", "c"}
    s2 := []string{"d", "e", "f"}
    collect.Contains([][]string{s1, s2}, s2) // true
    
  • Diff computes the difference set of two slices

    Examples
    d := []int{1, 2, 3}
    collect.Diff(d, []int{2, 3})  // []int{1}
    
  • Filter filters the elements in the slice

    Examples
    collect.Filter([]int{1, 2, 3, 4, 5}, func(value, index int) bool {
      return value % 2 == 0
    })  // []int{2, 4}
    
  • Map iterates over and sets the value of the elements in the slice

    Examples
    collect.Map([]int{1, 2, 3}, func(value, index int) int {
      return value * 2
    })  // []int{2, 4, 6}
    
  • Unique removes duplicate elements in the slice

    Examples
    d := []int{1, 2, 3, 3, 4}
    collect.Unique(d)  // []int{1, 2, 3, 4}
    
  • Duplicates gets the duplicate elements in the slice

    Examples
    d := []string{"a", "b", "a", "c"}
    collect.Duplicates(d)  // map[int]string{2: "a"}
    
  • Merge merges the current slice with other slices

    Examples
    d1 := []int{1, 2}
    d2 := []int{3, 4}
    d3 := []int{5, 6}
    
    collect.Merge(d1, d2)      // []int{1, 2, 3, 4}
    collect.Merge(d1, d2, d3)  // []int{1, 2, 3, 4, 5, 6}
    
  • Random gets an element of the slice at random

    Examples
    d := []int{1, 2}
    value, ok := collect.Random(d)  // 1 or 2, true
    
    d := []int{}
    value, ok := collect.Random(d)  // 0, false
    
  • Reverse reverses the elements in a slice

    Examples
    d := []int{1, 2}
    collect.Reverse(d)  // []int{2, 1}
    
  • Shuffle randomly shuffles the elements in a slice

    Examples
    d := []int{1, 2}
    collect.Shuffle(d)  // []int{1, 2} or []int{2, 1}
    
  • Slice takes a segment from a slice

    Examples

    Function signature: Slice(items T, offset int)

    d := []int{1, 2, 3, 4, 5}
    collect.Slice(d, 2)   // []int{3, 4, 5}
    collect.Slice(d, -1)  // []int{5}
    collect.Slice(d, -2)  // []int{4, 5}
    

    Function signature: Slice(items T, offset, length int)

    d := []int{1, 2, 3, 4, 5}
    collect.Slice(d, 0, 2)   // []int{1, 2}
    collect.Slice(d, 2, 3)   // []int{3, 4, 5}
    collect.Slice(d, 3, -2)  // []int{3, 4}
    collect.Slice(d, -4, 3)  // []int{2, 3, 4}
    
  • Split splits a slice into multiple slices by the specified amount

    Examples
    d := []int{1, 2, 3, 4, 5}
    collect.Split(d, 2)  // [][]int{{1, 2}, {3, 4}, {5}}
    
  • Splice removes a segment from the slice

    Examples

    Function signature: Splice(items T, offset int)

    d := []int{1, 2, 3, 4, 5}
    collect.Splice(&d, 2)  // []int{3, 4, 5}
    d                      // []int{1, 2}
    

    Function signature: Splice(items T, offset, length int)

    d := []int{1, 2, 3, 4, 5}
    collect.Splice(&d, 2, 2)  // []int{3, 4}
    d                         // []int{1, 2, 5}
    

    Function signature: Splice(items T, offset, length int, replacements ...T|E)

    d1 := []int{1, 2, 3, 4}
    collect.Splice(&d1, 1, 2, []int{22, 33})  // []int{2, 3}
    d1                                        // []int{1, 22, 33, 4}
    
    d2 := []int{1, 2, 3, 4}
    collect.Splice(&d2, 1, 2, 22, 33)  // []int{2, 3}
    d2                                 // []int{1, 22, 33, 4}
    
    d3 := []int{1, 2, 3, 4}
    collect.Splice(&d3, 1, 2, []int{22}, 33, []int{55})  // []int{2, 3}
    d3                                                   // []int{1, 22, 33, 55, 4}
    

    It is worth noting that this method also supports the use of negative numbers as arguments, and its behavior is the same as that of Slice, which is not repeated here due to space constraints.

  • Reduce reduces the collection to a single value, and the parameters of each iteration are the results of the previous iteration

    Examples
    collect.Reduce([]int{1, 2, 3}, 100, func(carry, value, key int) int {
    	return carry + value
    })  // 106
    
  • Pop removes and returns the last element of the collection

    Examples
    d := []int{1, 2}
    v, ok := collect.Pop(&d)  // 2, true
    d                         // []int{1}
    
    c := collect.UseSlice([]int{1, 2})
    v, ok := c.Pop()  // 2, true
    c.All()           // []int{1}
    
  • Push appends an element to the end of a collection

    Examples
    d := []int{1, 2}
    Push(&d, 3)
    d  // []int{1, 2, 3}
    
    collect.UseSlice([]int{1, 2}).Push(3).All()  // []int{1, 2, 3}
    
  • Where filters the collection by the specified rules

    Examples

    Function signature: Where(items T, target any)

    collect.Where([]int{1, 2, 3}, 2)  // []int{2}
    

    Function signature: Where(items T, operator string, target any)

    d := []int{1, 2, 3, 4}
    collect.Where(d, "=", 2)   // []int{2}
    collect.Where(d, "!=", 2)  // []int{1, 3, 4}
    collect.Where(d, ">", 2)   // []int{3, 4}
    collect.Where(d, ">=", 2)  // []int{2, 3, 4}
    collect.Where(d, "<", 3)   // []int{1, 2}
    collect.Where(d, "<=", 3)  // []int{1, 2, 3}
    

    Function signature: Where(items T, key any, target any)

    d := []User{{ID: 1, Name: "Hugo"}, {ID: 2, Name: "Lisa"}, {ID: 3, Name: "Iris"}, {ID: 4, Name: "Lisa"}}
    collect.Where(d, "Name", "Lisa")  // []User{{2 Lisa} {4 Lisa}}
    

    Function signature: Where(items T, key any, operator string, target any)

    d := []User{{ID: 1, Name: "Hugo"}, {ID: 2, Name: "Lisa"}, {ID: 3, Name: "Iris"}, {ID: 4, Name: "Lisa"}}
    collect.Where(d, "Name", "!=", "Lisa")  // []User{{1 Hugo} {3 Iris}}
    
  • WhereIn removes elements from the collection that do not exist in the specified slice

    Examples

    Function signature: WhereIn(items T, targets []any)

    d := []int{1, 2, 3, 4}
    collect.WhereIn(d, []int{2, 3})  // []int{2, 3}
    

    Function signature: WhereIn(items T, key any, targets []any)

    d := []User{{ID: 1, Name: "Hugo"}, {ID: 2, Name: "Lisa"}, {ID: 3, Name: "Iris"}, {ID: 4, Name: "Lisa"}}
    collect.WhereIn(d, "Name", []string{"Hugo", "Iris"})  // []User{{1 Hugo} {3 Iris}}
    
  • WhereNotIn removes elements from the collection that exist in the specified slice

    Examples

    Function signature: WhereNotIn(items T, targets []any)

    d := []int{1, 2, 3, 4}
    collect.WhereNotIn(d, []int{2, 3})  // []int{1, 4}
    

    Function signature: WhereNotIn(items T, key any, targets []any)

    d := []User{{ID: 1, Name: "Hugo"}, {ID: 2, Name: "Lisa"}, {ID: 3, Name: "Iris"}, {ID: 4, Name: "Lisa"}}
    collect.WhereNotIn(d, "Name", []string{"Lisa", "Iris"})  // []User{{1 Hugo}}
    
Array

Exactly the same as slice, you just pass in the array converted to a slice:

arr := [3]int{1, 2, 3}

collect.Len(arr[:])
// or
collect.UseSlice(arr[:]).Len()
Map

The corresponding chained function is collect.UseMap()

  • Len gets the number of elements in the map

    Examples
    d1 := map[string]int{"a": 1, "b": 2, "c": 3}
    collect.Len(d1) // 3
    
  • Empty checks if the map is empty

    Examples
    var d map[string]int
    collect.Empty(d) // true
    
  • Only gets the elements of the map with the specified keys

    Examples
    d := map[string]int{"a": 1, "b": 2, "c": 3}
    collect.Only(d, "a")       // map[string]int{"a": 1}
    collect.Only(d, "a", "b")  // map[string]int{"a": 1, "b": 2}
    
  • Except gets the elements of the map with the specified keys removed

    Examples
    d := map[string]int{"a": 1, "b": 2, "c": 3}
    collect.Except(d, "a")       // map[string]int{"b": 2, "c": 3}
    collect.Except(d, "a", "b")  // map[string]int{"c": 3}
    
  • Keys gets all the keys in the map

    Examples
    d := map[string]int{"a": 1, "b": 2, "c": 3}
    collect.Keys(d)  // []string{"a", "b", "c"}
    
  • DiffKeys compares with the given collection and returns the key/value pairs in the given collection that do not exist in the original collection

    Examples
    d1 := map[string]int{"a": 1, "b": 2, "c": 3}
    d2 := map[string]int{"b": 22, "c": 33}
    
    collect.DiffKeys(d1, d2)  // map[string]int{"a": 1}
    
  • Has checks if the map contains the specified key

    Examples
    d := map[string]int{"a": 1}
    collect.Has(d, "a")  // true
    
  • Get gets the value of the specified key in the map

    Examples
    d := map[string]int{"a": 1}
    
    value, ok := collect.Get(d, "a")  // 1, true
    value, ok := collect.Get(d, "b")  // 0, false
    
  • Put sets the value of the specified key in the map

    Examples
    d := map[string]int{"a": 1}
    collect.Put(d, "b", 2)  // map[string]int{"a": 1, "b": 2}
    
  • Pull removes the specified key from the collection and returns its value

    Examples
    d := map[string]int{"a": 1, "b": 2}
    v, ok := collect.Pull(d, "b")  // 2, true
    d                              // map[string]int{"a": 1}
    
  • Merge merges the current map with other maps

    Examples
    d1 := map[string]int{"a": 1, "b": 2}
    d2 := map[string]int{"b": 22}
    d3 := map[string]int{"b": 222, "c": 3}
    
    collect.MapMerge(d1, d2)            // map[string]int{"a": 1, "b": 22}
    collect.UseMap(d1).Merge(d2).All()  // Equal to the above
    
    collect.MapMerge(d1, d2, d3)            // map[string]int{"a": 1, "b": 222, "c": 3}
    collect.UseMap(d1).Merge(d2, d3).All()  // Equal to the above
    
  • Union unites the current map with other maps, and the items in the original map are given priority

    Examples
    d1 := map[string]int{"a": 1, "b": 2}
    d2 := map[string]int{"b": 22, "c": 3}
    collect.Union(d1, d2)  // map[string]int{"a": 1, "b": 2, "c": 3}
    
Number slice

The corresponding chained function is collect.UseNumber(),which is a subset of slice and includes, in addition to all the methods of slice, the additional:

  • Sum calculates the sum

    Examples
    collect.Sum([]float64{1, 3.14})  // 4.14
    
  • Min calculates the minimum value

    Examples
    collect.Min([]int{0, 1, -3})  // -3
    
  • Max calculates the maximum value

    Examples
    collect.Max([]int{0, 1, -3})  // 1
    
  • Sort sorts the numbers in the collection in ascending order

    Examples
    collect.Sort([]float64{1, -4, 0, -4.3})  // []float64{-4.3, -4, 0, 1}
    
  • SortDesc sorts the numbers in the collection in descending order

    Examples
    collect.SortDesc([]float64{1, -4, 0, -4.3})  // []float64{1, 0, -4, -4.3}
    
  • Avg calculates the average

    Examples
    collect.Avg([]int{1, 2, 3, 4})  // 2.5
    
  • Median calculates the median

    Examples
    collect.Median([]int{1, 2, 3, 4})  // 2.5
    
Standalone functions

Due to Golang's support for generics, it is not possible to define generic types in methods, so only their function implementations (which do not support chain calls) are listed below:

  • AnyGet gets value of arbitrary types (slices, maps, arrays, structures, and pointers to these) in a non-strict form

    Examples
    m := map[string]int{"a": 1, "b": 2}
    collect.AnyGet[int](m, "b")  // 2
    
    u := &User{"Email": "[email protected]"}
    collect.AnyGet[string](u, "Email")  // [email protected]
    
    s := [][]int{{1, 2}, {3, 4}}
    collect.AnyGet[[]int](s, 1)  // []{3, 4}
    
  • Pluck retrieves all values for a given key. supports all values supported by AnyGet

    Examples
    d := []User{{ID: 33, Name: "Lucy"}, {ID: 193, Name: "Peter"}}
    collect.Pluck[int](d, "ID")  // int[]{33, 193}
    
  • MapPluck retrieves all values of a given key, only maps are supported

    Examples
    d := []map[string]int{{"ID": 33, "Score": 10}, {"ID": 193, "Score": 6}}
    collect.MapPluck(d, "ID")  // int[]{33, 193}
    
  • KeyBy retrieves a collection with the value of the given key as the identifier (if there are duplicate keys, only the last one will be kept). Supports all values supported by AnyGet

    Examples
    d := []User{{ID: 33, Name: "Lucy"}, {ID: 193, Name: "Peter"}, {ID: 194, Name: "Peter"}}
    collect.KeyBy[string](d, "Name")  // map[Lucy:{33 Lucy} Peter:{194 Peter}]
    
  • MapKeyBy retrieves the collection with the value of the given key as the identifier (if there are duplicate keys, only the last one will be kept), only maps are supported

    Examples
    d := []map[string]int{{"ID": 33, "Score": 6}, {"ID": 193, "Score": 10}, {"ID": 194, "Score": 10}}
    collect.MapKeyBy(d, "Score")  // map[6:map[ID:33 Score:6] 10:map[ID:194 Score:10]]
    
  • GroupBy groups the items in a collection using the value of the given key as the identifier. Supports all values supported by AnyGet

    Examples
    d := []User{{ID: 33, Name: "Lucy"}, {ID: 193, Name: "Peter"}, {ID: 194, Name: "Peter"}}
    collect.GroupBy[string](d, "Name")  // map[Lucy:[{33 Lucy}] Peter:[{193 Peter} {194 Peter}]]
    
  • MapGroupBy groups items in a collection using the value of the given key as the identifier, only maps are supported

    Examples
    d := []map[string]int{{"ID": 33, "Score": 6}, {"ID": 193, "Score": 10}, {"ID": 194, "Score": 10}}
    collect.MapGroupBy(d, "Score")  // map[6:[map[ID:33 Score:6]] 10:[map[ID:193 Score:10] map[ID:194 Score:10]]]
    
  • Count counts the number of occurrences of each element in the slice

    Examples
    d := []bool{true, true, false}
    collect.Count(d)  // map[bool]int{true: 2, false: 1}
    
  • Times creates a new collection of slice by calling the callback with specified number of times

    Examples
    collect.Times(3, func(number int) float64 {
    	return float64(number) * 3.14
    })  // *SliceCollection{[]float64{3.14, 6.28, 9.42}}
    
  • SortBy calls a callback for each element and performs an ascending sort by the return value of the callback

    Examples
    collect.SortBy([]int{2, 1, 3}, func(item, index int) string {
    	return strconv.Itoa(item)
    })  // *SliceCollection{[]int{1, 2, 3}}
    
  • SortByDesc calls a callback for each element and performs a descending sort by the return value of the callback

    Examples
    collect.SortByDesc([]int{2, 1, 3}, func(item, index int) string {
    	return strconv.Itoa(item)
    })  // *SliceCollection{[]int{3, 2, 1}}
    

License

go-collection is MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyGet

func AnyGet[V, K any](item any, key K) (zero V, _ error)

func AnyNumberCompare added in v0.0.2

func AnyNumberCompare(a any, operator string, b any) bool

func Avg

func Avg[T ~[]E, E constraints.Integer | constraints.Float](items T) float64

func Compare added in v0.0.2

func Compare(a any, operator string, b any) bool

func Contains

func Contains[T ~[]E, E any](items T, item E) bool

func Count

func Count[T ~[]E, E comparable](items T) map[E]int

func Diff

func Diff[T ~[]E, E any](items, target T) T

func DiffKeys

func DiffKeys[T ~map[K]V, K comparable, V any](items T, target T) T

func Duplicates added in v0.0.2

func Duplicates[T ~[]E, E any](items T) map[int]E

func Each

func Each[T ~[]E, E any](items T, callback func(value E, index int))

func Empty

func Empty(v any) bool

func Except

func Except[T ~map[K]V, K comparable, V any](items T, keys ...K) T

func Filter

func Filter[T ~[]E, E any](items T, callback func(value E, index int) bool) T

func First

func First[T ~[]E, E any](items T) (E, bool)

func Get

func Get[T ~map[K]V, K comparable, V any](items T, key K) (value V, _ bool)

func GroupBy

func GroupBy[V comparable, K, I any](items []I, key K) map[V][]I

func Has

func Has[T ~map[K]V, K comparable, V any](items T, key K) bool

func Index

func Index[T ~[]E, E any](items T, target E) int

func IsNumber added in v0.0.2

func IsNumber(v any) bool

func KeyBy

func KeyBy[V comparable, K, I any](items []I, key K) map[V]I

func Keys

func Keys[T ~map[K]V, K comparable, V any](items T) (keys []K)

func Last

func Last[T ~[]E, E any](items T) (E, bool)

func Len

func Len(v any) int

func Map

func Map[T ~[]E, E any](items T, callback func(value E, index int) E) T

func MapGroupBy

func MapGroupBy[K, V comparable](items []map[K]V, key K) map[V][]map[K]V

func MapKeyBy

func MapKeyBy[K, V comparable](items []map[K]V, key K) map[V]map[K]V

func MapMerge

func MapMerge[T ~map[K]V, K comparable, V any](items T, targets ...T) T

func MapPluck

func MapPluck[K comparable, V any](items []map[K]V, key K) []V

func MapSame

func MapSame[T ~map[K]V, K comparable, V any](items, target T) bool

func Max

func Max[T ~[]E, E constraints.Integer | constraints.Float](items T) E

func Median

func Median[T ~[]E, E constraints.Integer | constraints.Float](items T) float64

func Merge

func Merge[T ~[]E, E any](items T, targets ...T) T

func Min

func Min[T ~[]E, E constraints.Integer | constraints.Float](items T) E

func NumberCompare added in v0.0.2

func NumberCompare[T constraints.Integer | constraints.Float](a T, operator string, b T) bool

func OffsetToIndex

func OffsetToIndex(actual, offset int, args ...int) (int, int)

func Only

func Only[T ~map[K]V, K comparable, V any](items T, keys ...K) T

func Pluck

func Pluck[V, K, I any](items []I, key K) []V

func Pop added in v0.0.2

func Pop[T ~[]E, E any](items *T) (E, bool)

func Pull added in v0.0.2

func Pull[T ~map[K]V, K comparable, V any](items T, key K) (value V, _ bool)

func Push added in v0.0.2

func Push[T ~[]E, E any](items *T, item E) T

func Put added in v0.0.2

func Put[T ~map[K]V, K comparable, V any](items T, key K, value V) T

func Random

func Random[T ~[]E, E any](items T) (E, bool)

func Reduce

func Reduce[T ~[]E, E any](items T, initial E, callback func(carry E, value E, key int) E) E

func Reverse

func Reverse[T ~[]E, E any](items T) T

func Same

func Same[T ~[]E, E any](items, target T) bool

func Shuffle

func Shuffle[T ~[]E, E any](items T) T

func Slice

func Slice[T ~[]E, E any](items T, offset int, args ...int) T

func Sort

func Sort[T ~[]E, E constraints.Ordered](items T) T

func SortDesc

func SortDesc[T ~[]E, E constraints.Ordered](items T) T

func Splice

func Splice[T ~[]E, E any](items *T, offset int, args ...any) T

func Split

func Split[T ~[]E, E any](items T, amount int) []T

func StringToNumber

func StringToNumber[T constraints.Integer | constraints.Float](s string) (result T, _ error)

func Sum

func Sum[T ~[]E, E constraints.Integer | constraints.Float](items T) (total E)

func Union

func Union[T ~map[K]V, K comparable, V any](items T, target T) T

func Unique

func Unique[T ~[]E, E any](items T) T

func Where added in v0.0.2

func Where[T ~[]E, E any](items T, args ...any) T

func WhereIn added in v0.0.2

func WhereIn[T ~[]E, E any](items T, args ...any) T

func WhereNotIn added in v0.0.2

func WhereNotIn[T ~[]E, E any](items T, args ...any) T

Types

type ComparisonSet added in v0.0.2

type ComparisonSet struct {
	LooseNumber bool
	// contains filtered or unexported fields
}

func NewComparisonSet added in v0.0.2

func NewComparisonSet(looseNumber bool) *ComparisonSet

func (*ComparisonSet) Add added in v0.0.2

func (c *ComparisonSet) Add(v any)

func (*ComparisonSet) Has added in v0.0.2

func (c *ComparisonSet) Has(v any) bool

func (*ComparisonSet) Normalize added in v0.0.2

func (c *ComparisonSet) Normalize(v reflect.Value) (reflect.Kind, any)

type MapCollection

type MapCollection[T ~map[K]V, K comparable, V any] struct {
	// contains filtered or unexported fields
}

func UseMap

func UseMap[T ~map[K]V, K comparable, V any](items T) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) All

func (m *MapCollection[T, K, V]) All() T

func (*MapCollection[T, K, V]) DiffKeys

func (m *MapCollection[T, K, V]) DiffKeys(target T) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) Empty

func (m *MapCollection[T, K, V]) Empty() bool

func (*MapCollection[T, K, V]) Except

func (m *MapCollection[T, K, V]) Except(keys ...K) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) Get

func (m *MapCollection[T, K, V]) Get(key K) (value V, _ bool)

func (*MapCollection[T, K, V]) Has

func (m *MapCollection[T, K, V]) Has(key K) bool

func (*MapCollection[T, K, V]) Keys

func (m *MapCollection[T, K, V]) Keys() []K

func (*MapCollection[T, K, V]) Len

func (m *MapCollection[T, K, V]) Len() int

func (*MapCollection[T, K, V]) Merge

func (m *MapCollection[T, K, V]) Merge(targets ...T) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) New

func (m *MapCollection[T, K, V]) New(items T) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) Only

func (m *MapCollection[T, K, V]) Only(keys ...K) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) Print

func (m *MapCollection[T, K, V]) Print() *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) Pull added in v0.0.2

func (m *MapCollection[T, K, V]) Pull(key K) (V, bool)

func (*MapCollection[T, K, V]) Put added in v0.0.2

func (m *MapCollection[T, K, V]) Put(key K, value V) *MapCollection[T, K, V]

func (*MapCollection[T, K, V]) Same

func (m *MapCollection[T, K, V]) Same(target T) bool

func (*MapCollection[T, K, V]) Union

func (m *MapCollection[T, K, V]) Union(target T) *MapCollection[T, K, V]

type NumberCollection

type NumberCollection[T ~[]E, E constraints.Integer | constraints.Float] struct {
	*SliceCollection[T, E]
}

func NumberFrom

func NumberFrom[N constraints.Integer | constraints.Float, T ~[]E, E any](c *SliceCollection[T, E]) *NumberCollection[[]N, N]

func UseNumber

func UseNumber[T ~[]E, E constraints.Integer | constraints.Float](items T) *NumberCollection[T, E]

func (*NumberCollection[T, E]) Avg

func (n *NumberCollection[T, E]) Avg() float64

func (*NumberCollection[T, E]) Max

func (n *NumberCollection[T, E]) Max() E

func (*NumberCollection[T, E]) Median

func (n *NumberCollection[T, E]) Median() float64

func (*NumberCollection[T, E]) Min

func (n *NumberCollection[T, E]) Min() E

func (*NumberCollection[T, E]) Sort

func (n *NumberCollection[T, E]) Sort() *NumberCollection[T, E]

func (*NumberCollection[T, E]) SortDesc

func (n *NumberCollection[T, E]) SortDesc() *NumberCollection[T, E]

func (*NumberCollection[T, E]) Sum

func (n *NumberCollection[T, E]) Sum() (total E)

type SliceCollection

type SliceCollection[T ~[]E, E any] struct {
	// contains filtered or unexported fields
}

func SortBy

func SortBy[T ~[]E, E any, C func(item E, index int) R, R constraints.Ordered](items T, callback C) *SliceCollection[T, E]

func SortByDesc

func SortByDesc[T ~[]E, E any, C func(item E, index int) R, R constraints.Ordered](items T, callback C) *SliceCollection[T, E]

func Times

func Times[T []E, E any](number int, callback func(number int) E) *SliceCollection[T, E]

func UseSlice

func UseSlice[T ~[]E, E any](items T) *SliceCollection[T, E]

func (*SliceCollection[T, E]) All

func (s *SliceCollection[T, E]) All() T

func (*SliceCollection[T, E]) Contains

func (s *SliceCollection[T, E]) Contains(value E) bool

func (*SliceCollection[T, E]) Diff

func (s *SliceCollection[T, E]) Diff(target T) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Duplicates added in v0.0.2

func (s *SliceCollection[T, E]) Duplicates() *MapCollection[map[int]E, int, E]

func (*SliceCollection[T, E]) Each

func (s *SliceCollection[T, E]) Each(callback func(value E, index int)) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Empty

func (s *SliceCollection[T, E]) Empty() bool

func (*SliceCollection[T, E]) Filter

func (s *SliceCollection[T, E]) Filter(callback func(value E, index int) bool) *SliceCollection[T, E]

func (*SliceCollection[T, E]) First

func (s *SliceCollection[T, E]) First() (E, bool)

func (*SliceCollection[T, E]) Index

func (s *SliceCollection[T, E]) Index(value E) int

func (*SliceCollection[T, E]) Last

func (s *SliceCollection[T, E]) Last() (E, bool)

func (*SliceCollection[T, E]) Len

func (s *SliceCollection[T, E]) Len() int

func (*SliceCollection[T, E]) Map

func (s *SliceCollection[T, E]) Map(callback func(value E, index int) E) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Merge

func (s *SliceCollection[T, E]) Merge(targets ...T) *SliceCollection[T, E]

func (*SliceCollection[T, E]) New

func (s *SliceCollection[T, E]) New(items T) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Pop added in v0.0.2

func (s *SliceCollection[T, E]) Pop() (E, bool)

func (*SliceCollection[T, E]) Print

func (s *SliceCollection[T, E]) Print() *SliceCollection[T, E]

func (*SliceCollection[T, E]) Push added in v0.0.2

func (s *SliceCollection[T, E]) Push(item E) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Random

func (s *SliceCollection[T, E]) Random() (E, bool)

func (*SliceCollection[T, E]) Reduce

func (s *SliceCollection[T, E]) Reduce(initial E, callback func(carry E, value E, key int) E) E

func (*SliceCollection[T, E]) Reverse

func (s *SliceCollection[T, E]) Reverse() *SliceCollection[T, E]

func (*SliceCollection[T, E]) Same

func (s *SliceCollection[T, E]) Same(target T) bool

func (*SliceCollection[T, E]) Shuffle

func (s *SliceCollection[T, E]) Shuffle() *SliceCollection[T, E]

func (*SliceCollection[T, E]) Slice

func (s *SliceCollection[T, E]) Slice(offset int, length ...int) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Splice

func (s *SliceCollection[T, E]) Splice(offset int, args ...any) *SliceCollection[T, E]

func (*SliceCollection[T, E]) Split

func (s *SliceCollection[T, E]) Split(amount int) []T

func (*SliceCollection[T, E]) Unique

func (s *SliceCollection[T, E]) Unique() *SliceCollection[T, E]

func (*SliceCollection[T, E]) Where added in v0.0.2

func (s *SliceCollection[T, E]) Where(args ...any) *SliceCollection[T, E]

func (*SliceCollection[T, E]) WhereIn added in v0.0.2

func (s *SliceCollection[T, E]) WhereIn(args ...any) *SliceCollection[T, E]

func (*SliceCollection[T, E]) WhereNotIn added in v0.0.2

func (s *SliceCollection[T, E]) WhereNotIn(args ...any) *SliceCollection[T, E]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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