Internal iteration as an idiom

You must be familiar with the Iterator pattern. It allows clients to sequentially access a collection, without knowing anything about its underlying representation. Iteration can be external or internal. In the first form, which is the most common, the client decides when to access the next element, what to do with it and when to stop the iteration:

// Java like pseudo-code. For simplicity, values in the collection are
// assumed to be integers.

int sum = 0;
// Iterate over a list of integers and
// find their sum.
Iterator it = list.iterator();
while (it.hasNext()) {
     sum += it.next();
}
print(sum);

The iterator object itself can be made to iterate over the sequence and perform an operation on each element. This is called internal iteration.

// An interface to specify operations on individual elements.
interface Operation {
    public void execute(int i);
}

// For internal iteration to work, the Iterator class
// should have a method that accepts an Operation and
// calls its execute(int) on each element.
class Iterator {
    // .....
    public void iterate(Operation opr) {
         while (hasNext()) {
             execute(next());
         }
    }
 }

Given the above definitions, it is easy to apply an Operation over a sequence and get back some aggregate result:

class Summation implements Operation {
    public void execute(int i) {
        sum += i;
    }

    public int getSum() {
        return sum;
    }

    private int sum;
}

// To find the sum of all ints in a list:
Operation s = new Summation();
list.iterator().iterate(s);
print(((Summation)s).getSum());

In the general OOP world, design patterns are considered arcane. Good knowledge and great care is required to implement the objects that comprise a pattern. Programmers need to be familiar with advanced language concepts like templates to implement patterns effectively. This is not the case with functional languages. Programmers who start with a functional language, learn most of the design patterns while mastering the basic idioms of the language itself and they do this unawares! To make the point clear, let us see how a programmer learning Scheme, one of the simplest of all functional languages, familiarizes himself with the iterator pattern, while learning two basic concepts of the language: higher-order functions and closures. Higher-Order functions are often introduced using map, which applies a procedure on all elements of a list. This is how the internal iterator we just saw, is implemented using a closure and map, in just a few lines of code:

;; Returns a closure that keeps an accumulated sum.
(define (make-accumulator)
  (let ((sum 0))
     (lambda (n)
       (set! sum (+ sum n))
       sum)))

 ;; Iterate a list and find the sum
 (define a (make-accumulator))
 (map a '(1 2 3 4 5))
 (a 0) ;; => 15