Learn R Programming

functionals (version 0.5.0)

floop: Functional loop with optional parallelism and progress bar

Description

`floop()` applies a function `.f` to each element of `.x`, optionally in parallel, and with an optional progress bar. Unlike `fwalk()`, it can return results or be used purely for side effects (like a for-loop).

Usage

floop(.x, .f, ncores = 1, pb = FALSE, .capture = TRUE, ...)

Value

A list of results if `.capture = TRUE`, otherwise returns `.x` invisibly.

Arguments

.x

A vector or list of elements to iterate over.

.f

A function to apply to each element of `.x`.

ncores

Integer. Number of cores to use. Default is 1 (sequential).

pb

Logical. Show a progress bar? Default is `FALSE`.

.capture

Logical. Should results of `.f` be captured and returned? If `FALSE`, acts like a side-effect loop.

...

Additional arguments passed to `.f`.

Examples

Run this code
# Functional loop that collects output
floop(1:3, function(i) i^2)

# Side-effect only loop (like for-loop with cat)
# \donttest{
floop(1:5, function(i) cat(" Processing", i, "\n"), pb = TRUE, .capture = FALSE)
# }

Run the code above in your browser using DataLab