/*-********************************************************************** TEMPLATE-ANALYSIS: return-type method-name(Traversal tr){ return-type acc = BASE-VALUE; while (CONTINUATION-PREDICATE){ acc = UPDATE (CURREENT, acc); tr = ADVANCE; } return acc; } COMPLETE METHOD TEMPLATE: ------------------------- <T> return-type method-name(Traversal<T> tr){ +------------------------------+ | return-type acc = BASE-VALUE |; +------------------------------+ +---------------+ while (| !tr.isEmpty() |) +---------------+ { +----------------------------+ acc = | update(tr.getFirst(), acc) |; +----------------------------+ +--------------+ tr = | tr.getRest() |; +--------------+ } return acc; } <T> return-type update(T t, return-type acc){ ... } *********************************************************************-*/
/** VERSION THAT USES THE while LOOP. * Count how many data elements generated by the given traversal * satisfy the given ISelect predicate. */ // orMap with while loop and iterator public <T> boolean orMapWhile(Traversal<T> tr, ISelect<T> choice){ // preamble: Define accumulator, initialize it to the BASE-VALUE boolean acc = false; // loop header: while(continuation-predicate) while(!tr.isEmpty()){ // loop body: update acc = updateOrMap(tr.getFirst(), acc, choice); // loop advance: tr = tr.getRest(); } // postmortem: produce the result return acc; }