Lesson contents

R — Post-test loops with repeat and break

R's repeat loop starts without a condition. Placing a break test after the work makes the body execute at least once.

A simple example

Print first, then test

  1. Start with n starts at 1
  2. Do this Print n, add 1
  3. Result Stop at 4
The first print happens before the stopping test. The loop prints 1, 2 and 3 on separate lines.
Lesson.R
n <- 1
repeat {
  cat(n, "\n", sep = "")
  n <- n + 1
  if (n > 3) break
}
Output
1
2
3
Advertisements

Poll until ready or the attempt limit

Count the first poll for every request, even if it is already ready. Stop after success or after the configured maximum number of attempts. The first poll increments attempts. The scalar || stops the loop on either success or exhaustion of the positive limit.

First check: Busy. Second check: Ready. Checks made: 2.
Check a printer until it is ready

A print desk checks the printer once before deciding whether to check again. If the printer answers ready on the second check, the loop stops there. A limit of three checks keeps an offline printer from holding up the desk.

Run the example

Use R 4.x. Run these commands from the directory containing the example.

Rscript --vanilla Lesson.R
Lesson.R
poll_count <- function(ready_on, limit) {
  attempts <- 0L
  repeat {
    attempts <- attempts + 1L
    if (attempts >= ready_on || attempts >= limit) break
  }
  attempts
}
for (case in list(c(5L, 1L), c(2L, 3L), c(8L, 3L))) {
  cat(poll_count(case[1], case[2]), "\n", sep = "")
}
Output
1
2
3

With one permitted attempt, the first case stops at 1. The second becomes ready on attempt 2. The last case never reaches readiness within its three-attempt limit.

Apply the loop to a retry queue

Simulate a job using prerecorded replies. T means a temporary failure, S means success, and X means permanent rejection. Process at most two replies per job. Stop on S or X and report how many replies were consumed.

First reply: Temporary failure. Next reply: Success. Attempts used: 2.
Handle a temporary print failure

A queue can retry a temporary failure once under a two-attempt limit. A temporary failure followed by success finishes the job. A permanent rejection stops the job immediately and leaves the next reply unused.

Run this example

Save this program as Lesson.R and run it separately.

Rscript --vanilla Lesson.R
Lesson.R
process <- function(responses, limit) {
  used <- 0L
  outcome <- "no response"
  for (response in head(strsplit(responses, "", fixed = TRUE)[[1]], limit)) {
    used <- used + 1L
    if (response == "S" || response == "X") {
      outcome <- if (response == "S") "sent" else "rejected"
      break
    }
    if (used == limit) outcome <- "limit reached"
  }
  list(outcome = outcome, attempts = used)
}

jobs <- c(A = "TS", B = "XS", C = "TTS", D = "")
for (id in names(jobs)) {
  result <- process(jobs[[id]], 2L)
  cat(sprintf("%s: %s (%d)\n", id, result$outcome, result$attempts))
}
Output
A: sent (2)
B: rejected (1)
C: limit reached (2)
D: no response (0)

Job A succeeds on its second reply. B stops at the rejection before its later success. C reaches the attempt limit with a success still unread. D has no recorded reply, so its count stays zero. Try raising the limit to three: only C changes, succeeding on the third reply.

Try it yourself

A request is ready on the first poll and allows three attempts. How many polls run?

Check your answer

Exactly one. The success check happens after the first poll. The example assumes a positive attempt limit.

R Language Definition

Related tags

  • R
  • post-test loops
  • retries
  • termination