If you really want to play with a functional approach to solving this problem, you could implement a functor for Iterator
, and map a function that formats a cell type over your rowIterator
.
Unary function interface:
public interface Fn1<A,B> {
B apply(A a);
}
A functor for Iterator
:
public class IteratorFunctor<A> {
public final Iterator<A> i;
private IteratorFunctor(Iterator<A> _i) {
i = _i;
}
public <B> IteratorFunctor<B> map(Fn1<A,B> f) {
List<B> bs = new LinkedList<B>();
while (i.hasNext()) {
bs.add(f.apply(i.next()));
}
return apply(bs.iterator());
}
public static <A> IteratorFunctor<A> apply(Iterator<A> _i) {
return new IteratorFunctor<A>(_i);
}
}
A format function (for you, this would take a Cell
and return perhaps a String
):
public static String format(int x) {
switch (x) {
case 1:
return "one";
case 2:
return "two";
default:
return "big";
}
}
Demo:
Fn1<Integer,String> formatFn = new Fn1<Integer,String>() {
public String apply(Integer x) {
return format(x);
}
};
List<Integer> xs = Arrays.asList(1,2,3,4);
IteratorFunctor<Integer> fi = IteratorFunctor.apply(xs.iterator());
IteratorFunctor<String> fs = fi.map(formatFn);
Iterator<String> ss = fs.i;
int i = 0;
while (ss.hasNext()) {
System.out.println(i + ": " + ss.next());
i = i + 1;
}
// Prints the following to stdout:
//
// 0: one
// 1: two
// 2: big
// 3: big
//