mfor: Multivariate For Loop

mforR Documentation

Multivariate For Loop

Description

mfor is a multivariate version of for.

Usage

mfor(...)

Arguments

...

arguments to mfor. The last argument (called expr) is the expression of the mfor loop (expression in a formal sense). This is either a simple expression or a so-called compound expression, usually of the form { expr1 ; expr2 }. The second last argument (called seqs) is the object being iterated over (a collection of sequences). Sequences are recycled as necessary. All other arguments (called vars) are syntactical names for variables.

Details

If exactly one variable is specified, the behaviour is similar to for. See section Examples.

break breaks out of an mfor loop, next halts the processing of the current iteration and advances the looping index; exactly the same as a for, while, or repeat loop.

The seqs in an mfor loop is evaluated at the start of the loop; changing it subsequently does affect the loop. If any sequence in seqs has length zero, the body of the loop is skipped. Otherwise, the variables vars are assigned in turn the elements of the sequences of seqs. You can assign to any of vars within the body of the loop, but this will not affect the next iteration. When the loop terminates, vars remains as the variables containing their last values.

Unlike for, the sequences will not be coerced to a vector or to a pairlist. Instead, their subsetting ([[), length, and lengths methods will be used.

Value

mfor returns NULL invisibly. It also sets vars to the last used values of seqs, or to NULL if any sequence was length zero.

See Also

for for the single variate for loop.

Examples

# when exactly one variable is specified,
# the behaviour is similar to 'for'
mfor(i, 1:5, print(1:i))


# 'mfor' works on classed objects, 'for' does not
mfor(date, Sys.time() + 0:9, print(date))


# sequences are recycled as necessary,
# with a warning for fractional recycling
mfor(i, j, k, list(1:4, 6:10, 11:15), {
    print(c(i = i, j = j, k = k))
})


# mfor works well with data frames
mfor(          col              , cex, main               ,
    data.frame(palette.colors(3), 1:3, paste("title", 1:3)),
    graphics::plot(x = 1:5, col = col, cex = cex, main = main,
        pch = 16)
)

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.