https://github.com/goiste/generics
Generic sets and slices in go
https://github.com/goiste/generics
generics go golang helper set slice
Last synced: 2 months ago
JSON representation
Generic sets and slices in go
- Host: GitHub
- URL: https://github.com/goiste/generics
- Owner: goiste
- Created: 2022-04-03T11:54:31.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-19T14:40:42.000Z (over 2 years ago)
- Last Synced: 2024-06-20T16:39:59.476Z (12 months ago)
- Topics: generics, go, golang, helper, set, slice
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Generics
some generics-based helper functions for working with slices and sets (go 1.18+)
Usage:
```go
package mainimport (
"fmt"
"github.com/goiste/generics/sets"
"github.com/goiste/generics/slices"
)func main() {
x := []int{1, 2, 3, 4, 5}
fmt.Println(slices.Reverse(x)) // [5 4 3 2 1]
f := slices.Convert[int, float64](x)
fmt.Printf("%T\n", f[0]) // float64
fmt.Println(slices.Min(f)) // 1
fmt.Println(slices.Max(f)) // 5
fmt.Println(slices.Sum(f)) // 15
f = []float64{0.000001, 0.02, 0.300000003}
formatted := slices.Format(f, "%.2f")
fmt.Println(formatted) // [0.00 0.02 0.30]genInt := slices.SequenceGenerator(10, -1)
fmt.Println(genInt()) // 10
fmt.Println(genInt()) // 9
fmt.Println(genInt()) // 8
// ...s := sets.Make[string]("one", "two", "three")
s.Add("three", "four", "five")
s.Diff(sets.Make[string]("four", "five"))
s.Map(func(str string) string {
return str + "!"
})
fmt.Println(s.Has("one!")) // true
s.Delete("one!")
fmt.Println(s.Len()) // 2
}
```