[ 
https://issues.apache.org/jira/browse/GROOVY-11654?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17949733#comment-17949733
 ] 

ASF GitHub Bot commented on GROOVY-11654:
-----------------------------------------

Copilot commented on code in PR #2214:
URL: https://github.com/apache/groovy/pull/2214#discussion_r2075538165


##########
src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java:
##########
@@ -7260,6 +7260,405 @@ public static <T> T[] plus(T[] left, Object right) {
         return result;
     }
 
+    
//--------------------------------------------------------------------------
+    // putAt
+
+    /**
+     * A helper method to allow arrays to be set with subscript operators.
+     * <pre class="groovyTestCase">
+     * Integer[] from = [70, 80, 90]
+     * Integer[] nums = [1, 2, 3, 4, 5]
+     * nums[1..3] = from
+     * assert nums == [1, 70, 80, 90, 5]
+     * </pre>
+     *
+     * @param self   an array
+     * @param range  the subset of the array to set
+     * @param source the source array from which new values will come
+     * @throws IndexOutOfBoundsException if the source doesn't have sufficient 
elements or the IntRange is too big for the self array
+     * @since 5.0.0
+     */
+    public static <T> void putAt(T[] self, IntRange range, T[] source) {
+        RangeInfo info = range.subListBorders(self.length);
+        System.arraycopy(source, 0, self, info.from, info.to - info.from);
+    }
+
+    /**
+     * A helper method to allow arrays to be set with subscript operators.
+     * Null will be used if the source iterable has insufficient elements.
+     * <pre class="groovyTestCase">
+     * def from = [70, 80, 90]
+     * Integer[] nums = [1, 2, 3, 4, 5]
+     * nums[1..3] = from
+     * assert nums == [1, 70, 80, 90, 5]
+     * </pre>
+     *
+     * @param self   an array
+     * @param range  the subset of the array to set
+     * @param source the source array from which new values will come
+     * @since 5.0.0
+     */
+    public static <T> void putAt(T[] self, IntRange range, Iterable<T> source) 
{
+        RangeInfo info = range.subListBorders(self.length);

Review Comment:
   The logic for processing Iterable sources is duplicated across various putAt 
methods. Consider refactoring the common iteration code into a private helper 
method to improve maintainability.
   ```suggestion
           RangeInfo info = range.subListBorders(self.length);
           assignFromIterable(self, info, source);
       }
   
       private static <T> void assignFromIterable(T[] self, RangeInfo info, 
Iterable<T> source) {
   ```





> Create AGM#putAt variants
> -------------------------
>
>                 Key: GROOVY-11654
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11654
>             Project: Groovy
>          Issue Type: New Feature
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>
> When creating potential examples for the Groovy 5 release notes, I created 
> this example:
> {code:groovy}
> // keep top 5 scores in top array
> int[] scores = [56, 82, 70, 74, 63, 92, 49, 69, 85, 79, 80, 82, 99, 95]
> int[] top = scores[0..4].sort()
> scores[5..-1].each { next ->
>     def ins = top.partitionPoint{ it < next } - 1
>     if (ins > 0) {
>         System.arraycopy(top, 1, top, 0, ins)
>         // top[0..<ins] = top[1..ins]
>     }
>     if (ins >= 0) {
>         top[ins] = next
>     }
> }
> assert top == [82, 85, 92, 95, 99]
> {code}
> The {{System.arraycopy()}} line would be {{top[0..<ins] = top[1..ins]}} if 
> using lists. It would be nice if arrays supported putAt and that line didn't 
> need to change.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to