Documentation
¶
Overview ¶
Package cont functions for multiple containers.
Index ¶
- Variables
- func All[S ~[]E, E any](items S, checkers ...types.Checker[E]) bool
- func Any[S ~[]E, E any](items S, checkers ...types.Checker[E]) bool
- func Contains[S ~[]E, E any](equal types.Equaler[E], items S, item E) bool
- func Difference[S ~[]E, E comparable](src S, items ...S) iter.Seq[E]
- func DifferenceFunc[S ~[]E, E any](equal types.Equaler[E], src S, items ...S) iter.Seq[E]
- func Fill[S ~[]E, E any](slice S, val E, startAndEnd ...int)
- func Filter[S ~[]T, T any](items S, checker types.Checker[T]) iter.Seq[T]
- func Intersection[S ~[]E, E comparable](items ...S) iter.Seq[E]
- func IntersectionFunc[S ~[]E, E any](equal types.Equaler[E], items ...S) iter.Seq[E]
- func LastIndex[S ~[]E, E comparable](items S, item E) int
- func LastIndexFunc[S ~[]E, E any](items S, checker types.Checker[E]) int
- func Max[T cmp.Ordered](items []T) T
- func Mean[S ~[]E, E types.Number](items S) E
- func Min[S ~[]E, E cmp.Ordered](items S) E
- func None[S ~[]T, T any](items S, checkers ...types.Checker[T]) bool
- func Reduce[S ~[]E, E any](items S, reducer types.Reducer[E]) E
- func Sum[S ~[]E, E cmp.Ordered](items S) E
- func Union[S ~[]E, E comparable](items ...S) iter.Seq[E]
- func UnionFunc[S ~[]E, E any](equal types.Equaler[E], items ...S) iter.Seq[E]
- type Set
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var StructNone = struct{}{}
nolint: gochecknoglobals
Functions ¶
func All ¶
All returns true if all elements in the slice satisfy every provided checker function. It returns false if any element fails to satisfy any of the checkers.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
equals3 := func(num int) bool { return num == 3 }
fmt.Println(cont.All([]int{3, 3, 3, 3}, equals3))
fmt.Println(cont.All([]int{3, 3, 1, 3}, equals3))
}
Output: true false
func Any ¶
Any returns true if any element in the slice satisfies at least one of the provided checker functions. It returns false if no elements satisfy any of the checkers.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
greaterThan0 := func(num int) bool { return 0 > num }
greaterThan2 := func(num int) bool { return 2 > num }
fmt.Println(cont.Any([]int{1, 2}, greaterThan0))
fmt.Println(cont.Any([]int{1, 2}, greaterThan2))
}
Output: false true
func Contains ¶ added in v1.0.2
Contains checks if a slice contains an element based on a custom equality function. It returns true if the specified item is found in the slice.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
equal := func(a, b int) bool { return a == b }
fmt.Println(cont.Contains(equal, []int{}, 2))
fmt.Println(cont.Contains(equal, []int{1, 2, 3}, 2))
fmt.Println(cont.Contains(equal, []int{1, 2, 3}, 4))
}
Output: false true false
func Difference ¶ added in v1.0.2
func Difference[S ~[]E, E comparable](src S, items ...S) iter.Seq[E]
Difference returns a sequence that src slice which are not present in any of the provided slices.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(slices.Collect(cont.Difference([]int{1, 2})))
fmt.Println(slices.Collect(cont.Difference([]int{1, 2}, []int{2, 3})))
for num := range cont.Difference([]int{1, 2}, []int{2, 3}) {
fmt.Println(num)
break
}
}
Output: [1 2] [1] 1
func DifferenceFunc ¶ added in v1.0.2
DifferenceFunc returns a sequence that src slice which are not present in any of the provided slices.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/xuender/helper/cont"
)
func main() {
equal := func(a, b int) bool { return a == b }
fmt.Println(slices.Collect(cont.DifferenceFunc(equal, []int{1, 2})))
fmt.Println(slices.Collect(cont.DifferenceFunc(equal, []int{1, 2}, []int{2, 3})))
for num := range cont.DifferenceFunc(equal, []int{1, 2}, []int{2, 3}) {
fmt.Println(num)
break
}
}
Output: [1 2] [1] 1
func Fill ¶ added in v1.0.2
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
items := []int{}
cont.Fill(items, 0, 8)
fmt.Println(items)
items = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
cont.Fill(items, 0, 8)
fmt.Println(items)
cont.Fill(items, 0, 3, 5)
fmt.Println(items)
cont.Fill(items, 0)
fmt.Println(items)
}
Output: [] [1 2 3 4 5 6 7 8 0 0] [1 2 3 0 0 6 7 8 0 0] [0 0 0 0 0 0 0 0 0 0]
func Filter ¶
Filter filters elements in a slice based on a provided checker function. It returns a new slice containing only the elements that satisfy the checker.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
items := cont.Filter([]int{1, 2, 3, 4}, func(num int) bool { return num%2 == 0 })
for num := range items {
fmt.Println(num)
break
}
}
Output: 2
func Intersection ¶
func Intersection[S ~[]E, E comparable](items ...S) iter.Seq[E]
Intersection returns a sequence that contains the intersection of multiple slices. It yields elements that are present in all provided slices, ensuring each element appears only once.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(slices.Collect(cont.Intersection[[]int]()))
fmt.Println(slices.Collect(cont.Intersection([]int{1, 2, 3})))
fmt.Println(slices.Collect(cont.Intersection([]int{1, 2, 2, 3}, []int{2, 3, 4})))
for num := range cont.Intersection([]int{1, 1, 2, 3}, []int{2, 3, 4}) {
fmt.Println(num)
break
}
}
Output: [] [1 2 3] [2 3] 2
func IntersectionFunc ¶ added in v1.0.2
IntersectionFunc returns a sequence that contains the intersection of multiple slices using equality function.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/xuender/helper/cont"
)
func main() {
equal := func(a, b int) bool { return a == b }
fmt.Println(slices.Collect(cont.IntersectionFunc[[]int](equal)))
fmt.Println(slices.Collect(cont.IntersectionFunc(equal, []int{1, 2, 3})))
fmt.Println(slices.Collect(cont.IntersectionFunc(equal, []int{1, 2, 2, 3}, []int{2, 3, 4})))
for num := range cont.IntersectionFunc(equal, []int{1, 1, 2, 3}, []int{2, 3, 4}) {
fmt.Println(num)
break
}
}
Output: [] [1 2 3] [2 3] 2
func LastIndex ¶
func LastIndex[S ~[]E, E comparable](items S, item E) int
LastIndex returns the last index of an item in the slice. If the item is not found, it returns -1. It searches from the end of the slice to the beginning.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(cont.LastIndex([]int{1, 2, 3}, 3))
fmt.Println(cont.LastIndex([]int{1, 2, 3}, 2))
fmt.Println(cont.LastIndex([]int{1, 2, 3}, 1))
fmt.Println(cont.LastIndex([]int{1, 2, 3}, 4))
}
Output: 2 1 0 -1
func LastIndexFunc ¶
LastIndexFunc returns the last index of an item that satisfies the checker function in the slice. If no item satisfies the checker, it returns -1. It searches from the end of the slice to the beginning.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(cont.LastIndexFunc([]int{1, 2, 3}, func(num int) bool { return num == 2 }))
fmt.Println(cont.LastIndexFunc([]int{1, 2, 3}, func(num int) bool { return num > 10 }))
}
Output: 1 -1
func Max ¶
Max returns the maximum value from a slice of ordered elements. If the slice is empty, it returns the zero value of the element type.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(cont.Max[int](nil))
fmt.Println(cont.Max([]int{}))
fmt.Println(cont.Max([]int{1}))
fmt.Println(cont.Max([]int{2, 3}))
}
Output: 0 0 1 3
func Mean ¶
Mean calculates the mean of the elements in the slice.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(cont.Mean([]int{1, 2, 3}))
fmt.Println(cont.Mean([]int{}))
}
Output: 2 0
func Min ¶
Min returns the minimum value from a slice of ordered elements. If the slice is empty, it returns the zero value of the element type.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(cont.Min([]int{}))
fmt.Println(cont.Min([]int{1}))
fmt.Println(cont.Min([]int{3, 2}))
}
Output: 0 1 2
func None ¶
None checks if no elements in the slice satisfy the provided checker function. It returns true if all elements fail the check, otherwise false.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
equals3 := func(num int) bool { return num == 3 }
fmt.Println(cont.None([]int{1, 2, 4, 5}, equals3))
fmt.Println(cont.None([]int{3, 3, 1, 3}, equals3))
}
Output: true false
func Reduce ¶
Reduce applies a reducer function to all elements of a slice and returns a single value. It iterates over the slice, applying the reducer to accumulate a result. The first element initializes the accumulator.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
sum := cont.Reduce([]int{1, 2, 3, 4}, func(val1, val2 int) int {
return val1 + val2
})
fmt.Println(sum)
}
Output: 10
func Sum ¶
Sum calculates the sum of elements in a slice.
Example ¶
package main
import (
"fmt"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(cont.Sum([]int{1, 2, 3, 4}))
}
Output: 10
func Union ¶
func Union[S ~[]E, E comparable](items ...S) iter.Seq[E]
Union returns a sequence that contains the union of multiple slices. It eliminates duplicate elements using a set, ensuring each element appears only once.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/xuender/helper/cont"
)
func main() {
fmt.Println(slices.Collect(cont.Union[[]int]()))
fmt.Println(slices.Collect(cont.Union([]int{1, 1})))
fmt.Println(slices.Collect(cont.Union([]int{1, 2}, []int{2, 3})))
for num := range cont.Union([]int{1, 2}, []int{2, 3}) {
fmt.Println(num)
break
}
}
Output: [] [1] [1 2 3] 1
func UnionFunc ¶ added in v1.0.2
UnionFunc returns a sequence that contains the union of multiple slices using a custom equality function.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/xuender/helper/cont"
)
func main() {
equal := func(a, b int) bool { return a == b }
fmt.Println(slices.Collect(cont.UnionFunc[[]int](equal)))
fmt.Println(slices.Collect(cont.UnionFunc(equal, []int{1, 1})))
fmt.Println(slices.Collect(cont.UnionFunc(equal, []int{1, 2}, []int{2, 3})))
for num := range cont.UnionFunc(equal, []int{1, 2}, []int{2, 3}) {
fmt.Println(num)
break
}
}
Output: [] [1] [1 2 3] 1
Types ¶
type Set ¶
type Set[V comparable] map[V]struct{}
Set represents a collection of unique elements. It provides methods to add elements, check for existence, and convert to slices or sequences.
func NewSet ¶
func NewSet[V comparable](elems ...V) Set[V]
NewSet creates a new Set with the provided elements.
Example ¶
package main
import (
"fmt"
"sort"
"github.com/xuender/helper/cont"
)
func main() {
nums := cont.NewSet(1, 2, 3)
fmt.Println(len(nums))
fmt.Println(len(nums.Add(3, 4, 5)))
fmt.Println(nums.Has(0))
fmt.Println(nums.Has(3))
delete(nums, 2)
ints := nums.Slice()
sort.Ints(ints)
fmt.Println(ints)
}
Output: 3 5 false true [1 3 4 5]