cont

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cont functions for multiple containers.

Index

Examples

Constants

This section is empty.

Variables

View Source
var StructNone = struct{}{}

nolint: gochecknoglobals

Functions

func All

func All[S ~[]E, E any](items S, checkers ...types.Checker[E]) bool

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

func Any[S ~[]E, E any](items S, checkers ...types.Checker[E]) bool

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

func Contains[S ~[]E, E any](equal types.Equaler[E], items S, item E) bool

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

func DifferenceFunc[S ~[]E, E any](equal types.Equaler[E], src S, items ...S) iter.Seq[E]

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

func Fill[S ~[]E, E any](slice S, val E, startAndEnd ...int)
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

func Filter[S ~[]T, T any](items S, checker types.Checker[T]) iter.Seq[T]

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

func IntersectionFunc[S ~[]E, E any](equal types.Equaler[E], items ...S) iter.Seq[E]

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

func LastIndexFunc[S ~[]E, E any](items S, checker types.Checker[E]) int

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

func Max[T cmp.Ordered](items []T) T

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

func Mean[S ~[]E, E types.Number](items S) E

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

func Min[S ~[]E, E cmp.Ordered](items S) E

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

func None[S ~[]T, T any](items S, checkers ...types.Checker[T]) bool

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

func Reduce[S ~[]E, E any](items S, reducer types.Reducer[E]) E

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

func Sum[S ~[]E, E cmp.Ordered](items S) E

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

func UnionFunc[S ~[]E, E any](equal types.Equaler[E], items ...S) iter.Seq[E]

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]

func (Set[V]) Add

func (p Set[V]) Add(elems ...V) Set[V]

Add adds the specified elements to the set.

func (Set[V]) AddSet

func (p Set[V]) AddSet(set ...Set[V]) Set[V]

AddSet adds all elements from the provided sets to the set.

func (Set[V]) Has

func (p Set[V]) Has(elem V) bool

Has checks if the set contains the specified element.

func (Set[V]) Slice

func (p Set[V]) Slice() []V

Slice returns a slice of all elements in the set.

func (Set[V]) Values

func (p Set[V]) Values() iter.Seq[V]

Values returns an iterator sequence of all elements in the set.

Jump to

Keyboard shortcuts

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