Learn R Programming

functionals (version 0.5.0)

fwalk: Walk over a vector or list with side effects

Description

Applies a function `.f` to each element of `.x`, typically for its side effects (e.g., printing, writing files). This function is the side-effect-friendly equivalent of `fmap()`. Supports parallel execution and progress bar display.

Usage

fwalk(.x, .f, ncores = NULL, pb = FALSE, ...)

Value

Invisibly returns `.x`, like `purrr::walk()`.

Arguments

.x

A list or atomic vector of elements to iterate over.

.f

A function to apply to each element of `.x`. Should be called primarily for side effects.

ncores

Integer. Number of cores to use for parallel processing. Default is `NULL` (sequential).

pb

Logical. Whether to show a progress bar. Default is `FALSE`.

...

Additional arguments passed to `.f`.

Examples

Run this code
# Print each element
fwalk(1:3, print)

# Simulate writing files in parallel
# \donttest{
fwalk(1:3, function(i) {
  cat(paste("Processing item", i, "\n"))
  Sys.sleep(0.5)
}, ncores = 2, pb = TRUE)
# }

Run the code above in your browser using DataLab