aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-05-24 22:30:51 +0700
committerShulhan <ms@kilabit.info>2019-05-25 00:21:41 +0700
commitcf5b98a0d410bb975ffc72c086c929b68d965ab2 (patch)
tree35714794449630a4ad8578f1146d43b92f0e0eb6
parentec125a97110880e34bac150b9acb36f7553f7060 (diff)
downloadpakakeh.go-cf5b98a0d410bb975ffc72c086c929b68d965ab2.tar.xz
strings: add function to append uniq values to slice of strings
The AppendUniq will append a string to slice only if the same string its not already exist. The string is compared in case insensitive manner.
-rw-r--r--lib/strings/strings.go24
-rw-r--r--lib/strings/strings_example_test.go11
2 files changed, 35 insertions, 0 deletions
diff --git a/lib/strings/strings.go b/lib/strings/strings.go
index c78ea788..d1e11fe8 100644
--- a/lib/strings/strings.go
+++ b/lib/strings/strings.go
@@ -15,6 +15,30 @@ import (
)
//
+// AppendUniq append case-insensitive strings to slice of input without
+// duplicate.
+//
+func AppendUniq(in []string, vals ...string) []string {
+ var found bool
+
+ for x := 0; x < len(vals); x++ {
+ found = false
+ for y := 0; y < len(in); y++ {
+ if vals[x] == in[y] {
+ found = true
+ break
+ }
+ }
+ if found {
+ continue
+ }
+ in = append(in, vals[x])
+ }
+
+ return in
+}
+
+//
// CountMissRate given two slice of string, count number of string that is
// not equal with each other, and return the miss rate as
//
diff --git a/lib/strings/strings_example_test.go b/lib/strings/strings_example_test.go
index d661232a..df2f5125 100644
--- a/lib/strings/strings_example_test.go
+++ b/lib/strings/strings_example_test.go
@@ -8,6 +8,17 @@ import (
"fmt"
)
+func ExampleAppendUniq() {
+ in := []string{"a", "", "b", "c"}
+ vals := []string{"b", "", "C", "d"}
+
+ in = AppendUniq(in, vals...)
+
+ fmt.Println(in)
+ // Output:
+ // [a b c C d]
+}
+
func ExampleCountMissRate() {
src := []string{"A", "B", "C", "D"}
tgt := []string{"A", "B", "C", "D"}