Skip to contents

Constructs an iteror that returns elements from an iterable following the given sequence with starting value start and ending value end. The sequence's step size is given by step.

Usage

i_slice(object, start = 1, end = NULL, step = 1, ...)

Arguments

object

iterable object through which this function iterates

start

the index of the first element to return from object

end

the index of the last element to return from object

step

the step size of the sequence

...

passed along to iteror(object, ...)

Value

iteror that returns object in sequence

Details

The iterable given in object is traversed beginning with element having index specified in start. If start is greater than 1, then elements from the object are skipped until start is reached. By default, elements are returned consecutively. However, if the step size is greater than 1, elements in object are skipped.

If stop is Inf (default), the iteration continues until the iteror is exhausted unless end is specified. In this case, end specifies the sequence position to stop iteration.

Originally from package itertools2.

Examples

it <- i_slice(1:5, start=2)
nextOr(it, NULL) # 2
#> [1] 2
nextOr(it, NULL) # 3
#> [1] 3
nextOr(it, NULL) # 4
#> [1] 4
nextOr(it, NULL) # 5
#> [1] 5

it2 <- i_slice(1:10, start=2, end=5)
unlist(as.list(it2)) == 2:5
#> [1] TRUE TRUE TRUE TRUE

it3 <- i_slice(1:10, start=2, end=9, step=2)
unlist(as.list(it3)) == c(2, 4, 6, 8)
#> [1] TRUE TRUE TRUE TRUE