Documentation
¶
Index ¶
- func Contains[T, E any](list []T, item E, compare func(T, E) bool) bool
- func ContainsComparable[T comparable](list []T, item T) bool
- func ContainsSlice[T, E any](list []T, subset []E, compare func(T, E) bool) bool
- func FailFastMap[T, U any](input []T, mapperFunc func(T) (U, error)) ([]U, error)
- func Filter[T any](input []T, check func(T) bool) []T
- func FindAll[T any](list []T, check func(T) bool) (elements []T)
- func FindMany[T any](list []T, check func(T) bool, number int) (elements []T)
- func FindOne[T any](list []T, check func(T) bool) (T, bool)
- func IndexOf[T any](list []T, check func(item T) bool) int
- func IndexesOf[T any](list []T, check func(item T) bool) []int
- func InsertOneBefore[T any](before T, input []T) []T
- func LastIndexOf[T any](list []T, check func(item T) bool) int
- func Map[T, U any](input []T, mapperFunc func(T) U) []U
- func MapDistinct[T any, U any](input []T, mapperFunc func(T) U, comparatorFunc func(U, U) bool) (output []U)
- func Split[T Splittable[U, V], U, V any](input []T) ([]U, []V)
- func Without[S ~[]E, E comparable](a S, b S) S
- type Splittable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
Contains checks if an item exists in a slice based on a custom comparison function.
Parameters:
- list: The slice of items to search within.
- item: The value to search for in the slice.
- compare: A comparison function that takes an item of type T and an item of type E and returns true if they are considered equal, false otherwise.
Returns:
- true if the item is found in the slice according to the comparison function, false otherwise.
Example:
items := []string{"apple", "banana", "cherry"}
exists := Contains("BANANA", items, func(a, b string) bool {
return strings.ToLower(a) == strings.ToLower(b)
}) // true
func ContainsComparable ¶
func ContainsComparable[T comparable](list []T, item T) bool
ContainsComparable checks if a comparable item exists in a slice.
Parameters:
- list: The slice of comparable items to search within.
- item: The value to search for in the slice.
Returns:
- true if the item is found in the slice, false otherwise.
Example:
items := []int{1, 2, 3}
exists := ContainsComparable(2, items) // true
func ContainsSlice ¶
ContainsSlice checks if all elements of one slice (`subset`) are present in another slice (`list`), based on a custom comparison function.
Parameters:
- list: The slice of items to search within.
- subset: The slice of type T containing elements to check for in the `list`.
- compare: A comparison function that takes an item of type T and an item of type E and returns true if they are considered equal, false otherwise.
Returns:
- true if all elements in `subset` are present in `list` according to the comparison function, false otherwise.
Example:
numbers := []int{1, 2, 3, 4, 5}
subset := []int{2, 3}
isSubset := ContainsSlice(numbers, subset, func(a, b int) bool {
return a == b
}) // true
strings := []string{"apple", "banana", "cherry"}
toCheck := []string{"banana", "cherry"}
isAllPresent := ContainsSlice(strings, toCheck, func(a, b string) bool {
return a == b
}) // true
missingItems := []string{"banana", "pear"}
isAllPresent = ContainsSlice(strings, missingItems, func(a, b string) bool {
return a == b
}) // false
func FailFastMap ¶ added in v0.0.5
FailFastMap transforms a slice of type T into a slice of type U by applying a mapping function to each element. The mapping function may return an error. In this cast, FailFastMap will stop and return the error.
Parameters:
- input: The slice of type T to be transformed.
- mapperFunc: A function that takes an element of type T and returns an element of type U and an error.
Returns:
- A new slice of type U where each element is the result of applying `mapperFunc` to the corresponding element of `input []T`, or the first error returned by the `mapperFunc`.
Example:
numbers := []int{4, -1}
roots, err := Map(numbers, func(n int) (int, error) {
if n < 0 {
return errors.New("cannot compute the square root of a negative number")
}
return math.Sqrt(n)
}) // nil, error ('cannot compute the square root of a negative number')
numbers := []float64{4, 9, 16}
roots, err := FailFastMap(numbers, func(n float64) (float64, error) {
if n < 0 {
return n, errors.New("cannot compute the square root of a negative number")
}
return math.Sqrt(n), nil
}) // []float64{2, 3, 4}, nil
func Filter ¶
Filter returns a new slice containing only the elements of the input slice that satisfy the check function.
Parameters:
- input: The slice of type T to filter.
- check: A function that takes an element of type T and returns true if the element should be included in the output slice.
Returns:
- A new slice containing only the elements of `input []T` for which `check` returns true.
Example:
numbers := []int{1, 2, 3, 4, 5}
evens := Filter(numbers, func(n int) bool { return n%2 == 0 }) // []int{2, 4}
strings := []string{"apple", "banana", "cherry"}
startsWithB := Filter(strings, func(s string) bool { return strings.HasPrefix(s, "b") }) // []string{"banana"}
func FindAll ¶
FindAll finds and returns all elements in the slice that satisfy the provided check function.
Parameters:
- list: The slice of type T to search through.
- check: A function that takes an element of type T and returns true if the element satisfies the condition.
Returns:
- A slice containing all elements that satisfy the condition.
Example:
numbers := []int{1, 2, 3, 4, 5}
allGreaterThanTwo := FindAll(numbers, func(n int) bool { return n > 2 }) // []int{3, 4, 5}
strings := []string{"apple", "banana", "cherry", "blueberry"}
allStartingWithB := FindAll(strings, func(s string) bool { return strings.HasPrefix(s, "b") }) // []string{"banana", "blueberry"}
func FindMany ¶
FindMany finds and returns up to a specified number of elements in the slice that satisfy the provided check function.
Parameters:
- list: The slice of type T to search through.
- check: A function that takes an element of type T and returns true if the element satisfies the condition.
- number: The maximum number of elements to return.
Returns:
- A slice containing up to `number` elements that satisfy the condition.
Example:
numbers := []int{1, 2, 3, 4, 5}
firstTwo := FindMany(numbers, func(n int) bool { return n > 2 }, 2) // []int{3, 4}
strings := []string{"apple", "banana", "cherry", "blueberry"}
firstOne := FindMany(strings, func(s string) bool { return strings.HasPrefix(s, "b") }, 1) // []string{"banana"}
func FindOne ¶
FindOne searches a slice for the first element that satisfies a given check function.
Parameters:
- list: The slice of items to search within.
- check: A check function that takes an item of type T and returns true if the item satisfies the condition.
Returns:
- A value containing the first element that satisfies the check function, and a boolean value. True if a match is found, false otherwise
Example:
numbers := []int{1, 2, 3, 4, 5}
firstEven, ok := FindOne(numbers, func(n int) bool { return n%2 == 0 }) // 2, true
strings := []string{"apple", "banana", "cherry"}
firstBanana, ok := FindOne(strings, func(s string) bool { return s == "banana" }) // "banana", true
noMatch, ok := FindOne(numbers, func(n int) bool { return n > 10 }) // 0, false
func IndexOf ¶
IndexOf returns the index of the first element in the slice that satisfies the provided check function.
Parameters:
- list: The slice of type T to search through.
- check: A function that takes an element of type T and returns true if the element satisfies the condition.
Returns:
- The index of the first matching element if found.
- -1 if no element satisfies the condition.
Example:
numbers := []int{1, 2, 3, 4, 5}
index := IndexOf(numbers, func(n int) bool { return n > 3 }) // 3
strings := []string{"apple", "banana", "cherry"}
index := IndexOf(strings, func(s string) bool { return s == "banana" }) // 1
func IndexesOf ¶
IndexesOf returns the indexes of all elements in the slice that satisfy the provided check function.
Parameters:
- list: The slice of type T to search through.
- check: A function that takes an element of type T and returns true if the element satisfies the condition.
Returns:
- A slice of integers representing the indexes of all matching elements.
- An empty slice if no elements satisfy the condition.
Example:
numbers := []int{1, 2, 3, 4, 5, 4}
indexes := IndexesOf(numbers, func(n int) bool { return n == 4 }) // []int{3, 5}
strings := []string{"apple", "banana", "cherry", "banana"}
indexes := IndexesOf(strings, func(s string) bool { return s == "banana" }) // []int{1, 3}
func InsertOneBefore ¶
func InsertOneBefore[T any](before T, input []T) []T
InsertOneBefore inserts a single element at the beginning of a slice.
Parameters:
- before: The element to insert at the beginning of the slice.
- input: The original slice where the element will be inserted.
Returns:
- A new slice with the `before` element added at the start, followed by all elements of the original slice.
Example:
original := []int{2, 3, 4}
withOne := InsertOneBefore(1, original) // []int{1, 2, 3, 4}
strings := []string{"banana", "cherry"}
withApple := InsertOneBefore("apple", strings) // []string{"apple", "banana", "cherry"}
func LastIndexOf ¶
LastIndexOf returns the index of the last element in the slice that satisfies the provided check function.
Parameters:
- list: The slice of type T to search through.
- check: A function that takes an element of type T and returns true if the element satisfies the condition.
Returns:
- The index of the last matching element if found.
- -1 if no element satisfies the condition.
Example:
numbers := []int{1, 2, 3, 4, 5, 4}
index := LastIndexOf(numbers, func(n int) bool { return n == 4 }) // 5
strings := []string{"apple", "banana", "cherry", "banana"}
index := LastIndexOf(strings, func(s string) bool { return s == "banana" }) // 3
func Map ¶
func Map[T, U any](input []T, mapperFunc func(T) U) []U
Map transforms a slice of type T into a slice of type U by applying a mapping function to each element.
Parameters:
- input: The slice of type T to be transformed.
- mapperFunc: A function that takes an element of type T and returns an element of type U.
Returns:
- A new slice of type U where each element is the result of applying `mapperFunc` to the corresponding element of `input []T`.
Example:
numbers := []int{1, 2, 3}
squared := Map(numbers, func(n int) int { return n * n }) // []int{1, 4, 9}
strings := []string{"apple", "banana", "cherry"}
lengths := Map(strings, func(s string) int { return len(s) }) // []int{5, 6, 6}
func MapDistinct ¶
func MapDistinct[T any, U any](input []T, mapperFunc func(T) U, comparatorFunc func(U, U) bool) (output []U)
MapDistinct applies a mapping function to each element in the input slice, ensuring that the output slice contains only distinct elements based on a custom comparator function.
Parameters:
- input: The slice of type T to transform.
- mapperFunc: A function that maps an element of type T to a new element of type U.
- comparatorFunc: A function that compares two elements of type U and returns true if they are considered equal. Calls the Contains function
Returns:
- A slice of type U containing distinct elements based on the comparator function.
Example 1:
type Person struct {
Name string
Age int
}
input := []Person{
{"Alice", 25},
{"Bob", 30},
{"Alice", 30},
}
names := MapDistinct(input, func(p Person) string {
return p.Name
}, func(a, b string) bool {
return a == b
}) // []string{"Alice", "Bob"}
Example:
numbers := []int{1, 2, 2, 3, 4, 3}
distinctSquares := MapDistinct(numbers, func(n int) int {
return n * n
}, func(a, b int) bool {
return a == b
}) // []int{1, 4, 9, 16}
func Split ¶
func Split[T Splittable[U, V], U, V any](input []T) ([]U, []V)
Split takes a slice of elements that implement the Splittable interface and splits them into two separate slices.
Parameters:
- input: A slice of elements of type T, where T implements Splittable[U, V].
Returns:
- Two slices:
- The first slice contains all elements of type U extracted from the input.
- The second slice contains all elements of type V extracted from the input.
Example:
type Item struct {
Key string
Value int
}
type Split_U struct {
Key string
}
type Split_V struct {
Value int
}
func (p Item) Split() (Split_U, Split_V) {
return Split_U{Key: p.Key}, Split_V{Value: p.Value}
}
items := []Item{
{"apple", 5},
{"banana", 3},
{"cherry", 7},
}
keys, values := Split(items) // keys: []string{"apple", "banana", "cherry"}, values: []int{5, 3, 7}
func Without ¶ added in v0.0.4
func Without[S ~[]E, E comparable](a S, b S) S
Without returns a new slice containing the elements of one input slice that are not contained in a second slice.
Parameters:
- a: The base slice.
- b: The slice of elements to not include.
Returns:
- A new slice containing the elements of `a` that are not in `b`.
Example:
numbers := []int{1, 2, 3, 4, 5}
bad := []int{7, 3}
good := Without(numbers, bad) // []int{1, 2, 4, 5}
Types ¶
type Splittable ¶
type Splittable[U, V any] interface { Split() (U, V) }
Splittable is an interface representing a type that can be split into two components of types U and V.