diff --git a/AUTHORS b/AUTHORS index da8381c..473f0ce 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,6 +1,7 @@ # This file lists all individuals having contributed content to the repository. # For how it is generated, see 'https://github.com/moul/rules.mk' +dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Manfred Touron <94029+moul@users.noreply.github.com> moul-bot Renovate Bot diff --git a/README.md b/README.md index 572379d..1cf9522 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,15 @@ func TempfileWithContent(content []byte) (*os.File, func(), error) returned parameter is a cleanup function that closes and removes the temp file. +func UniqueInterfaces(input []interface{}) []interface{} + UniqueInterfaces removes duplicate values from an interface slice. + +func UniqueInts(input []int) []int + UniqueInts removes duplicate values from an int slice. + +func UniqueStrings(input []string) []string + UniqueStrings removes duplicate values from a string slice. + func Unzip(src string, dest string) ([]string, error) Unzip decompresses a zip archive, moving all files and folders within the zip file to an output directory. Based on diff --git a/depaware.txt b/depaware.txt index 6078c6f..a2736b0 100644 --- a/depaware.txt +++ b/depaware.txt @@ -18,14 +18,15 @@ moul.io/u dependencies: (generated by github.com/tailscale/depaware) hash from archive/zip+ hash/crc32 from archive/zip io from archive/zip+ - io/ioutil from archive/zip+ + io/fs from archive/zip+ + io/ioutil from moul.io/u math from compress/flate+ math/bits from compress/flate+ os from archive/zip+ os/exec from moul.io/u os/signal from moul.io/u os/user from moul.io/u - path from archive/zip + path from archive/zip+ path/filepath from io/ioutil+ reflect from encoding/binary+ sort from compress/flate+ diff --git a/slice.go b/slice.go new file mode 100644 index 0000000..705c0b4 --- /dev/null +++ b/slice.go @@ -0,0 +1,40 @@ +package u + +// UniqueStrings removes duplicate values from a string slice. +func UniqueStrings(input []string) []string { + keys := make(map[string]bool) + list := []string{} + for _, entry := range input { + if _, value := keys[entry]; !value { + keys[entry] = true + list = append(list, entry) + } + } + return list +} + +// UniqueInts removes duplicate values from an int slice. +func UniqueInts(input []int) []int { + keys := make(map[int]bool) + list := []int{} + for _, entry := range input { + if _, value := keys[entry]; !value { + keys[entry] = true + list = append(list, entry) + } + } + return list +} + +// UniqueInterfaces removes duplicate values from an interface slice. +func UniqueInterfaces(input []interface{}) []interface{} { + keys := make(map[interface{}]bool) + list := []interface{}{} + for _, entry := range input { + if _, value := keys[entry]; !value { + keys[entry] = true + list = append(list, entry) + } + } + return list +} diff --git a/slice_test.go b/slice_test.go new file mode 100644 index 0000000..d89c46f --- /dev/null +++ b/slice_test.go @@ -0,0 +1,22 @@ +package u_test + +import ( + "fmt" + + "moul.io/u" +) + +func ExampleUniqueStrings() { + fmt.Println(u.UniqueStrings([]string{"foo", "bar", "foo", "baz", "foo", "bar", "foobar", "baz", "foobaz"})) + // Output: [foo bar baz foobar foobaz] +} + +func ExampleUniqueInts() { + fmt.Println(u.UniqueInts([]int{13, 51, 36, 69, 92, 92, 42, 21, 36, 13, 51})) + // Output: [13 51 36 69 92 42 21] +} + +func ExampleUniqueInterfaces() { + fmt.Println(u.UniqueInterfaces([]interface{}{13, "foo", "bar", 42, 13, 43, "baz"})) + // Output: [13 foo bar 42 43 baz] +}