Skip to contents

Constructs an iterator that computes the given function f using the arguments from each of the iterables given in ....

Usage

i_map(f, ...)

Arguments

f

a function

...

multiple arguments to iterate through in sequence

Value

iterator that returns the values of object along with the index of the object.

Details

The iterator returned is exhausted when the shortest iterable in ... is exhausted. Note that i_map does not recycle arguments as Map does.

The primary difference between i_starmap and i_map is that the former expects an iterable object whose elements are already grouped together, while the latter case groups the arguments together before applying the given function. The choice is a matter of style and convenience.

Examples

pow <- function(x, y) {
  x^y
}
it <- i_map(pow, c(2, 3, 10), c(5, 2, 3))
as.list(it)
#> [[1]]
#> [1] 32
#> 
#> [[2]]
#> [1] 9
#> 
#> [[3]]
#> [1] 1000
#> 

# Similar to the above, but because the second vector is exhausted after two
# calls to `nextElem`, the iterator is exhausted.
it2 <- i_map(pow, c(2, 3, 10), c(5, 2))
as.list(it2)
#> [[1]]
#> [1] 32
#> 
#> [[2]]
#> [1] 9
#> 

# Another similar example but with lists instead of vectors
it3 <- i_map(pow, list(2, 3, 10), list(5, 2, 3))
nextOr(it3, NA) # 32
#> [1] 32
nextOr(it3, NA) # 9
#> [1] 9
nextOr(it3, NA) # 1000
#> [1] 1000