Repository: elliotchance/pie
Pull request #158 (2020-02-03 10:36:51)

Group
refs to https://github.com/elliotchance/pie/issues/26

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/pie/158)
<!-- Reviewable:end -->


codecov-io: # [Codecov](https://codecov.io/gh/elliotchance/pie/pull/158?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@31a2a34`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/elliotchance/pie/pull/158/graphs/tree.svg?width=650&token=OvugwUQGM3&height=150&src=pr)](https://codecov.io/gh/elliotchance/pie/pull/158?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##             master     #158   +/-   ##
=========================================
  Coverage          ?   98.96%           
=========================================
  Files             ?        8           
  Lines             ?     1732           
  Branches          ?        0           
=========================================
  Hits              ?     1714           
  Misses            ?        9           
  Partials          ?        9
```


| [Impacted Files](https://codecov.io/gh/elliotchance/pie/pull/158?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pie/float64s\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/158/diff?src=pr&el=tree#diff-cGllL2Zsb2F0NjRzX3BpZS5nbw==) | `99.24% <100%> (ø)` | |
| [pie/strings\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/158/diff?src=pr&el=tree#diff-cGllL3N0cmluZ3NfcGllLmdv) | `98.28% <100%> (ø)` | |
| [pie/ints\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/158/diff?src=pr&el=tree#diff-cGllL2ludHNfcGllLmdv) | `99.24% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/elliotchance/pie/pull/158?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/elliotchance/pie/pull/158?src=pr&el=footer). Last update [31a2a34...370bace](https://codecov.io/gh/elliotchance/pie/pull/158?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.md b/README.md
index aade08c..23b3b8a 100644
--- a/README.md
+++ b/README.md
@@ -194,6 +194,7 @@ The letters in brackets indicate:
 | [`First`](#first)                       | ✓      | ✓      | ✓      |      | 1        | First returns the first element, or zero. Also see FirstOr().  |
 | [`FirstOr`](#firstor)                   | ✓      | ✓      | ✓      |      | 1        | FirstOr returns the first element or a default value if there are no elements.  |
 | [`Float64s`](#float64s) (S)             | ✓      | ✓      | ✓      |      | n        | Float64s transforms each element to a float64.  |
+| [`Group`](#group)                       | ✓      | ✓      |        |      | n        | Group returns a map of the value with an individual count.  |
 | [`Intersect`](#intersect)               | ✓      | ✓      |        |      | n        | Intersect returns items that exist in all lists.  |
 | [`Ints`](#ints) (S)                     | ✓      | ✓      | ✓      |      | n        | Ints transforms each element to an integer.  |
 | [`Join`](#join) (S)                     | ✓      | ✓      | ✓      |      | n        | Join returns a string from joining each of the elements.  |
@@ -410,6 +411,13 @@ elements.
 Float64s transforms each element to a float64.
 
 
+## Group
+
+Group returns a map of the value with an individual count.
+
+
+
+
 ## Intersect
 
 Intersect returns items that exist in all lists.
diff --git a/functions/group.go b/functions/group.go
new file mode 100644
index 0000000..5aff14f
--- /dev/null
+++ b/functions/group.go
@@ -0,0 +1,11 @@
+package functions
+
+// Group returns a map of the value with an individual count.
+//
+func (ss SliceType) Group() map[ElementType]int {
+	group := map[ElementType]int{}
+	for _, n := range ss {
+		group[n]++
+	}
+	return group
+}
diff --git a/functions/main.go b/functions/main.go
index 6ad997d..8d96ac2 100644
--- a/functions/main.go
+++ b/functions/main.go
@@ -41,6 +41,7 @@ var Functions = []struct {
 	{"First", "first.go", ForAll, "1"},
 	{"FirstOr", "first_or.go", ForAll, "1"},
 	{"Float64s", "float64s.go", ForAll, "n"},
+	{"Group", "group.go", ForNumbersAndStrings, "n"},
 	{"Intersect", "intersect.go", ForNumbersAndStrings, "n"},
 	{"Ints", "ints.go", ForAll, "n"},
 	{"Join", "join.go", ForAll, "n"},
diff --git a/pie/float64s_pie.go b/pie/float64s_pie.go
index d58bc17..b800800 100755
--- a/pie/float64s_pie.go
+++ b/pie/float64s_pie.go
@@ -304,6 +304,16 @@ func (ss Float64s) Float64s() Float64s {
 	return result
 }
 
+// Group returns a map of the value with an individual count.
+//
+func (ss Float64s) Group() map[float64]int {
+	group := map[float64]int{}
+	for _, n := range ss {
+		group[n]++
+	}
+	return group
+}
+
 // Intersect returns items that exist in all lists.
 //
 // It returns slice without any duplicates.
diff --git a/pie/ints_pie.go b/pie/ints_pie.go
index d9d044f..11ea71b 100755
--- a/pie/ints_pie.go
+++ b/pie/ints_pie.go
@@ -304,6 +304,16 @@ func (ss Ints) Float64s() Float64s {
 	return result
 }
 
+// Group returns a map of the value with an individual count.
+//
+func (ss Ints) Group() map[int]int {
+	group := map[int]int{}
+	for _, n := range ss {
+		group[n]++
+	}
+	return group
+}
+
 // Intersect returns items that exist in all lists.
 //
 // It returns slice without any duplicates.
diff --git a/pie/strings_pie.go b/pie/strings_pie.go
index 3b6afdf..7bb650a 100755
--- a/pie/strings_pie.go
+++ b/pie/strings_pie.go
@@ -280,6 +280,16 @@ func (ss Strings) Float64s() Float64s {
 	return result
 }
 
+// Group returns a map of the value with an individual count.
+//
+func (ss Strings) Group() map[string]int {
+	group := map[string]int{}
+	for _, n := range ss {
+		group[n]++
+	}
+	return group
+}
+
 // Intersect returns items that exist in all lists.
 //
 // It returns slice without any duplicates.
diff --git a/template.go b/template.go
index d0fc78e..7a0496b 100644
--- a/template.go
+++ b/template.go
@@ -343,6 +343,18 @@ func (ss SliceType) Float64s() pie.Float64s {
 
 	return result
 }
+`,
+	"Group": `package functions
+
+// Group returns a map of the value with an individual count.
+//
+func (ss SliceType) Group() map[ElementType]int {
+	group := map[ElementType]int{}
+	for _, n := range ss {
+		group[n]++
+	}
+	return group
+}
 `,
 	"Intersect": `package functions
 


Test patch:
diff --git a/pie/float64s_test.go b/pie/float64s_test.go
index afdfe12..763161a 100644
--- a/pie/float64s_test.go
+++ b/pie/float64s_test.go
@@ -1488,3 +1488,17 @@ func TestFloat64s_Join(t *testing.T) {
 	var f1, f2 float64 = 0.1, 20000000000000000
 	assert.Equal(t, "0.1;2e+16", Float64s{f1, f2}.Join(";"))
 }
+
+func TestFloat64s_Group(t *testing.T) {
+	assert.Equal(t, map[float64]int{}, Float64s(nil).Group())
+
+	assert.Equal(t, map[float64]int{
+		1: 1,
+	}, Float64s{1}.Group())
+
+	assert.Equal(t, map[float64]int{
+		1: 1,
+		2: 2,
+		3: 3,
+	}, Float64s{1, 2, 2, 3, 3, 3}.Group())
+}
diff --git a/pie/ints_test.go b/pie/ints_test.go
index a92490b..ab7ae35 100644
--- a/pie/ints_test.go
+++ b/pie/ints_test.go
@@ -1477,3 +1477,17 @@ func TestInts_Join(t *testing.T) {
 	assert.Equal(t, "1-2-3", Ints{1, 2, 3}.Join("-"))
 	assert.Equal(t, "1--2-3", Ints{1, -2, 3}.Join("-"))
 }
+
+func TestInts_Group(t *testing.T) {
+	assert.Equal(t, map[int]int{}, Ints(nil).Group())
+
+	assert.Equal(t, map[int]int{
+		1: 1,
+	}, Ints{1}.Group())
+
+	assert.Equal(t, map[int]int{
+		1: 1,
+		2: 2,
+		3: 3,
+	}, Ints{1, 2, 2, 3, 3, 3}.Group())
+}
diff --git a/pie/strings_test.go b/pie/strings_test.go
index 96bcebb..2cc0a62 100644
--- a/pie/strings_test.go
+++ b/pie/strings_test.go
@@ -1501,3 +1501,17 @@ func TestStrings_ShiftAndUnshift(t *testing.T) {
 		})
 	}
 }
+
+func TestStrings_Group(t *testing.T) {
+	assert.Equal(t, map[string]int{}, Strings(nil).Group())
+
+	assert.Equal(t, map[string]int{
+		"foo": 1,
+	}, Strings{"foo"}.Group())
+
+	assert.Equal(t, map[string]int{
+		"foo":    1,
+		"bar":    2,
+		"foobar": 3,
+	}, Strings{"foo", "bar", "bar", "foobar", "foobar", "foobar"}.Group())
+}