@@ -1084,6 +1084,118 @@ public static <T, E, C extends Collection<T>> C collectMany(E[] self, C collecto
1084
1084
return DefaultGroovyMethods .collectMany (new ArrayIterable <>(self ), collector , projection );
1085
1085
}
1086
1086
1087
+ //--------------------------------------------------------------------------
1088
+ // column
1089
+
1090
+ /**
1091
+ * Select a column from a 2D array.
1092
+ * <pre class="groovyTestCase">
1093
+ * double[][] nums = [[1.0d, 2.0d], [10.0d, 20.0d]]
1094
+ * assert nums.column(0) == [1.0d, 10.0d] as double[]
1095
+ * assert nums.column(1) == [2.0d, 20.0d] as double[]
1096
+ * </pre>
1097
+ *
1098
+ * @param self a double[][]
1099
+ * @return a double[]
1100
+ */
1101
+ public static double [] column (double [][] self , int col ) {
1102
+ Objects .requireNonNull (self );
1103
+ int rows = self .length ;
1104
+ if (rows == 0 ) {
1105
+ return new double [0 ];
1106
+ }
1107
+ validateCol (self [0 ].length , col );
1108
+ final double [] result = new double [rows ];
1109
+ for (int row = 0 ; row < rows ; row ++) {
1110
+ result [row ] = self [row ][col ];
1111
+ }
1112
+ return result ;
1113
+ }
1114
+
1115
+ /**
1116
+ * Select a column from a 2D array.
1117
+ * <pre class="groovyTestCase">
1118
+ * float[][] nums = [[1.0f, 2.0f], [10.0f, 20.0f]]
1119
+ * assert nums.column(0) == [1.0f, 10.0f] as float[]
1120
+ * assert nums.column(1) == [2.0f, 20.0f] as float[]
1121
+ * </pre>
1122
+ *
1123
+ * @param self a float[][]
1124
+ * @return a float[]
1125
+ */
1126
+ public static float [] column (float [][] self , int col ) {
1127
+ Objects .requireNonNull (self );
1128
+ int rows = self .length ;
1129
+ if (rows == 0 ) {
1130
+ return new float [0 ];
1131
+ }
1132
+ validateCol (self [0 ].length , col );
1133
+ final float [] result = new float [rows ];
1134
+ for (int row = 0 ; row < rows ; row ++) {
1135
+ result [row ] = self [row ][col ];
1136
+ }
1137
+ return result ;
1138
+ }
1139
+
1140
+ /**
1141
+ * Select a column from a 2D array.
1142
+ * <pre class="groovyTestCase">
1143
+ * int[][] nums = [[1, 2], [10, 20]]
1144
+ * assert nums.column(0) == [1, 10] as int[]
1145
+ * assert nums.column(1) == [2, 20] as int[]
1146
+ * </pre>
1147
+ *
1148
+ * @param self an int[][]
1149
+ * @return an int[]
1150
+ */
1151
+ public static int [] column (int [][] self , int col ) {
1152
+ Objects .requireNonNull (self );
1153
+ int rows = self .length ;
1154
+ if (rows == 0 ) {
1155
+ return new int [0 ];
1156
+ }
1157
+ validateCol (self [0 ].length , col );
1158
+ final int [] result = new int [rows ];
1159
+ for (int row = 0 ; row < rows ; row ++) {
1160
+ result [row ] = self [row ][col ];
1161
+ }
1162
+ return result ;
1163
+ }
1164
+
1165
+ /**
1166
+ * Select a column from a 2D array.
1167
+ * <pre class="groovyTestCase">
1168
+ * long[][] nums = [[1L, 2L], [10L, 20L]]
1169
+ * assert nums.column(0) == [1L, 10L] as long[]
1170
+ * assert nums.column(1) == [2L, 20L] as long[]
1171
+ * </pre>
1172
+ *
1173
+ * @param self a long[][]
1174
+ * @return a long[]
1175
+ */
1176
+ public static long [] column (long [][] self , int col ) {
1177
+ Objects .requireNonNull (self );
1178
+ int rows = self .length ;
1179
+ if (rows == 0 ) {
1180
+ return new long [0 ];
1181
+ }
1182
+ validateCol (self [0 ].length , col );
1183
+ final long [] result = new long [rows ];
1184
+ for (int row = 0 ; row < rows ; row ++) {
1185
+ result [row ] = self [row ][col ];
1186
+ }
1187
+ return result ;
1188
+ }
1189
+
1190
+ private static void validateCol (int size , int col ) {
1191
+ if (col < 0 ) {
1192
+ throw new IllegalArgumentException ("Expected column value of " + col + " to be 0 or greater" );
1193
+ }
1194
+ if (col >= size ) {
1195
+ throw new IllegalArgumentException ("Expected column value of " + col + " to be less than " + size );
1196
+ }
1197
+ }
1198
+
1087
1199
//--------------------------------------------------------------------------
1088
1200
// columns
1089
1201
@@ -1853,6 +1965,81 @@ public static void eachByte(Byte[] self, @ClosureParams(FirstParam.Component.cla
1853
1965
each (self , closure );
1854
1966
}
1855
1967
1968
+ //--------------------------------------------------------------------------
1969
+ // eachColumn
1970
+
1971
+ /**
1972
+ * Process the columns of the array.
1973
+ * <pre class="groovyTestCase">
1974
+ * double[][] nums = [[1.0d, 2.0d], [10.0d, 20.0d]]
1975
+ * nums.eachColumn {
1976
+ * assert it[0] * 10.0d == it[1]
1977
+ * }
1978
+ * </pre>
1979
+ *
1980
+ * @param self a double[][]
1981
+ * @param closure the closure applied on each array column
1982
+ * @return the self array
1983
+ */
1984
+ public static double [][] eachColumn (double [][] self , @ ClosureParams (FirstParam .Component .class ) Closure <?> closure ) {
1985
+ DefaultGroovyMethods .each (new DoubleDoubleArrayColumnIterator (self ), closure );
1986
+ return self ;
1987
+ }
1988
+
1989
+ /**
1990
+ * Process the columns of the array.
1991
+ * <pre class="groovyTestCase">
1992
+ * double[][] nums = [[1.0f, 2.0f], [10.0f, 20.0f]]
1993
+ * nums.eachColumn {
1994
+ * assert it[0] * 10.0f == it[1]
1995
+ * }
1996
+ * </pre>
1997
+ *
1998
+ * @param self a float[][]
1999
+ * @param closure the closure applied on each array column
2000
+ * @return the self array
2001
+ */
2002
+ public static float [][] eachColumn (float [][] self , @ ClosureParams (FirstParam .Component .class ) Closure <?> closure ) {
2003
+ DefaultGroovyMethods .each (new FloatFloatArrayColumnIterator (self ), closure );
2004
+ return self ;
2005
+ }
2006
+
2007
+ /**
2008
+ * Process the columns of the array.
2009
+ * <pre class="groovyTestCase">
2010
+ * double[][] nums = [[1, 2], [10, 20]]
2011
+ * nums.eachColumn {
2012
+ * assert it[0] * 10 == it[1]
2013
+ * }
2014
+ * </pre>
2015
+ *
2016
+ * @param self an int[][]
2017
+ * @param closure the closure applied on each array column
2018
+ * @return the self array
2019
+ */
2020
+ public static int [][] eachColumn (int [][] self , @ ClosureParams (FirstParam .Component .class ) Closure <?> closure ) {
2021
+ DefaultGroovyMethods .each (new IntIntArrayColumnIterator (self ), closure );
2022
+ return self ;
2023
+ }
2024
+
2025
+ /**
2026
+ * Process the columns of the array.
2027
+ * <pre class="groovyTestCase">
2028
+ * double[][] nums = [[1L, 2L], [10L, 20L]]
2029
+ * nums.eachColumn {
2030
+ * assert it[0] * 10L == it[1]
2031
+ * }
2032
+ * </pre>
2033
+ *
2034
+ * @param self a long[][]
2035
+ * @param closure the closure applied on each array column
2036
+ * @return the self array
2037
+ */
2038
+ public static long [][] eachColumn (long [][] self , @ ClosureParams (FirstParam .Component .class ) Closure <?> closure ) {
2039
+ DefaultGroovyMethods .each (new LongLongArrayColumnIterator (self ), closure );
2040
+ return self ;
2041
+ }
2042
+
1856
2043
//--------------------------------------------------------------------------
1857
2044
// eachWithIndex
1858
2045
0 commit comments