/*-*********************************************************************** TEMPLATE-ANALYSIS: return-type method-name(Traversal tr){ return-type acc = BASE-VALUE; for (return-type acc = BASE-VALUE; *** DO NOT INCLUDE - DONE ALREADY *** CONTINUATION-PREDICATE; tr = ADVANCE){ acc = UPDATE (CURREENT, acc); } return acc; } COMPLETE METHOD TEMPLATE: ------------------------- <T> return-type method-name(Traversal<T> tr){ +------------------------------+ | return-type acc = BASE-VALUE |; +------------------------------+ for (... no initialization is needed ...; +---------------+ | !tr.isEmpty() |; +---------------+ +--------------+ tr = | tr.getRest() |) +--------------+ { +----------------------------+ acc = | update(tr.getFirst(), acc) |; +----------------------------+ } return acc; } <T> return-type update(T t, return-type acc){ ... } *********************************************************************-*/
/** IMPERATIVE VERSION THAT USES for LOOP WITH THE Traversal. * Count how many data elements generated by the given traversal * satisfy the given ISelect predicate. */ // orMap with for loop and iterator public <T> boolean orMapFor(Traversal<T> tr, ISelect<T> choice){ // Define the accumulator and initialize it to the BASE-VALUE; boolean acc = false; // loop header: // for(... accumulator is already defined and initialized ... ; // continuation-predicate; // update) for(; !tr.isEmpty(); tr = tr.getRest()){ // loop body: uses current element acc = updateOrMap(tr.getFirst(), acc, choice); } // postmortem: produce the result return acc; }