@Override public <C> Map<C, Array<T>> groupBy(Function<? super T, ? extends C> classifier) { return foldLeft( HashMap.empty(), (map, t) -> { final C key = classifier.apply(t); final Array<T> values = map.get(key).map(ts -> ts.append(t)).getOrElse(of(t)); return map.put(key, values); }); }
@Override public <U> Array<T> sortBy( Comparator<? super U> comparator, Function<? super T, ? extends U> mapper) { final Function<? super T, ? extends U> domain = Function1.of(mapper::apply).memoized(); return toJavaStream() .sorted((e1, e2) -> comparator.compare(domain.apply(e1), domain.apply(e2))) .collect(collector()); }
@Override public <U> Array<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); final Object[] arr = new Object[length()]; for (int i = 0; i < length(); i++) { arr[i] = mapper.apply(get(i)); } return wrap(arr); }
@Override public <U> Array<T> distinctBy(Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); final java.util.Set<U> seen = new java.util.HashSet<>(); return filter(t -> seen.add(keyExtractor.apply(t))); }