Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming Collection Extensions for clarity

Tags:

Introducing some of the goodness of collection operations to our codebase without adding a new external library dependency, we are adding these methods to our utility package.

static public List<T> filter(List<T> source, Predicate<T> filter);
static <Y,T> public List<Y> transform(List<T> source, Mutator<Y,T> filter);
static public boolean exists(List<T> source, Predicate<T> filter);
static public T findFirst(List<T> source, Predicate<T> filter);
static public boolean trueForAll(List<T> source, Predicate<T> filter);

With the attendant interfaces

public interface Predicate<T> { public boolean apply(T item); }
public interface Mutator<T,Y> { public Y apply(T item); }

So the questions:

  • Is Filters a good name for the class containing the extensions? If not, a better?
  • Is Mutator<T,Y> appropriately named?
  • Should I prefer map to transform and reduce to filter?
  • Are there any important set-based functions that I've forgotten to include in the library class?

Edited to add: A significant argument I have against map (and thus in favor of transform) is that map has significant semantic load due to the many uses for java.util.Map

like image 568
Tetsujin no Oni Avatar asked May 27 '09 17:05

Tetsujin no Oni


1 Answers

Are there any important set-based functions that I've forgotten to include in the library class?

For higher-order collection functions I use the approach outlined by Adrian Kuhn in his article "Pimp My Foreach".

Some of these you've already got, but thought I'd throw them out there anyway:

  • AllSatisfy
  • AnySatisfy
  • Cardinality
  • Collect
  • Count
  • CutPieces
  • Detect
  • Fold
  • GroupedBy
  • IndexOf
  • Inject
  • Reject
  • Select
like image 87
cwash Avatar answered Oct 11 '22 15:10

cwash