You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2023/12/18 23:26:00 UTC
(groovy) branch master updated: GROOVY-11253: Identity variants for DGM#countBy
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 4c10bc8ad4 GROOVY-11253: Identity variants for DGM#countBy
4c10bc8ad4 is described below
commit 4c10bc8ad43a9dc015be374889adb2e967412afc
Author: Paul King <pa...@asert.com.au>
AuthorDate: Mon Dec 18 21:02:39 2023 +1000
GROOVY-11253: Identity variants for DGM#countBy
---
.../groovy/runtime/ArrayGroovyMethods.java | 16 +++++++++++++
.../groovy/runtime/DefaultGroovyMethods.java | 28 ++++++++++++++++++++++
2 files changed, 44 insertions(+)
diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index 54c2acb443..ca16a4b9a9 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -1422,6 +1422,22 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
return DefaultGroovyMethods.countBy(new ArrayIterator<>(self), closure);
}
+ /**
+ * Creates a multiset-like map of the array members.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">
+ * assert [1:2, 2:2, 3:1] == ([1,2,1,2,3] as Object[]).countBy()
+ * </pre>
+ *
+ * @param self an array to group and count
+ * @return a new Map where the keys are the set of values in the array and the values are the frequency counts
+ * @since 5.0.0
+ */
+ public static <E> Map<E, Integer> countBy(E[] self) {
+ return countBy(self, Closure.IDENTITY);
+ }
+
//-------------------------------------------------------------------------
// drop
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 2bf5658e85..6999785f8b 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -3092,6 +3092,20 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
return countBy(self.iterator(), closure);
}
+ /**
+ * Creates a multiset-like map of the collection members.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">assert [1:2, 2:2, 3:1] == [1,2,1,2,3].countBy()</pre>
+ *
+ * @param self a collection to group and count
+ * @return a new Map where the keys are the set of values in the collection and the values are the frequency counts
+ * @since 5.0.0
+ */
+ public static <E> Map<E, Integer> countBy(Iterable<E> self) {
+ return countBy(self.iterator(), Closure.IDENTITY);
+ }
+
/**
* Sorts all iterator items into groups determined by the supplied mapping
* closure and counts the group size. The closure should return the key that each
@@ -3116,6 +3130,20 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
return answer;
}
+ /**
+ * Creates a multiset-like map of the iterator members.
+ * <p>
+ * Example usage:
+ * <pre class="groovyTestCase">assert [1:2, 2:2, 3:1] == [1,2,1,2,3].iterator().countBy()</pre>
+ *
+ * @param self an iterator to group and count
+ * @return a new Map where the keys are the set of values produced by the iterator and the values are the frequency counts
+ * @since 5.0.0
+ */
+ public static <E> Map<E, Integer> countBy(Iterator<E> self) {
+ return countBy(self, Closure.IDENTITY);
+ }
+
/**
* Groups the members of a map into groups determined by the
* supplied mapping closure and counts the frequency of the created groups.