Skip to content

Commit

Permalink
feat: add u.Unique{Strings,Ints,Interfaces} helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Apr 23, 2021
1 parent 46bb44f commit cfc54c9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions depaware.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
40 changes: 40 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
@@ -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]
}

0 comments on commit cfc54c9

Please sign in to comment.