I want to define a Functor class in Java. This works:
//a Function
public interface F {
public R apply(A a);
}
public interface Functor {
Building on the answer of Sergey, I think I came close to what I wanted. Seems like I can combine his idea with my failed attempt:
public interface Functor> {
public > I fmap(F f);
}
public class ListFunctor implements Functor> {
final private List list;
public ListFunctor(List list) {
this.list = list;
}
@Override
public >> I fmap(F f) {
List result = new ArrayList();
for(A a: list) result.add(f.apply(a));
return (I) new ListFunctor(result);
}
}
List list = java.util.Arrays.asList("one","two","three");
ListFunctor fs = new ListFunctor(list);
ListFunctor fi = fs.>fmap(stringLengthF);
//--> [3,3,5]
The remaining problem is that I could write e.g. ListFunctor
without complaints from the compiler. At least I can look for a way to hide the ugly guts behind a static method, and to enforce that relation behind the scenes...