package stdlib; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import zsynthetic.FunctionUSizeTToBool; import zsynthetic.FunctionUSizeTToU; import zsynthetic.FunctionUSizeTToVoid; public final class Arrays { private Arrays() {} public static T getFirst(Class typeOfT, T[] self) { return self.length == 0 ? null : self[0]; } public static T getLast(Class typeOfT, T[] self) { return self.length == 0 ? null : self[self.length - 1]; } public static void reverse(Class typeOfT, T[] self) { int limitForI = self.length / 2; for (int i = 0; i < limitForI; i++) { T temp = self[i]; self[i] = self[self.length - i - 1]; self[self.length - i - 1] = temp; } } public static U[] map(Class typeOfT, T[] self, Class typeOfU, Function projection) { U[] temp1 = (U[])(Array.newInstance(typeOfU, self.length)); for (int temp2 = 0; temp2 < temp1.length; temp2++) temp1[temp2] = projection.apply(self[temp2]); return temp1; } public static U[] map(Class typeOfT, T[] self, Class typeOfU, FunctionUSizeTToU projection) { U[] temp1 = (U[])(Array.newInstance(typeOfU, self.length)); for (int temp2 = 0; temp2 < temp1.length; temp2++) temp1[temp2] = projection.invoke(temp2, self[temp2]); return temp1; } public static T[] filter(Class typeOfT, T[] self, Predicate predicate) { List values = new ArrayList(); for (T value : self) if (predicate.test(value)) values.add(value); return values.toArray((T[])(Array.newInstance(typeOfT, values.size()))); } public static T[] filter(Class typeOfT, T[] self, FunctionUSizeTToBool predicate) { List values = new ArrayList(); for (int i = 0; i < self.length; i++) { T value = self[i]; if (predicate.invoke(i, value)) values.add(value); } return values.toArray((T[])(Array.newInstance(typeOfT, values.size()))); } public static void each(Class typeOfT, T[] self, Consumer consumer) { for (T value : self) consumer.accept(value); } public static void each(Class typeOfT, T[] self, FunctionUSizeTToVoid consumer) { for (int i = 0; i < self.length; i++) { T value = self[i]; consumer.invoke(i, value); } } public static boolean contains(Class typeOfT, T[] self, Predicate predicate) { for (T value : self) if (predicate.test(value)) return true; return false; } public static boolean contains(Class typeOfT, T[] self, FunctionUSizeTToBool predicate) { for (int i = 0; i < self.length; i++) { T value = self[i]; if (predicate.invoke(i, value)) return true; } return false; } public static boolean all(Class typeOfT, T[] self, Predicate predicate) { for (T value : self) if (!predicate.test(value)) return false; return true; } public static boolean all(Class typeOfT, T[] self, FunctionUSizeTToBool predicate) { for (int i = 0; i < self.length; i++) { T value = self[i]; if (!predicate.invoke(i, value)) return false; } return true; } public static T first(Class typeOfT, T[] self, Predicate predicate) { for (T value : self) if (predicate.test(value)) return value; return null; } public static T first(Class typeOfT, T[] self, FunctionUSizeTToBool predicate) { for (int i = 0; i < self.length; i++) { T value = self[i]; if (predicate.invoke(i, value)) return value; } return null; } public static T last(Class typeOfT, T[] self, Predicate predicate) { int i = self.length; while (i > 0) { i--; if (predicate.test(self[i])) return self[i]; } return null; } public static T last(Class typeOfT, T[] self, FunctionUSizeTToBool predicate) { int i = self.length; while (i > 0) { i--; if (predicate.invoke(i, self[i])) return self[i]; } return null; } public static int count(Class typeOfT, T[] self, Predicate predicate) { int result = 0; for (T value : self) if (predicate.test(value)) result++; return result; } public static int count(Class typeOfT, T[] self, FunctionUSizeTToBool predicate) { int result = 0; for (int i = 0; i < self.length; i++) { T value = self[i]; if (predicate.invoke(i, value)) result++; } return result; } public static Map index(Class typeOfT, T[] self, Class typeOfK, Function key) { Map result = new HashMap<>(); for (T value : self) result.put(key.apply(value), value); return result; } }