Iterator Design Pattern in R

1. Definition

The Iterator Design Pattern provides a way to access elements of a collection object in sequential manner without any need to know its underlying representation. It decouples the collection from the iteration logic.

2. Problem Statement

Imagine you have a collection of items, such as a list or a vector in R, and you want to provide a standardized way to traverse its elements without exposing its underlying representation. Moreover, you may need to support different types of traversals for the same collection.

3. Solution

The Iterator pattern suggests separating the traversal logic from the collection. Create a separate iterator object that knows how to iterate through the collection without the client knowing about the underlying representation.

4. Real-World Use Cases

1. Navigating through different types of data structures like lists, vectors, trees.

2. Pagination in databases or user interfaces.

3. Implementing various traversal algorithms for the same data structure.

5. Implementation Steps

1. Create an Iterator interface that defines methods like has_next() and next().

2. Create concrete Iterator classes for specific collection types.

3. Optionally, define a Container interface to return the specific Iterator.

6. Implementation in R Programming

# Step 1: Iterator Interface
Iterator <- setRefClass("Iterator", methods = list(
  has_next = function() {},
  next = function() {}
# Step 2: Concrete Iterator for a Vector
VectorIterator <- setRefClass("VectorIterator", contains = "Iterator",
  fields = list(data = "numeric", index = "integer"),
  methods = list(
    has_next = function() {
      index <= length(data)
    next = function() {
      item <- data[index]
      index <<- index + 1
# Step 3: Container Interface
Container <- setRefClass("Container", methods = list(get_iterator = function() {}))
# Concrete Container for a Vector
VectorContainer <- setRefClass("VectorContainer", contains = "Container",
  fields = list(data = "numeric"),
  methods = list(
    get_iterator = function() {
      VectorIterator$new(data = data, index = 1)
# Client Code
vec <- c(1, 2, 3, 4, 5)
container <- VectorContainer$new(data = vec)
iterator <- container$get_iterator()
while (iterator$has_next()) {
  cat(iterator$next(), "\n")




1. We begin by defining an abstract Iterator interface with methods has_next and next.

2. A concrete VectorIterator class is then implemented, which knows how to iterate over vectors in R.

3. The Container interface provides a standardized method (get_iterator) to retrieve the iterator.

4. VectorContainer is a concrete container for vectors that returns a VectorIterator.

5. In the client code, the iterator abstracts away the details of the vector, allowing a uniform way of accessing its elements.

7. When to use?

Use the Iterator Design Pattern when:

1. You want to provide a uniform way to traverse different data structures.

2. You want to separate the traversal logic from the collection itself.

3. You need to support different traversal mechanisms for a single collection without changing its code.

The Iterator pattern is fundamental in languages that emphasize the importance of collections, and R, being a data-centric language, benefits significantly from it.