generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from moul/dev/moul/unique-slices
feat: add u.Unique{Strings,Ints,Interfaces} helpers
- Loading branch information
Showing
5 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |