interface Mapper<A, B> { B map(A a); } <A, B> Iterable<B> mapperConvert(Iterable<A> x, Mapper<A, B> m) { Collection<B> y = new LinkedList<B>(); for(A a : x) y.add(m.map(a)); return y; }
Problem resolved? LinkedList
?
What if we want to map something that is not only not Iterable
, but it makes no sense for it
to be Iterable
? i.e. we couldn't even implement the interface if we wanted to.
Should we concede and use dynamic typing — any object that supports the map
method? Or
perhaps concede Java's static type system and use reflection?
Perhaps we want to do a bit more, such as remove elements of a certain criteria as we map?