A utility library for working with slices and maps, an open source library focused on type safety and performance.
GitHub address: https://github.com/elliotchance/pie
quick start
The current version of pie is v2, which requires Go 1.18+ and supports generics. If it is Go 1.17 or lower, you must use the v1 version (https://github.com/elliotchance/pie/v1).
package main
import (
"fmt"
"strings"
"github.com/elliotchance/pie/v2"
)
func main() {
names := pie.FilterNot([]string{
"Bob", "Sally", "John", "Jane"},
func(name string) bool {
return strings.HasPrefix(name, "J")
})
fmt.Println(names) // "[Bob Sally]"
}
pie's goal
Type safety: I never want to run into runtime errors because I might pass in the wrong type, or have an invalid type case on the other end.
High performance: These functions need to be as fast as the native Go implementation, otherwise the library is pointless.
Nil safety: all functions will happily accept nil and treat them as empty slices. In addition to reducing possible panics, it also makes usage easier.
Immutable: The function never modifies the input (except in illogical circumstances), unlike some built-in functions such as sort.Strings.
Function Description
The complete function description of pie is at this URL
https://pkg.go.dev/github.com/elliotchance/pie/v2
Next, let's take an example to see what practical functions pie provides.
Numerical operations
For T interface{ constraints.Integer | constraints.Float, that is, integer and float types support operation functions.
func main() {
s := []int{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println(pie.Min(s)) // 最小值:1
fmt.Println(pie.Max(s)) // 最大值:10
fmt.Println(pie.Sum(s)) // 求和:55
fmt.Println(pie.Average(s)) // 取平均值:5.5
fmt.Println(pie.Product(s)) // 相乘:3628800
}
Set operations
support the difference and intersection between two slices for all types.
func main() {
a1 := []int{
1, 2, 3, 4, 6, 7, 8}
a2 := []int{
2, 3, 4, 5, 7, 8, 9, 10}
added, removed := pie.Diff(a1, a2) // 计算差集
fmt.Println(added, removed) // [5 9 10] [1 6]
intersect := pie.Intersect(a1, a2) // 计算交集
fmt.Println(intersect) // [2 3 4 7 8]
}
Conditional judgment
func main() {
// 判断条件是否都满足 output: ture
fmt.Println(pie.All(s, func(x int) bool {
return x > 0 }))
// 判断条件是否有满足 output: ture
fmt.Println(pie.Any(s, func(x int) bool {
return x > 5 }))
}
Serialization
The default json serialization will return an error. Sometimes it is clear that an error will not occur, but it still needs to be processed, so pie ignores the error.
func main() {
s := []int{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
jsonString := pie.JSONString(s)
fmt.Println(jsonString) // [1,2,3,4,5,6,7,8,9,10]
}
Sorting function
pie provides a sorting function, does not modify the parameters passed in, and is guaranteed to be immutable.
func main() {
s1 := []int{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
sorted := pie.AreSorted(s1)
fmt.Println(sorted) // true
s2 := []string{
"Hello", "World", "!"}
sort := pie.Sort(s2)
fmt.Println(sort, s2) // [! Hello World] [Hello World !]
}
Summarize
pie provides many other functions, such as deduplication, traversal, filtering and other operations. Many common functions are encapsulated and are very convenient to use.