accumulate_while: Accumulate a list by iteratively performing a function

Description Usage Arguments Details Examples

View source: R/accumulate-while.R

Description

Perform a function to transform an object multiple times, until a predicate is not satisfied.

Usage

1
accumulate_while(.x, .f, .p, ..., .compare = FALSE, .max = 1e+05)

Arguments

.x

Starting value

.f

Function to apply to transform each step to the next

.p

A predicate applied to each intermediate step: if false, it stops the iteration

...

Extra arguments passed to .f

.compare

Whether the predicate will be given the last two steps rather than just the most recent.

.max

Maximum number of iterations to perform. Give Inf to continue growing the vector as long as necessary.

Details

I've suggested a function like this in an issue on purrr: https://github.com/hadley/purrr/issues/253. I've put it here so I can use it personally in the meantime. If it gets added to purrr, I will remove it from drlib.

Examples

1
2
3
4
accumulate_while(1, ~ . + 1, ~ . < 5)

# accumulate until difference is within some tolerance
accumulate_while(1, ~ . / 2, ~ abs(.x - .y) > 1e-4, .compare = TRUE)

dgrtwo/drlib documentation built on Oct. 11, 2019, 6:42 a.m.