From 4449e4c7ed69601d1ffd70168428d50eb1db7332 Mon Sep 17 00:00:00 2001 From: akushman Date: Tue, 19 Mar 2019 22:18:41 +0300 Subject: [PATCH] #18. Add ArrayCountValues + change file names --- README.md | 7 +++++++ array.go | 13 +++++++++++++ array_test.go | 22 ++++++++++++++++++++++ pdate.go => date.go | 0 pdate_test.go => date_test.go | 0 pfiles.go => files.go | 0 pfiles_test.go => files_test.go | 0 pstr.go => str.go | 0 pstr_test.go => str_test.go | 0 9 files changed, 42 insertions(+) rename pdate.go => date.go (100%) rename pdate_test.go => date_test.go (100%) rename pfiles.go => files.go (100%) rename pfiles_test.go => files_test.go (100%) rename pstr.go => str.go (100%) rename pstr_test.go => str_test.go (100%) diff --git a/README.md b/README.md index a6fd83d..27200ff 100644 --- a/README.md +++ b/README.md @@ -139,4 +139,11 @@ map[int]string{ */ ``` +#### Count all the values of an array/slice +```go +pgo.ArrayCountValues([]string{"foo", "bar", "foo", "baz", "bar", "bar"}) // map[string]int{"foo": 2, "bar": 3, "baz": 1} + +pgo.ArrayCountValues([]float64{3.14159, 43.03, 8, 3.14159, 43.02, 8}) // map[float64]int{3.14159: 2, 8: 2, 43.03: 1, 43.02: 1} +``` + See more examples in *_test.go files. diff --git a/array.go b/array.go index 7b1c2a1..b3ce8ac 100644 --- a/array.go +++ b/array.go @@ -65,3 +65,16 @@ func ArrayCombine(keys interface{}, values interface{}) map[interface{}]interfac return resultMap } + +// ArrayCountValues counts all the values of an array/slice +func ArrayCountValues(array interface{}) map[interface{}]int { + res := make(map[interface{}]int) + + s := reflect.ValueOf(array) + len := s.Len() + for i := 0; i < len; i++ { + res[s.Index(i).Interface()]++ + } + + return res +} diff --git a/array_test.go b/array_test.go index d53e6c4..3d205d0 100644 --- a/array_test.go +++ b/array_test.go @@ -98,3 +98,25 @@ func TestArrayCombine(t *testing.T) { } } } + +var testArrayCountValues = []struct { + values interface{} + result interface{} +}{ + {[]string{"foo", "bar", "foo", "baz", "bar", "bar"}, map[string]int{"foo": 2, "bar": 3, "baz": 1}}, + {[]int{3, 43, 8, 43, 8}, map[int]int{43: 2, 8: 2, 3: 1}}, + {[]float64{3.14159, 43.03, 8, 3.14159, 43.02, 8}, map[float64]int{3.14159: 2, 8: 2, 43.03: 1, 43.02: 1}}, +} + +func TestArrayCountValues(t *testing.T) { + for _, object := range testArrayCountValues { + res := pgo.ArrayCountValues(object.values) + + m := reflect.ValueOf(object.result) + for k, v := range res { + if m.MapIndex(reflect.ValueOf(k)).Interface() != v { + t.Fatalf("want %d, got %d", m.MapIndex(reflect.ValueOf(k)).Interface(), v) + } + } + } +} diff --git a/pdate.go b/date.go similarity index 100% rename from pdate.go rename to date.go diff --git a/pdate_test.go b/date_test.go similarity index 100% rename from pdate_test.go rename to date_test.go diff --git a/pfiles.go b/files.go similarity index 100% rename from pfiles.go rename to files.go diff --git a/pfiles_test.go b/files_test.go similarity index 100% rename from pfiles_test.go rename to files_test.go diff --git a/pstr.go b/str.go similarity index 100% rename from pstr.go rename to str.go diff --git a/pstr_test.go b/str_test.go similarity index 100% rename from pstr_test.go rename to str_test.go