Documentation ¶
Overview ¶
Package generic implements some utility functions which work on any type.
Keep in mind though, that this convenience has a non-negligible runtime penalty, so you shouldn't use the functions in this package for CPU intensive operations nor working with big datasets.
Index ¶
- func Avg(in interface{}) interface{}
- func By(iterable interface{}, field string) interface{}
- func Contains(iterable interface{}, val interface{}) bool
- func Filter(in interface{}, f interface{}) interface{}
- func GroupsBy(iterable interface{}, field string) interface{}
- func Keys(m interface{}) interface{}
- func Map(in interface{}, f interface{}) interface{}
- func Max(in interface{}) interface{}
- func Min(in interface{}) interface{}
- func Reduce(in interface{}, start interface{}, f interface{}) interface{}
- func Remove(slicePtr interface{}, val interface{}) bool
- func Select(sl interface{}, key string) interface{}
- func Sort(data interface{}, key string)
- func SortFunc(data interface{}, less interface{})
- func Sum(in interface{}) interface{}
- func Values(m interface{}) interface{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Avg ¶
func Avg(in interface{}) interface{}
Avg returns the average of the elements from the in argument, which must be a slice or array of a summable and divisible type. Summable types include:
- basic types with a defined + operator and a defined / operator
If the element type is not summable and divisible, Avg will panic. Given a slice of type []T, the returned value will be of type T.
func By ¶
func By(iterable interface{}, field string) interface{}
By returns a map with the elements of the given iterable, which must be a slice of structs or pointers to structs, where the key is the field named by the field argument. If there are multiple elements with the same field value, only the last one will be in the returned map. If you want multiple elements to be returned use GroupsBy. Given a slice of type []T with a field of type int this function will return a map[int]T. If the arguments are not valid, this function will panic.
func Contains ¶
func Contains(iterable interface{}, val interface{}) bool
Contains returns wheter the given slice or array contains the given value. Given a slice of type []T, val must be of type T. Otherwise, this function will panic.
func Filter ¶
func Filter(in interface{}, f interface{}) interface{}
Filter applies the predicate function f over the elements in the first argument, returning a new slice of the same type with the elements for which f returns true. Given a slice of type T, f must be of the form:
func(T) bool
If in is not a slice or array or f has not the right type, Filter will panic.
Example (Even) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []int{1, 2, 3, 4} out := generic.Filter(in, func(a int) bool { return a%2 == 0 }).([]int) fmt.Println(out) }
Output: [2 4]
func GroupsBy ¶
func GroupsBy(iterable interface{}, field string) interface{}
GroupsBy returns a map with slices of elements from the given iterable, which must be a slice of structs or pointers to structs, where the key is the field named by the field argument. Map values will be slices of the elements with the same key. If you only want an element per key, use By instead. Given a slice of type []T with a field of type int this function will return a map[int][]T. If the arguments are not valid, this function will panic.
func Keys ¶
func Keys(m interface{}) interface{}
Keys returns the keys of the map m as a slice. Given a map m of type map[T1]T2 the return value will be of type []T1. If m is not a map, Keys will panic.
func Map ¶
func Map(in interface{}, f interface{}) interface{}
Map returns a new slice of the same length as the first argument, where the elements are the result of applying f to each element in the input. The returned slice has the element type that f returns. Given a slice of type T1, f must be of the form:
func(T1) T2 // note that T2 can be equal to T1
The returned slice will be of type []T2.
If in is not a slice or array or f has not the right type, Map will panic.
Example (Add_float) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []int{1, 2, 3, 4} out := generic.Map(in, func(a int) float64 { return float64(a) + 0.1 }).([]float64) fmt.Println(out) }
Output: [1.1 2.1 3.1 4.1]
Example (Extract_type) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []interface{}{1, 2, 3, 4} out := generic.Map(in, func(a interface{}) int { return a.(int) }).([]int) fmt.Println(out) }
Output: [1 2 3 4]
Example (Square) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []int{1, 2, 3, 4} out := generic.Map(in, func(a int) int { return a * a }).([]int) fmt.Println(out) }
Output: [1 4 9 16]
func Max ¶
func Max(in interface{}) interface{}
Max returns the biggest element from the in argument, which must be a slice or array of a comparable type. Comparable types include:
- basic types with a defined < operator
- slices (their length is compared)
If the element type is not comparable, Max will panic. Given a slice of type []T, the returned value will be of type T.
func Min ¶
func Min(in interface{}) interface{}
Min returns the smallest element from the in argument, which must be a slice or array of a comparable type. Comparable types include:
- basic types with a defined < operator
- slices (their length is compared)
If the element type is not comparable, Min will panic. Given a slice of type []T, the returned value will be of type T.
func Reduce ¶
func Reduce(in interface{}, start interface{}, f interface{}) interface{}
Reduce applies f cumulatively to the elements in the first argument. The second argument is used as the initial value and should usually be the neutral element for the binary operation represented by f (e.g. 0 for addition, 1 for multiplication, etc...). Given a slice of type []T, the following conditions must be satisfied:
f -> func(T, T) T start -> assignable to T
If in is not an array or slice or the previous conditions are not satisfied, Reduce will panic.
Example (Concat) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []string{"a", "b", "c"} out := generic.Reduce(in, "", func(a, b string) string { return a + b }).(string) fmt.Println(out) }
Output: abc
Example (Mult) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []float64{-1, 3, 4} out := generic.Reduce(in, 1, func(a, b float64) float64 { return a * b }).(float64) fmt.Println(out) }
Output: -12
Example (Sum) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []int{1, 2, 3, 4} out := generic.Reduce(in, 0, func(a, b int) int { return a + b }).(int) fmt.Println(out) }
Output: 10
func Remove ¶
func Remove(slicePtr interface{}, val interface{}) bool
Remove removes the first occurence of the given val. It returns true if an element was removed, false otherwise. Note that the first argument must a pointer to a slice, while the second one must be a valid element of the slice. Otherwise, this function will panic.
func Select ¶
func Select(sl interface{}, key string) interface{}
Select returns a new slice with the selected key extracted from the sl argument. The key must be either a field name or a method name with no arguments and just one return value. The returned value is a slice of the same type of the selected field. e.g.
type Person struct { Name string } ... var persons []*Person = ... names := Select(persons, "Name").([]string)
This function is around 2 times slower than the specific code for extracting the field.
func Sort ¶
func Sort(data interface{}, key string)
Sort sorts an array or slice of structs or pointer to structs by comparing the given key, which must be a an exported struct field or an exported method with no arguments and just one return value. If the key is prefixed by the character '-', the sorting is performed in descending order. If there are any errors, Sort panics since they can't be anything but programming errors.
func SortFunc ¶
func SortFunc(data interface{}, less interface{})
SortFunc shorts the given slice or array using the provided less function. The function must accept two arguments of the same type of the slice element and must return just one bool argument.
func Sum ¶
func Sum(in interface{}) interface{}
Sum returns the sum of every element from the in argument, which must be a slice or array of a summable type. Summable types include:
- basic types with a defined + operator
- slices (they are concatenated)
If the element type is not summable, Sum will panic. Given a slice of type []T, the returned value will be of type T.
Example ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := []int{1, 2, 3, 4} out := generic.Sum(in).(int) fmt.Println(out) }
Output: 10
Example (Slice) ¶
package main import ( "fmt" "gnd.la/util/generic" ) func main() { in := [][]int{[]int{1, 2}, []int{3, 4}} out := generic.Sum(in).([]int) fmt.Println(out) }
Output: [1 2 3 4]
Types ¶
This section is empty.