you could easily roll your own video based on your description:
public interface Reducer<A, T> { public A foldIn(A accum, T next); }
Then using the strategy template:
public class Reductor<A, T> { private Reducer<A, T> worker; public Reductor<A, T>(Reducer<A, T> worker) { this.worker = worker; } public A fold(A rval, Iterator<T> itr) { while(itr.hasNext()) { A rval = worker.foldIn(rval, itr.next()); } return rval; } }
I'm sure there are a ton of syntax errors, but the main thing (there are several options that you could make about how to get an empty battery value. Then, to use it on a particular iterator, just define your gear to fly:
Reductor r = new Reductor<A, T>(new Reducer<A, T>() { public A foldIn(A prev, T next) { A rval;
depending on how your iterator works, it can fold left or fold right while the iterator goes in the right direction.
hope this helps.
luke
source share