imap: Iterator that applies a given function to several iterables...

Description Usage Arguments Details Value Examples

Description

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

Usage

1
imap(f, ...)

Arguments

f

a function

...

multiple arguments to iterate through in sequence

Details

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

The primary difference between istarmap and imap 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.

Value

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
pow <- function(x, y) {
  x^y
}
it <- imap(pow, c(2, 3, 10), c(5, 2, 3))
as.list(it)

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

# Another similar example but with lists instead of vectors
it3 <- imap(pow, list(2, 3, 10), list(5, 2, 3))
iterators::nextElem(it3) # 32
iterators::nextElem(it3) # 9
iterators::nextElem(it3) # 1000

Example output

[[1]]
[1] 32

[[2]]
[1] 9

[[3]]
[1] 1000

[[1]]
[1] 32

[[2]]
[1] 9

[1] 32
[1] 9
[1] 1000

itertools2 documentation built on May 2, 2019, 3:37 p.m.