Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:Fast, Compact Iterators and Tools
Version:1.0
Maintainer:Peter Meilstrup <peter.meilstrup@gmail.com>
Description:A fresh take on iterators in R. Designed to be cross-compatible with the 'iterators' package, but using the 'nextOr' method will offer better performance as well as more compact code. With batteries included: includes a collection of iterator constructors and combinators ported and refined from the 'iterators', 'itertools', and 'itertools2' packages.
Depends:R (≥ 3.6)
Imports:rlang
Encoding:UTF-8
Suggests:reticulate, combinat, testthat (≥ 3.0.0), foreach, iterators(≥ 1.0.7), rmarkdown, knitr
URL:https://github.com/crowding/iterors,https://crowding.github.io/iterors/
License:GPL (≥ 3)
RoxygenNote:7.2.3
VignetteBuilder:knitr
Config/testthat/edition:3
Config/testthat/parallel:false
NeedsCompilation:no
Packaged:2023-05-18 02:53:41 UTC; peter
Author:Peter Meilstrup [cre, aut, cph], Folashade Daniel [aut], Revolution Analytics [aut, cph], Steve Weston [aut, cph], John A. Ramey [aut, cph], Kayla Schaefer [aut], Hadley Wickham [aut]
Repository:CRAN
Date/Publication:2023-05-18 08:40:02 UTC

Collect all (or some given number of) values from an iteror,returning a vector of the given type.

Description

Collect all (or some given number of) values from an iteror,returning a vector of the given type.

Usage

## S3 method for class 'iteror'as.list(x, n = as.integer(2^31 - 1), ...)## S3 method for class 'iteror'as.double(x, n = as.integer(2^31 - 1), ...)## S3 method for class 'iteror'as.numeric(x, n = as.integer(2^31 - 1), ...)## S3 method for class 'iteror'as.logical(x, n = as.integer(2^31 - 1), ...)## S3 method for class 'iteror'as.character(x, n = as.integer(2^31 - 1), ...)## S3 method for class 'iteror'as.vector(x, mode)

Arguments

x

an iterable object

n

the maximum number of elements to return.

...

Unused arguments will throw an error.

mode

What mode to use for the output vector.

Value

The returned value will ben elements long if theiterator did not stop.

See Also

concat take


Concatenate contents of multiple iterators into a vector.

Description

concat collects all values from an iterable object, and pastesthem end to end into one vector. In other wordsconcat is toas.list.iteror asc is tolist.

Usage

concat(obj, mode = "list", n = Inf, ...)## Default S3 method:concat(obj, mode = "list", n = as.integer(2^31 - 1), ...)## S3 method for class 'iteror'concat(obj, mode = "list", n = Inf, length.out = Inf, ...)

Arguments

obj

An iteror.

mode

The mode of vector to return.

n

The maximum number of times to callnextOr(obj).

...

passed along toiteror constructor.

length.out

The target size of the output vector (afterresults have been pasted together). If the iteror ends (or emitsn results) before emitting this many elements, the result will beshorter thanlength.out. If the iterator does not end early, the outputwill have at leastlength.out elements, and possibly more,as the entire last chunk will be included.

Value

a vector with modemode.

Examples

it <- i_apply(icount(), seq_len) # [1], [1, 2], [1, 2, 3], ...concat(it, n=4, mode="numeric")  # [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]concat(it, length.out=4, mode="numeric")  # [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]

Consumes the first n elements of an iterator

Description

Advances the iterator n-steps ahead without returning anything.

Usage

consume(obj, n = Inf, ...)## S3 method for class 'iteror'consume(obj, n = Inf, ...)

Arguments

obj

an iterable object

n

The number of elements to consume.

...

passed along toiteror constructor.

Value

obj, invisibly.

See Also

take collect

Examples

it <- iteror(1:10)# Skips the first 5 elementsconsume(it, n=5)# Returns 6nextOr(it, NA)it2 <- iteror(letters)# Skips the first 4 elementsconsume(it2, 4)# Returns 'e'nextOr(it2, NA)

Consumes an iterator and computes its length

Description

Counts the number of elements in an iterator. NOTE: The iterator is consumedin the process.

Usage

count(object, ...)

Arguments

object

an iterable object

...

passed along toiteror constructor.

Value

the number of elements in the iterator

See Also

take consume as.list.iteror

Examples

count(1:5) == length(1:5)it <- iteror(1:5)count(it) == length(1:5)it2 <- i_chain(1:3, 4:5, 6)count(it2)it3 <- i_chain(1:3, levels(iris$Species))count(it3)

Computes the dot product of two iterable objects.

Description

Returns the dot product of two numeric iterables of equal length

Usage

dotproduct(vec1, vec2)

Arguments

vec1

the first

vec2

the second iterable object

Value

the dot product of the iterators

Examples

it <- iteror(1:3)it2 <- iteror(4:6)dotproduct(it, it2) # 32it <- iteror(1:4)it2 <- iteror(7:10)dotproduct(1:4, 7:10) # 90

Does This Iterator Have A Next Element

Description

wrapped <- ihasnext(obj) wraps aniteror object with theihasNext class. ThenhasNext(wrapped) will indicate if theiterator has another element.

Usage

hasNext(obj, ...)ihasNext(obj, ...)

Arguments

obj

an iterable

...

extra arguments may be passed along toiteror.

Details

A classihasNext was introduced in theitertoolspackage to try to reduce the boilerplate around extracting thenext value usingiterators::nextElem.ihasNext is includediniterors for backward compatibility with iterator code; however,it is less needed when using thenextOr iteration method, as you candirectly give an action to take at end of iteration.

Value

Logical value indicating whether the iterator has a nextelement.

Examples

# The bad old style of consuming an iterator in a loop with `nextElem`:  it <- ihasNext(iteror(c('a', 'b', 'c')))  tryCatch(repeat {    print(iterators::nextElem(it))  }, error=function(err) {    if (conditionMessage(err) != "StopIteration")      stop(err)  })# with ihasNext, this became:  it <- ihasNext(iteror(c('a', 'b', 'c')))  while (hasNext(it))    print(iterators::nextElem(it))# But using `nextOr` all you need is:  iteror(c('a', 'b', 'c'))  repeat print(nextOr(it, break))

Iterators returning distant random-number seeds.

Description

TheiRNGStream creates a sequence of random number seedsthat are very "far apart" (2^127 steps) in the overall randomnumber sequence, so that each can be used to make a parallel,psudo-independent random iterator. This usesparallel::nextRNGStream and the "L'Ecuyer-CMRG" generator.

Usage

iRNGStream(seed)

Arguments

seed

Either a single number to be passed toset.seed or a

Details

iRNGSubStream creates seeds that are somewhat less far apart (2^76steps), which might be used as "substream" seeds.

Originally from theitertools package.

Value

Aniteror which produces seed values.vector to be passed tonextRNGStream ornextRNGSubStream.

Aniteror which yields successive seed values.

References

For more details on the L'Ecuyer-CMRG generator, seevignette("parallel", package="parallel").

See Also

set.seed,nextRNGStream,nextRNGSubStream

Examples

global.seed <- .Random.seedrng.seeds <- iRNGStream(313)print(nextOr(rng.seeds))print(nextOr(rng.seeds))# create three pseudo-independent and# reproducible random number streamsit1 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))it2 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))it3 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))all(.Random.seed == global.seed)take(it1, 5, "numeric") # 0 0 0 1 1take(it2, 5, "numeric") # 0 1 1 1 1take(it3, 5, "numeric") # 1 1 1 0 0# none of this affects the global seedall(global.seed == .Random.seed)# Compute random numbers in three parallel processes with three# well-separated seeds. Requires package "foreach"library(foreach)foreach(1:3, rseed=iRNGSubStream(1970), .combine='c') %dopar% {  RNGkind("L'Ecuyer-CMRG") # would be better to initialize workers only once  assign('.Random.seed', rseed, pos=.GlobalEnv)  runif(1)}

Apply a function to each element of an iterator.

Description

i_apply(obj, f) returns the iteror that appliesf toeach element of the given iterableobj. It is an iteratorequivalent oflapply.

Usage

i_apply(obj, f, ...)

Arguments

obj

an iterable.

f

a function

...

Additional arguments will be passed along tof

Value

An iteror.

See Also

To apply a function of multiple arguments to multipleiterators, seei_map. To split an array over margins (likeiterators::i_apply useiteror(obj, by=MARGIN


Create an iterator that can be told to stop.

Description

Create an iterator that iterates over another iterator until a specifiedfunction returnsFALSE. This can be useful for breaking out of aforeach loop, for example.

Usage

i_break(iterable, finished, ...)

Arguments

iterable

Iterable to iterate over.

finished

Function that returns a logical value. The iterator stopswhen this function returnsFALSE.

...

Further arguments forwarded toiteror.

Details

Originally from theitertools package.

Value

an iteror which will stop whenfinished() isTRUE

Examples

# See how high we can count in a tenth of a secondmkfinished <- function(time) {  starttime <- proc.time()[3]  function() proc.time()[3] > starttime + time}length(as.list(i_break(icount(), mkfinished(0.1))))

Combine an iterator's values into chunks.

Description

Create an iterator that issues lists of values from the underlying iterable.This is useful for manually “chunking” values from an iterable.

Usage

i_chunk(iterable, size, mode = "list", fill, ...)

Arguments

iterable

Iterable to iterate over.

size

Maximum number of values fromiterable to returnin each value issued by the resulting iterator.

mode

Mode of the objects returned by the iterator.

fill

Value to use to pad the last chunk to size, if it isshort. If missing, no padding will be done.

...

Further arguments will be forwarded toiteror(iterable, ...).

Value

an iteror that yields items of lengthsize and modemode.

See Also

iteror.default

Argumentsize does not need to be an integer, for instance achunk of 3.5 will produce chunks of sizes 3 and 4alternating. The precise behavior will be subject to floatingpoint precision.

Examples

# Split the vector 1:10 into "chunks" with a maximum length of threeit <- i_chunk(1:10, 3)repeat print(unlist(nextOr(it, break)))# Same as previous, but return integer vectors rather than listsit <- i_chunk(1:10, 3, mode='integer')repeat print(unlist(nextOr(it, break)))it <- i_chunk(iterators::iter(1:5), 2, fill=NA)# List: list(1, 2, 3)nextOr(it, NULL)# List: list(4, 5, NA)nextOr(it, NULL)it2 <- i_chunk(levels(iris$Species), 4, fill="weeee")# Returns: list("setosa", "versicolor", "virginica", "weeee")nextOr(it2, NA)

Iteror that chains multiple arguments together into a single iterator

Description

i_concat(obj) takes an iterable that returnsiterables, and chains together all inner values of iterables intoone iterator. Analogous tounlist(recursive=FALSE).

i_chain for iterators is analogous toc() on vectors.i_chainconstructs aniteror that returns elements from the firstargument until it is exhausted, then elements from the nextargument, and so on until all arguments have been exhausted.

Usage

i_concat(obj, ...)i_chain(...)

Arguments

obj

an iterable.

...

multiple iterable arguments

Value

iteror that iterates through each argument in sequence

Author(s)

Peter Meilstrup

Examples

it <- i_chain(1:3, 4:5, 6)as.list(it)it2 <- i_chain(1:3, levels(iris$Species))as.list(it2)

Iterator that drops elements until the predicate function returns FALSE

Description

Constructs an iterator that drops elements from the iterableobject aslong as thepredicate function is true; afterwards, every element ofiterable object is returned.

Usage

i_dropwhile(object, predicate, ...)

Arguments

object

an iterable object

predicate

a function that determines whether an element isTRUEorFALSE. The function is assumed to take only one argument.

...

Further arguments forwarded toiteror.

Details

Because the iterator does not return any elements until thepredicatefirst becomes false, there may have a lengthy start-up time before elementsare returned.

Value

Aniteror object.

Examples

# Filters out numbers exceeding 3not_too_large <- function(x) {  x <= 3}it <- i_dropwhile(1:8, not_too_large)as.list(it)# Same approach but uses an anonymous functionit2 <- i_dropwhile(seq(2, 20, by=2), function(x) x <= 10)as.list(it2)

Iterator that returns the elements of an object along with their indices

Description

Constructs an iterator that returns the elements of an object along with eachelement's indices. Enumeration is useful when looping through anobject and a counter is required.

Thei_enumerate method for arrays allows splitting anarray by arbitrary margins, including by multiple margins. Theindex element returned will be a vector (or if chunking is used, amatrix) of indices.

Usage

i_enumerate(obj, ...)ienumerate(obj, ...)## Default S3 method:i_enumerate(obj, ..., recycle = FALSE, chunkSize, chunks)i_enum(obj, ...)## S3 method for class 'array'i_enumerate(  obj,  ...,  recycle = FALSE,  chunkSize,  chunks,  by = c("cell", "row", "column"),  rowMajor = TRUE,  drop = FALSE)

Arguments

obj

object to return indefinitely.

...

Undocumented.

recycle

Whether to restart the iterator after finishing thearray.

chunkSize

How large a chunk to take along the specifieddimension.

chunks

How many chunks to divide the array into.

by

Which array margins to iterate over. Can be "row", "col", "cell",or a vector of numerical indices.

rowMajor

If TRUE, the first index varies fastest, if FALSE, the last index varies fastest.

drop

Whether to drop marginalized dimensions. If chunking isused, this has no effect.

Details

This function is intended to follow the convention used in Python'senumerate function where the primary difference is that a list isreturned instead of Python'stuple construct.

Each call tonextElem returns a list with twoelements:

index:

a counter

value:

the current value ofobject

i_enum is an alias toi_enumerate to save a few keystrokes.

First appeared in packageiterators2.

These are two closely closely related functions:i_enumerate accepts an iterable, and will only emit a singleindex starting with 1.ienumerate is a generic with methods forvectors and arrays, supporting all chunking and recyclingoptions, and returning multiple indices for arrays.

Value

iterator that returns the values ofobj along with theindex of the object.

Author(s)

Peter Meilstrup

Examples

set.seed(42)it <- i_enumerate(rnorm(5))as.list(it)# Iterates through the columns of the iris data.frameit2 <- i_enum(iris)nextOr(it2, NA)nextOr(it2, NA)nextOr(it2, NA)nextOr(it2, NA)nextOr(it2, NA)a <- array(1:27, c(3, 3, 3))as.list(i_enumerate(a, by=c(1, 2), drop=TRUE))as.list(i_enumerate(a, by=c(3), drop=FALSE))as.list(i_enumerate(a, by=c(2, 3), chunkSize=7))

Iterator that filters elements not satisfying a predicate function

Description

i_keep(iterable, predicate) constructs an iterator that filterselements from iterable returning only those for which the predicateisTRUE.

Usage

i_keep(iterable, predicate, ...)i_drop(iterable, predicate, ...)

Arguments

iterable

an iterable object.

predicate

a function that determines whether an element isTRUE orFALSE. The function is assumed to take onlyone argument.

...

passed along toiteror constructor.

Details

Originally called 'ifilter' from packageitertools. Renamed because the order of arguments has changedto put the iterable in the first argument, the better to be usedwith the⁠|>⁠ operator.

Value

iterator object

See Also

i_drop i_keepwhile i_dropwhile

Examples

# Filters out odd numbers and retains only even numbersis_even <- function(x) {  x %% 2 == 0}it <- i_keep(1:10, is_even)as.list(it)# Similar idea here but anonymous function is used to retain only odd# numbersit2 <- i_drop(1:10, function(x) x %% 2 == 0)nextOr(it2, NA) # 1nextOr(it2, NA) # 3nextOr(it2, NA) # 5nextOr(it2, NA) # 7nextOr(it2, NA) # 9is_vowel <- function(x) {  x %in% c('a', 'e', 'i', 'o', 'u')}it3 <- i_keep(letters, is_vowel)as.list(it3)# Filters out even numbers and retains only odd numbersis_even <- function(x) {  x %% 2 == 0}it <- i_drop(1:10, is_even)as.list(it)# Similar idea here but anonymous function is used to filter out odd# numbersit2 <- i_drop(1:10, function(x) x %% 2 == 1)as.list(it2)is_vowel <- function(x) {  x %in% c('a', 'e', 'i', 'o', 'u')}it3 <- i_drop(letters, is_vowel)nextOr(it3, NA) # bnextOr(it3, NA) # cnextOr(it3, NA) # dnextOr(it3, NA) # fnextOr(it3, NA) # g# nextOr(it, NA) continues through the rest of the consonants

Iterator that returns elements while a predicate function returns TRUE

Description

Constructs an iterator that returns elements from an iterableobjectas long as the givenpredicate function returnsTRUE.

Usage

i_keepwhile(object, predicate, ...)

Arguments

object

an iterable object

predicate

a function that determines whether an element isTRUEorFALSE. The function is assumed to take only one argument.

...

passed along toiteror(object, ...)

Value

iterator object

Examples

# Filters out numbers exceeding 5not_too_large <- function(x) {  x <= 5}it <- i_keepwhile(1:100, not_too_large)unlist(as.list(it)) == 1:5# Same approach but uses an anonymous functionit2 <- i_keepwhile(seq(2, 100, by=2), function(x) x <= 10)unlist(as.list(it2)) == c(2, 4, 6, 8, 10)

Limit the length of an iterator.

Description

Create an iterator that limits the specified iterable to a specified number of items.

Usage

i_limit(iterable, n, ...)

Arguments

iterable

Iterable to iterate over.

n

Maximum number of values to return.

...

Extra arguments foriteror(iterable, ...)

Details

Originally from theitertools package.

Value

aniteror which will stop after yieldingn values.

Examples

# Limit icount to only return three valuesas.list(i_limit(icount(), 3))

Iterator that applies a given function to several iterables concurrently.

Description

Constructs an iterator that computes the given functionf using thearguments from each of the iterables given in....

Usage

i_map(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 thati_map does not recycle arguments asMap does.

The primary difference betweeni_starmap andi_map is that the former expects an iterable objectwhose elements are already grouped together, while the latter case groups thearguments together before applying the given function. The choice is a matterof style and convenience.

Value

iterator that returns the values ofobject along with theindex of the object.

Examples

pow <- function(x, y) {  x^y}it <- i_map(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 <- i_map(pow, c(2, 3, 10), c(5, 2))as.list(it2)# Another similar example but with lists instead of vectorsit3 <- i_map(pow, list(2, 3, 10), list(5, 2, 3))nextOr(it3, NA) # 32nextOr(it3, NA) # 9nextOr(it3, NA) # 1000

Iterator that filters elements where corresponding selector is false.

Description

Constructs an iterator that filters elements from iterable returning onlythose for which the corresponding element fromselectors isTRUE.

Usage

i_mask(object, selectors)

Arguments

object

an iterable object

selectors

an iterable that determines whether the correspondingelement inobject is returned.

Details

The iterator stops when eitherobject orselectors has beenexhausted.

Value

iterator object

Examples

# Filters out odd numbers and retains only even numbersn <- 10selectors <- rep(c(FALSE, TRUE), n)it <- i_mask(seq_len(n), selectors)as.list(it)# Similar idea here but anonymous function is used to filter out even# numbersn <- 10it2 <- i_mask(seq_len(10), rep(c(TRUE, FALSE), n))as.list(it2)it3 <- i_mask(letters, letters %in% c('a', 'e', 'i', 'o', 'u'))as.list(it3)

Iterator that returns an object followed indefinitely by a fill value

Description

Constructs an iterator that returns an iterableobject before paddingthe iterator with the givenfill value indefinitely.

Usage

i_pad(object, fill = NA, ...)

Arguments

object

an iterable object

fill

the value to pad the indefinite iterator after the initialobject is consumed. Default:NA

...

Passed along toiteror constructor.

Value

iterator that returnsobject followed indefinitely by thefill value

Examples

it <- iteror(1:9)it_i_pad <- i_pad(it)as.list(i_slice(it_i_pad, end=9)) # Same as as.list(1:9)it2 <- iteror(1:9)it2_i_pad <- i_pad(it2)as.list(i_slice(it2_i_pad, end=10)) # Same as as.list(c(1:9, NA))it3 <- iteror(1:9)it3_i_pad <- i_pad(it3, fill=TRUE)as.list(i_slice(it3_i_pad, end=10)) # Same as as.list(c(1:9, TRUE))

Create a recycling iterator

Description

Create an iterator that recycles a specified iterable. On the firstrepeat the iterable is buffered into memory until it finishes, thenwe repeat the same sequence of values.

Usage

i_recycle(iterable, times = Inf, ...)

Arguments

iterable

The iterable to recycle.

times

integer. Number of times to recycle the values .Default value ofInf means to recycle indefinitely.

...

Further arguments will be passed along toiteror.

Details

Originally from theitertools package.

Value

aniteror recycling the values from the underlying iterable.

Examples

# Recycle over 'a', 'b', and 'c' three timesi <- i_recycle(letters[1:3], 3)as.character(i)it <- i_recycle(1:3)nextOr(it, NA) # 1nextOr(it, NA) # 2nextOr(it, NA) # 3nextOr(it, NA) # 1nextOr(it, NA) # 2nextOr(it, NA) # 3nextOr(it, NA) # 1it2 <- i_recycle(1:3, times=2)as.list(it2)

Repeat values from an iterator.

Description

An analogue of therep function operating on iterables.

Usage

i_rep(iterable, times = 1, length.out = NULL, each = 1, ...)

Arguments

iterable

The iterable to iterate over repeatedly.

times

How many times to recycle the underlying iteror (viai_recycle).

length.out

The maximum length of output. If this is giventimes is ignored.

each

The number of times to repeat each element. You canpass a vector (recycled), or another iterable, to repeat eachelement a varying number of times.

...

further arguments passed along toiteror(iterable, ...)

Details

Note that argumentstimes andeach can work slightlydifferently fromrep;times must always be of length 1; torepeat each element a specific number of times, provide a vector toeach rather thantimes.

Originally from theitertools package.

Value

an iteror yilding and repeating values fromiterable.

See Also

base::rep,i_recycle

Examples

as.numeric(i_rep(1:4, 2))as.numeric(i_rep(1:4, each=2))as.numeric(i_rep(1:4, each=c(2,2,2,2)))as.numeric(i_rep(1:4, each=c(2,1,2,1)))as.numeric(i_rep(1:4, each=2, len=4))as.numeric(i_rep(1:4, each=2, len=10))as.numeric(i_rep(1:4, each=2, times=3))# Note `rep` makes `times` behave like `each` when given a vector.# `i_rep` does not reproduce this behavior; give the vector to `each`.# These are equivalent:as.numeric(i_rep(1:4, each = 1:8, times=2))rep(rep(1:4, times=2), times=1:8)

Create a repeating iterator

Description

Create an iterator that returns a value a specified number of times.

Usage

i_repeat(x, times)

Arguments

x

The value to return repeatedly.

times

The number of times to repeat the value. Default value isinfinity.

Details

Originally from theitertools package.

Value

aniteror.

Examples

# Repeat a value 10 timesunlist(as.list(i_repeat(42, 10)))

Run-length encoding iterator.

Description

This is an iterator equivalent ofrle; it produces one outputvalue for each run if identical values in its input, along with thelenght of the run.i_rle_inverse() performs the inversetransformstion.

Usage

i_rle(obj, cmp = identical, ...)i_rleinv(obj, ...)

Arguments

obj

An iterable

cmp

A function to use for comparison. It should take twoarguments and returnTRUE orFALSE.

...

further arguments forwarded toiteror(obj, ...).

Value

An iterator returning entries of the formlist(length=n, value=X).

i_rleinv recreates the original data from the output ofi_rle.

Author(s)

Peter Meilstrup

See Also

i_dedupe

Examples

it <- isample(c(TRUE, FALSE), 1, replace=TRUE)rle <- i_rle(it)x <- take(rle, 10)as.logical(i_rleinv(x))

Iteror that traverses each given iterable in a roundrobin order

Description

Constructs an iterator that traverses each given iterable in a roundrobinorder. That is, the iterables are traversed in an alternating fashion suchthat the each element is drawn from the next iterable. If an iterable has nomore available elements, it is skipped, and the next element is taken fromthe next iterable having available elements.

Usage

i_roundrobin(...)

Arguments

...

multiple arguments to iterate through in roundrobin sequence

Value

iterator that alternates through each argument in roundrobin sequence

Examples

it <- iteror(c("A", "B", "C"))it2 <- iteror("D")it3 <- iteror(c("E", "F"))as.list(i_roundrobin(it, it2, it3)) # A D E B F Cit_rr <- i_roundrobin(1:3, 4:5, 7:10)as.list(it_rr) # 1 4 7 2 5 8 3 9 10

Iteror that returns selected elements from an iterable.

Description

Constructs an iteror that returns elements from an iterable following thegiven sequence with starting valuestart and ending valueend.The sequence's step size is given bystep.

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 fromobject

end

the index of the last element to return fromobject

step

the step size of the sequence

...

passed along toiteror(object, ...)

Details

The iterable given inobject is traversed beginning with elementhaving index specified instart. Ifstart is greater than 1,then elements from theobject are skipped untilstart isreached. By default, elements are returned consecutively. However, if thestep size is greater than 1, elements inobject are skipped.

Ifstop isInf (default), the iteration continues until theiteror is exhausted unlessend is specified. In this case,end specifies the sequence position to stop iteration.

Originally from packageitertools2.

Value

iteror that returnsobject in sequence

Examples

it <- i_slice(1:5, start=2)nextOr(it, NULL) # 2nextOr(it, NULL) # 3nextOr(it, NULL) # 4nextOr(it, NULL) # 5it2 <- i_slice(1:10, start=2, end=5)unlist(as.list(it2)) == 2:5it3 <- i_slice(1:10, start=2, end=9, step=2)unlist(as.list(it3)) == c(2, 4, 6, 8)

Iteror that applies a given function to the elements of an iterable.

Description

Constructs an iteror that applies the functionf concurrently to theelements within the listx.

Usage

i_starmap(f, x)i_star(f, x)

Arguments

f

a function to apply to the elements ofx

x

an iterable object

Details

The iteror returned is exhausted when the shortest element inxis exhausted. Note thati_starmap does not recycle arguments asMap does.

The primary difference betweeni_starmap andi_map is that the former expects an iterable objectwhose elements are already grouped together, while the latter case groups thearguments together before applying the given function. The choice is a matterof style and convenience.

Value

iterator that returns the values ofobject along with theindex of the object.

Examples

pow <- function(x, y) {  x^y}it <- i_starmap(pow, list(c(2, 3, 10), c(5, 2, 3)))unlist(as.list(it)) == c(32, 9, 1000)# Similar to the above, but because the second vector is exhausted after two# calls to `nextElem`, the iterator is exhausted.it2 <- i_starmap(pow, list(c(2, 3, 10), c(5, 2)))unlist(as.list(it2)) == c(32, 9)# Another similar example but with lists instead of vectorsit3 <- i_starmap(pow, list(list(2, 3, 10), list(5, 2, 3)))as.list(it3)# Computes sum of each row in the iris data set# Numerically equivalent to base::rowSums()tolerance <- sqrt(.Machine$double.eps)iris_x <- iris[, -5]it4 <- i_starmap(sum, iris_x)unlist(as.list(it4)) - rowSums(iris_x) < tolerance

Create multiple iterators from one source

Description

i_tee(obj, n) consumes and buffers the output of a single iteratorobj so that it can be read byn independent sub-iterators.

Usage

i_tee(obj, n, max = 2^16 - 1, ...)

Arguments

obj

an iterable object

n

the number of iterators to return

max

The maximum number of values to buffer.

...

passed along toiteror(obj, ...)

Details

It works by saving the output of sourceobj in a queue, whileeach sub-iterator has a "read pointer" indexing into thequeue. Items are dropped from the queue after all sub-iteratorshave seen them.

This means that if one sub-iterator falls far behind the others, orequivalently if one sub-iterator reads far ahead its cohort theothers, the intervening values will be kept in memory. Themaxargument gives a limit on how many items will be held. If thislimit is exceeded due to one sub-iterator reading far ahead of theothers, an error will be thrown when that sub-iterator attempts toread a new value.

Value

a list ofn iterators.

Author(s)

Peter Meilstrup


Create a timeout iterator

Description

Create an iterator that iterates over another iterator for a specifiedperiod of time, and then stops. This can be useful when you want to searchfor something, or run a test for awhile, and then stop.

Usage

i_timeout(iterable, time, ...)

Arguments

iterable

Iterable to iterate over.

time

The time interval to iterate for, in seconds.

...

passed along toiteror(iterable, ...)

Details

Originally from theitertools package.

Value

aniteror yielding values fromiterable so long astime is in the future

Examples

# See how high we can count in a tenth of a secondlength(as.list(i_timeout(icount(), 0.1)))

Iterator that extracts the unique elements from an iterable object

Description

Constructs an iterator that extracts each unique element in turn from aniterableobject. Order of the elements is maintained. This function isan iterator analogue tounique.

Usage

i_unique(object, digest = rlang::hash, ...)

Arguments

object

an iterable object

digest

Optionally specify a custom hash function(e.g.digest::digest,rlang::hash). It should be a functionreturning a character value.

...

Extra arguments are forwarded toiteror.

Details

NOTE: In order to determine whether an element is unique, a list of previousunique elements is stored. In doing so, the list can potentially become largeif there are a large number of unique elements.

Value

an iterator that returns only the unique elements fromobject

See Also

i_dedupe

Examples

it <- i_chain(rep(1, 4), rep(2, 5), 4:7, 2)as.list(i_unique(it)) # 1 2 4 5 6 7it2 <- iterators::iter(c('a', 'a', "A", "V"))as.list(i_unique(it2)) # a A Vx <- as.character(gl(5, 10))it_unique <- i_unique(x)as.list(it_unique) # 1 2 3 4 5

Construct a sliding window over an iterator

Description

Each element returned byi_window(obj) consists ofn consecutiveelements from the underlyingobj, with the window advancingforward by one element each iteration.

Usage

i_window(obj, n, tail, ...)

Arguments

obj

An iterable.

n

The width of the window to apply

tail

If a value is given, tails will be included atthe beginning and end of iteration, filled with the given value.

...

passed along toiteror(object, ...)

Value

an iteror.

Author(s)

Peter Meilstrup

Examples

#' @examplesit <- i_window(iteror(letters[1:4]), 2)nextOr(it, NA) # list("a", "b")nextOr(it, NA) # list("b", "c")nextOr(it, NA) # list("c", "d")it2 <- i_window(icount(5), 2)nextOr(it2, NA) # list(1, 2)nextOr(it2, NA) # list(2, 3)nextOr(it2, NA) # list(3, 4)nextOr(it2, NA) # list(4, 5)it <- i_window(letters[1:4], 2)nextOr(it, NA) # list("a", "b")nextOr(it, NA) # list("b", "c")nextOr(it, NA) # list("c", "d")it <- i_window(letters[1:4], 3)nextOr(it) # list("a", "b", "c")nextOr(it) # list("b", "c", "d")it <- i_window(letters[1:4], 3, tail=" ")nextOr(it) # list(" ", " ", "a")nextOr(it) # list(" ", "a", "b")nextOr(it) # list("a", "b", "c")nextOr(it) # list("b", "c", "d")nextOr(it) # list("c", "d", " ")nextOr(it) # list("d", " ", " ")

Combine several iterables in parallel.

Description

The resulting iterator aggregates one element from each of theiterables into a list for each iteration. Used for lock-stepiteration over several iterables at a time.

Usage

i_zip(...)i_zip_longest(..., fill = NA)

Arguments

...

multiple arguments to iterate through in parallel

fill

the value used to replace missing values when the iterables in... are of uneven length

Details

For⁠[i_zip]⁠, the output will finish when any of the underlying iterables finish.

Originally from theitertools package.

Originally from packageitertools2.

Value

iterator that iterates through each argument in sequence

Examples

# Iterate over two iterables of different sizesas.list(i_zip(a=1:2, b=letters[1:3]))it <- i_zip_longest(x=1:3, y=4:6, z=7:9)nextOr(it, NA) # list(x=1, y=4, z=7)nextOr(it, NA) # list(x=2, y=5, z=8)nextOr(it, NA) # list(x=3, y=6, z=9)it2 <- i_zip_longest(1:3, 4:8)nextOr(it2, NA) # list(1, 4)nextOr(it2, NA) # list(2, 5)nextOr(it2, NA) # list(3, 6)nextOr(it2, NA) # list(NA, 7)nextOr(it2, NA) # list(NA, 8)it3 <- i_zip_longest(1:2, 4:7, levels(iris$Species), fill="w00t")nextOr(it3, NA) # list(1, 4, "setosa")nextOr(it3, NA) # list(2, 5, "versicolor")nextOr(it3, NA) # list("w00t", 6, "virginica")nextOr(it3, NA) # list("w00t", 7, "w00t")

Iterator that generates all combinations of a vector taken m at a time.

Description

Constructs an iterator generates all combinations of a vector takenmat a time. This function is similar tocombn.

Usage

icombinations(object, m, replacement = FALSE)

Arguments

object

vector

m

the length of each combination

replacement

Generate combinations with replacement? Default: no.

Details

By default, the combinations arewithout replacement so that elements arenot repeated. To generate combinationswith replacement, setreplacement=TRUE.

The function implementation is loosely based on thecombinationsfunction from Python's itertools. Combinations with replacement are based oncombinations_with_replacement from the same Python library.

Value

iterator that generates all combinations ofobject

Examples

# Combinations without replacementit <- icombinations(1:4, m=2)nextOr(it, NA) # c(1, 2)nextOr(it, NA) # c(1, 3)nextOr(it, NA) # c(1, 4)nextOr(it, NA) # c(2, 3)nextOr(it, NA) # c(2, 4)nextOr(it, NA) # c(3, 4)# Combinations without replacementit <- icombinations(1:4, m=2, replacement=TRUE)nextOr(it, NA) # c(1, 1)nextOr(it, NA) # c(1, 2)nextOr(it, NA) # c(1, 3)nextOr(it, NA) # c(1, 4)nextOr(it, NA) # c(2, 2)nextOr(it, NA) # c(2, 3)nextOr(it, NA) # c(2, 4)nextOr(it, NA) # c(3, 3)nextOr(it, NA) # c(3, 4)nextOr(it, NA) # c(4, 4)it3 <- icombinations(1:5, m=2)as.list(it3)utils::combn(x=1:5, m=2, simplify=FALSE)

Counting Iterators

Description

Returns an iterator that counts starting from one.

icountn(vn) takes a vector specifying an array size,and returns an iterator over array indices. Each returned elementis a vector the same length as vn, with the first index varying fastest.If vn has a names attribute the output will have the same names.

Usage

icount(count = Inf, ..., recycle = FALSE, chunkSize, chunks)icountn(vn, ..., recycle = FALSE, chunkSize, chunks, rowMajor = TRUE)

Arguments

count

number of times that the iterator will fire. Use NA orInf to make an iterator that counts forever.

...

Undocumented

recycle

Whether to restart the count after finishing.

chunkSize

How many valies to return from each call to nextOr().

chunks

How many chunks to split the input. Eitherchunks orchunkSize may be given but not both.

vn

A vector of integers.

rowMajor

IfTRUE (default), the earliest indices will cycle fastest;ifFALSE, last indices cycle fastest.

Details

Originally from theiterators package.

Value

The counting iterator.

See Also

For more control over starting number and step size, seeiseq.

Examples

# create an iterator that counts from 1 to 3.it <- icount(3)nextOr(it)nextOr(it)nextOr(it)nextOr(it, NULL)  # expect NULLx <- icount(5)repeat print(nextOr(x, break))it2 <- icount(100)all.equal(as.numeric(it2), 1:100)as.list(icountn(c(2, 3)))

Drop duplicated items from an iterator.

Description

Constructs an iterator that removes runs of repeated elements from theunderlying iterator. Order of the elements is maintained. Only the elementjust seen is remembered for determining whether to drop.

Usage

idedup(object, cmp = identical, ...)

Arguments

object

an iterable object

cmp

A function to use for comparison.

...

passed along toiteror(object, ...)

Details

Originated asitertools2::iunique_lastseen.object.

Value

an iterator that skips over duplicate items from tehunterlying iterator.

See Also

i_rle

Examples

it <- i_chain(rep(1,4), rep(2, 5), 4:7, 2)it_i_unique <- idedup(it)as.list(it_i_unique) # 1 2 4 5 6 7 2it2 <- iteror(c('a', 'a', "A", 'a', 'a', "V"))i_dedupe <- idedup(it2)as.list(idedup) # a A a V

Dividing Iterator

Description

Returns an iterator dividing a value into integer chunks, such thatsum(idiv(n, ...)) == floor(n)

Usage

idiv(count, ..., recycle = FALSE, chunkSize, chunks)

Arguments

count

The total

...

Unused.

recycle

Whether to restart the count after finishing.

chunkSize

the maximum size of the pieces thatn should bedivided into. This is useful when you know the size of the pieces that youwant. If specified, thenchunks should not be.

chunks

the number of pieces thatn should be divided into.This is useful when you know the number of pieces that you want. Ifspecified, thenchunkSize should not be.

Details

Originally from theiterators package.

Value

The dividing iterator.

Examples

# divide the value 10 into 3 piecesit <- idiv(10, chunks = 3)nextOr(it)nextOr(it)nextOr(it)nextOr(it, NULL)  # expect NULL# divide the value 10 into pieces no larger than 3it <- idiv(10, chunkSize = 3)nextOr(it)nextOr(it)nextOr(it)nextOr(it)nextOr(it, NULL)  # end of iterator

Iterator that covers the Cartesian product of the arguments.

Description

Given a number of vectors as arguments, constructs an iterator that enumerates the Cartesian product of all arguments.

Usage

igrid(  ...,  recycle = FALSE,  chunkSize,  chunks,  simplify = FALSE,  rowMajor = TRUE)

Arguments

...

Named vectors to iterate over.

recycle

If TRUE, the iteror starts over on reaching the end.

chunkSize

Optional; how many rows to return in each step.

chunks

Optional; how many chunks to divide the input into.

simplify

If TRUE, inputs are coerced to a common data typeand results are returned in a vector (or matrix if chunking isenabled). If FALSE, results are returned as a list (or data.frameif chunking).

rowMajor

If TRUE, the left-most indices change fastest. IfFALSE the rightmost indices change fastest.

Details

Although they share the same end goal,igrid can yielddrastic memory savings compared toexpand.grid.

Value

aniteror that iterates through each element from theCartesian product of its arguments.

Examples

# Simulate a doubly-nested loop with a single while loopit <- igrid(a=1:3, b=1:2)repeat {  x <- nextOr(it, break)  cat(sprintf('a = %d, b = %d\n', x$a, x$b))}it <- igrid(x=1:3, y=4:5)nextOr(it, NA) # list(x=1, y=4)nextOr(it, NA) # list(x=1, y=5)nextOr(it, NA) # list(x=2, y=4)nextOr(it, NA) # list(x=2, y=5)nextOr(it, NA) # list(x=3, y=4)nextOr(it, NA) # list(x=3, y=5)# Second Cartesian productnextOr(it, NA) # list(x=1, y=4)nextOr(it, NA) # list(x=1, y=5)nextOr(it, NA) # list(x=2, y=4)nextOr(it, NA) # list(x=2, y=5)nextOr(it, NA) # list(x=3, y=4)nextOr(it, NA) # list(x=3, y=5)# igrid is an iterator equivalent to base::expand.grid()# Large data.frames are not created unless the iterator is manually consumeda <- 1:2b <- 3:4c <- 5:6it3 <- igrid(a=a, b=b, c=c)df_igrid <- do.call(rbind, as.list(it3))df_igrid <- data.frame(df_igrid)# Compare df_igrid with the results from base::expand.grid()base::expand.grid(a=a, b=b, c=c)

Iterator that generates all permutations of a vector.

Description

Constructs an iterator generates all permutations of an iterable object. Bydefault, full-length permutations are generated. Ifm is specified,successivem length permutations are instead generated.

Usage

ipermutations(object, m = NULL)

Arguments

object

vector

m

length of permutations. By default, full-length permutations aregenerated.

Details

The implementation is loosely based on that of Python's itertools.

Value

iterator that generates all permutations ofobject

Examples

it <- ipermutations(1:3)nextOr(it, NA) # c(1, 2, 3)nextOr(it, NA) # c(1, 3, 2)nextOr(it, NA) # c(3, 1, 2)nextOr(it, NA) # c(3, 2, 1)nextOr(it, NA) # c(2, 3, 1)nextOr(it, NA) # c(2, 1, 3)it2 <- ipermutations(letters[1:4])# 24 = 4! permutations of the letters a, b, c, and das.list(it2)

Iterator over Rows of a Data Frame Stored in a File

Description

Returns an iterator over the rows of a data frame stored in a file in tableformat. It is a wrapper around the standardread.table function.

Usage

iread.table(file, ..., verbose = FALSE)

Arguments

file

the name of the file to read the data from.

...

all additional arguments are passed on to theread.tablefunction. See the documentation forread.table for more information.

verbose

logical value indicating whether or not to print the calls toread.table.

Details

Originally from theiterators package.

Value

The file reading iterator.

Note

In this version ofiread.table, both theread.tableargumentsheader androw.names must be specified. This isbecause the default values of these arguments depend on the contents of thebeginning of the file. In order to make the subsequent calls toread.table work consistently, the user must specify those argumentsexplicitly. A future version ofiread.table may remove thisrequirement.

See Also

read.table


Create an iterator to read binary data from a connection

Description

Create an iterator to read binary data from a connection.

Usage

ireadBin(  con,  what = "raw",  n = 1L,  size = NA_integer_,  signed = TRUE,  endian = .Platform$endian,  ipos = NULL)

Arguments

con

A connection object or a character string naming a file or a rawvector.

what

Either an object whose mode will give the mode of the vector tobe read, or a character vector of length one describing the mode: one of“numeric”, “double”, “integer”, “int”,“logical”, “complex”, “character”, “raw”.UnlikereadBin, the default value is “raw”.

n

integer. The (maximal) number of records to be read each time theiterator is called.

size

integer. The number of bytes per element in the byte stream.The default, ‘NA_integer_’, uses the natural size.

signed

logical. Only used for integers of sizes 1 and 2, when itdetermines if the quantity on file should be regarded as a signed orunsigned integer.

endian

The endian-ness ('“big”' or '“little”') of thetarget system for the file. Using '“swap”' will force swappingendian-ness.

ipos

iterable. If notNULL, values from this iterable will beused to do a seek on the file before calling readBin.

Details

Originally from theitertools package.

Value

Aniteror reading binary chunks from the connection.

Examples

zz <- file("testbin", "wb")writeBin(1:100, zz)close(zz)it <- ihasNext(ireadBin("testbin", integer(), 10))repeat print(nextOr(it, break))unlink("testbin")

Iterator over Lines of Text from a Connection

Description

Returns an iterator over the lines of text from a connection. It is awrapper around the standardreadLines function.

Usage

ireadLines(con, n = 1, ...)

Arguments

con

a connection object or a character string.

n

integer. The maximum number of lines to read. Negative valuesindicate that one should read up to the end of the connection. The defaultvalue is 1.

...

passed on to thereadLines function.

Details

Originally from theiterators package.

Value

The line reading iterator.

See Also

readLines

Examples

# create an iterator over the lines of COPYINGit <- ireadLines(file.path(R.home(), "COPYING"))nextOr(it)nextOr(it)nextOr(it)

Create an iterator to read data frames from files

Description

Create an iterator to read data frames from files.

Usage

ireaddf(filenames, n, start = 1, col.names, chunkSize = 1000)

Arguments

filenames

Names of files contains column data.

n

Maximum number of elements to read from each column file.

start

Element to start reading from.

col.names

Names of the columns.

chunkSize

Number of rows to read at a time.

Details

Originally from theitertools package.

Value

Aniteror yieldingdata.frame objects with up ton rows.


Random Number Iterators

Description

These functions each construct an iterator that producesrandom numbers of various distributions. Each one is a wrapperaround a base R function.

Usage

irnorm(  n,  mean = 0,  sd = 1,  count = Inf,  independent = !missing(seed) || !missing(kind),  seed = NULL,  kind = NULL,  normal.kind = NULL,  sample.kind = NULL)irbinom(  n,  size,  prob,  count = Inf,  independent = !missing(seed) || !missing(kind),  seed = NULL,  kind = NULL,  normal.kind = NULL,  sample.kind = NULL)irnbinom(  n,  size,  prob,  mu,  count = Inf,  independent = !missing(seed) || !missing(kind),  seed = NULL,  kind = NULL,  normal.kind = NULL,  sample.kind = NULL)irpois(  n,  lambda,  count = Inf,  independent = !missing(seed) || !missing(kind),  seed = NULL,  kind = NULL,  normal.kind = NULL,  sample.kind = NULL)isample(  x,  size,  replace = FALSE,  prob = NULL,  count = Inf,  independent = !missing(seed) || !missing(kind),  seed = NULL,  kind = NULL,  normal.kind = NULL,  sample.kind = NULL)irunif(  n,  min = 0,  max = 1,  count = Inf,  independent = !missing(seed) || !missing(kind),  seed = NULL,  kind = NULL,  normal.kind = NULL,  sample.kind = NULL)

Arguments

n

How many samples to compute per call; see e.g.rnorm.

mean

seernorm.

sd

seernorm.

count

number of times that the iterator will fire. If notspecified, it will fire values forever.

independent

If TRUE, this iterator will keep its own privaterandom state, so that its output is reproducible and independentof anything else in the program; this comes at some performancecost. Default is FALSEunlessseed orkind are given. Ifindependent=TRUE but neitherseed norkind are specified,we will use the "L'Ecuyer-CMRG" generator with a seed valuetaken from a package-private instance ofiRNGStream.

seed

A specific seed value for reproducibility. If given,independent=TRUE is implied. This can be a single number (whichwill be passed toset.seed(seed, kind, normal.kind, sample.kind); it can also be a vector containing acomplete, valid state for.Random.seed. If the latter,argumentskind, etc. are ignored.

kind

Which random number algorithm to use; passed along toset.seed, If given,independent=TRUE is implied.

normal.kind

Passed along toset.seed.

sample.kind

Passed along toset.seed.

size

see e.g.rbinom.

prob

see e.g.rbinom.

mu

seernbinom.

lambda

seerpois.

x

seeisample.

replace

seeisample.

min

seerunif.

max

seerunif.

Details

Originally from theiterators package.

Value

An iterator that is a wrapper around the correspondingrandom number generator function.

See Also

If you are creating multiple independent iterators,iRNGStream will create well-separated seed values, which mayhelp avoid spurious correlations between iterators.

Examples

# create an iterator that returns three random numbersit <- irnorm(1, count = 3)nextOr(it)nextOr(it)nextOr(it)nextOr(it, NULL)# iterators created with a specific seed will make reproducible valuesit <- irunif(n=1, seed=314, kind="L'Ecuyer-CMRG")nextOr(it) # 0.4936700nextOr(it) # 0.5103891nextOr(it) # 0.2338745# the iRNGStream produces a sequence of well separated seed values,rng.seeds <- iRNGStream(313)it1 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))it2 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))it3 <- isample(c(0, 1), 1, seed=nextOr(rng.seeds))take(it1, 5, "numeric") # 0 1 0 0 1take(it2, 5, "numeric") # 0 1 0 0 0take(it3, 5, "numeric") # 0 0 0 1 1

is.iteror indicates if an object is an iteror.

Description

is.iteror indicates if an object is an iteror.

Usage

is.iteror(x)

Arguments

x

any object.

Value

TRUE if the object has classiteror.

Examples

it <- iteror(1:3)stopifnot(is.iteror(it))repeat {  print(nextOr(it, break))}

Iterators for sequence generation

Description

Constructs iterators that generate regular sequences that follow theseq family.

Usage

iseq(  from = 1,  to = NULL,  by = NULL,  length_out = NULL,  along_with = NULL,  ...,  recycle = FALSE,  chunkSize,  chunks)iseq_along(along_with, ...)

Arguments

from

the starting value of the sequence.

to

the end value of the sequence.

by

increment of the sequence.

length_out

desired length of the sequence. A non-negative number,which forseq will be rounded up if fractional.

along_with

the length of the sequence will match the length of this

...

Unused.

recycle

Whether to restart the sequence after it reachesto.

chunkSize

Optional; return this many values per call.

chunks

Optional; return this many chunks.

Details

Theiseq function generates a sequence of values beginning withfrom and ending withto. The sequence of values between aredetermined by theby,length_out, andalong_witharguments. Theby argument determines the step size of the sequence,whereaslength_out andalong_with determine the length of thesequence. Ifby is not given, then it is determined by eitherlength_out oralong_with. By default, neither are given, inwhich caseby is set to 1 or -1, depending on whetherto >from.

Value

aniteror.

See Also

icount icountn

Examples

it <- iseq(from=2, to=5)unlist(as.list(it)) == 2:5

Split Iterator

Description

Returns an iterator that divides the data in the vectorx into thegroups defined byf.

Usage

isplit(x, f, drop = FALSE, ...)

Arguments

x

vector or data frame of values to be split into groups.

f

a factor or list of factors used to categorizex.

drop

logical indicating if levels that do not occur should bedropped.

...

current ignored.

Details

Originally from theiterators package.

Value

The split iterator.

See Also

split

Examples

x <- rnorm(200)f <- factor(sample(1:10, length(x), replace = TRUE))it <- isplit(x, f)expected <- split(x, f)for (i in expected) {    actual <- nextOr(it, break)    stopifnot(actual$value == i)}

Iterator that maps a function to a sequence of numeric values

Description

Constructs an iterator that maps a given function over an indefinite sequenceof numeric values. The input the functionf is expected to accept asingle numeric argument. The sequence of arguments passed tof beginwithstart and are incremented bystep.

Usage

itabulate(f, start = 1, step = 1)

Arguments

f

the function to apply

start

sequence's initial value

step

sequence's step size

Value

an iterator that returns the mapped values from the sequence

Examples

it <- itabulate(f=function(x) x + 1)take(it, 4) # 2 3 4 5it2 <- itabulate(f=function(x) x^2, start=-3)take(it2, 6) # 9 4 1 0 1 4it3 <- itabulate(abs, start=-5, step=2)take(it3, 6) # 5 3 1 1 3 5it4 <- itabulate(exp, start=6, step=-2)take(it4, 4) # exp(c(6, 4, 2, 0))

Make an iteror from a given object.

Description

it <- iteror(obj, ...) is a generic constructor that createsobjects of class "iteror" from its input. An iteror outputs asingle element of a sequence each time you callnextOr(it). Differentiteror methods exist for different datatypes and may take different optional arguments as listed in thispage.

Usage

iteror(obj, ...)## S3 method for class 'iter'iteror(obj, ...)## Default S3 method:iteror(obj, ..., recycle = FALSE, chunkSize, chunks)## S3 method for class 'connection'iteror(obj, ...)

Arguments

obj

An object to iterate with.

...

Differentiteror methods may take additional optionsdepending on the class ofobj.

recycle

a boolean describing whether the iterator shouldreset after running through all its values.

chunkSize

How many elements (or slices) to include in eachchunk.

chunks

Split the input into this many chunks.

Details

When called, an iteror may either return a new value or stop. Theway an iteror signals a stop is that it does whatever you write inthe argumentor. For instance you can writeor=break to exit aloop. Summing over an iteror this way looks like:

sum <- 0it <- iteror(iseq(0, 100, 7))repeat {  sum <- sum + nextOr(it, break)}

Another way to use the "or" argument is to give it a sentinel value;that is, a special value that you will interpret as end ofiteration. If the result of callingnextOr isidentical() tothe value you provided, then you know the iterator has ended. Thispattern looks like:

sum <- 0stopped <- new.env()it <- iteror(iseq(0, 100, 7))repeat {  val <- nextOr(it, stopped)  if (identical(val, stopped)) break  sum <- sum + val}

(Here I'm usingnew.env() as a sentinel value. In R it iscommonplace to useNULL orNA as a kind of sentinel value, butthat only works until you have an iterator that needs to yield NULLitself. A safer alternative is to use a local, one-shot sentinel value;new.env() is ideal, as it constructs an object that isnotidentical to any other object in the R session.)

Note thatiteror objects are simply functions with a classattribute attached, and allnextOr.iteror does is call thefunction. So if you were in the mood, you could skip callingnextOr involving S3 dispatch and instead call the iterordirectly. If you take this approach, make sure you have callediteror() first to ensure that you have a trueiteror object.

sum <- 0it <- iteror(iseq(0, 100, 7))repeat sum <- sum + it(or=break)sum#> [1] 735

To create iterors with custom-defined behavior, seeiteror.function.

Value

an object of classes 'iteror' and 'iter'.

The methoditeror.iter wraps aniterators::iter objectand returns an iteror.

The default methoditeror.default treatsobj as avector to yield values from.

See Also

iteror.array iteror.function iteror.data.frame


Iterate over an array or data frame by a specified dimension.

Description

Iterate over an array or data frame by a specified dimension.

Usage

## S3 method for class 'array'iteror(  obj,  ...,  by = c("cell", "row", "column"),  chunkSize,  chunks,  recycle = FALSE,  drop = FALSE,  rowMajor = TRUE)## S3 method for class 'matrix'iteror(  obj,  ...,  by = c("cell", "row", "column"),  chunkSize,  chunks,  recycle = FALSE,  drop = FALSE,  rowMajor = TRUE)## S3 method for class 'data.frame'iteror(obj, ..., recycle = FALSE, chunkSize, chunks, by = c("column", "row"))

Arguments

obj

An object to iterate over.

...

Undocumented.

by

Which dimension to slice an array or data frame by. Can be "cell","row", "column", or numeric dimensions.

chunkSize

The thickness of the slice to take along the specified dimension.

chunks

How many slices to take.

recycle

If TRUE, the iteror starts over on reaching the end.

drop

Whether to drop the array dimensions enumerated over.

rowMajor

If TRUE, will return slices in order with the firstindices varying fastest (same as ini_enumerate).

Value

an iteror yielding fromobj along the specified dimensions.

Examples

l <- iteror(letters, chunkSize=7)as.list(l)a <- array(1:8, c(2, 2, 2))# iterate over all the slicesit <- iteror(a, by=3)as.list(it)# iterate over all the columns of each sliceit <- iteror(a, by=c(2, 3))as.list(it)# iterate over all the rows of each sliceit <- iteror(a, by=c(1, 3))as.list(it)

Construct an iteror object with custom-programmed behavior.

Description

Passobj a function that has a first argument named "or". Inwriting this function, you can maintain state by using enclosedvariables and update using⁠<<-⁠, Whatever valueobj() returns isthe next element of the iteror. Treat argumentor as a lazy value;do not touch it until until you need to signal end of iteration;to signal end of iteration, force and immediately returnor.

Usage

## S3 method for class ''function''iteror(obj, ..., catch, sentinel, count)

Arguments

obj

A function. It should have having an argument named "or"

...

Undocumented.

catch

Ifobj does not have anor argument, specifye.g.catch="StopIteration" to interpret that an error withthat message as end of iteration.

sentinel

Ifobj does not have anor argument, you can specifya special value to watch for end of iteration. Stop will be signaledif the function result isidentical() tosentinel.

count

Ifobj does not have anor argument, you can specifyhow many calls before stop iteration, orgiveNA orInf to never stop.

Details

You can also provideobj a simple function of no arguments, aslong as you specify one ofcatch,sentinel, orcount to specifyhow to detect end of iteration.

Value

An object of mode "function" and class "iteror".

Aniteror which calls the given function to produce values.

Examples

# an iterator that counts from start to stopirange <- function(from=1, to=Inf) {  current <- from  iteror(function(or) {    if (current > to) {      return(or)    } else {      tmp <- current      current <<- current + 1      tmp    }  })}it <- irange(5, 10)as.vector(it, "numeric")# an endless random number generatorirand <- function(min, max) { iteror(function() runif(1, min=min, max=max), count=Inf)}take(irand(5, 10), 10)

Iterator Constructor-Constructor Function Wrapper

Description

ThemakeIwrapper function wraps an R function to produce aniterator constructor. It is used to construct random samplingiterators in this package; for instanceirnorm is defined asirnorm <- makeIwrapper(rnorm).

Usage

makeIwrapper(FUN)

Arguments

FUN

a function that generates different values each time itis called; typically one of the standard random number generatorfunctions.

Details

The resulting iterator constructors all take an optionalcount argument which specifies the number of times theresulting iterator should fire. They also have an argumentindependent which enables independent tracking of the randomnumber seed. Theisample function is an example of one suchiterator constructoe (as areirnorm,irunif, etc.).

Original version appeared in theiterators package.

Value

An iterator that is a wrapper around the correspondingfunction.

Examples

# create an iterator maker for the sample functionmysample <- makeIwrapper(sample)# use this iterator maker to generate an iterator that will generate three five# member samples from the sequence 1:100it <- mysample(1:100, 5, count = 3)nextOr(it)nextOr(it)nextOr(it)nextOr(it, NULL)  # NULL

Retreive the next element from an iteror.

Description

Retreive the next element from an iteror.

Usage

nextOr(obj, or, ...)

Arguments

obj

Aniteror.

or

If the iterator has reached its end, this argumentwill be forced and returned.

...

Other arguments may be used by specific iterors.

Value

Either the next value ofiteror, or the value ofor.


Returns the nth item of an iteror

Description

Returns thenth item of aniteror after advancing theiterorn steps ahead. If theiteror is entirely consumed,the argumentor is returned instead. That is, if eithern >length(iteror) orn is 0, then theiteror is consumed.

Usage

nth(obj, n, or, ...)

Arguments

obj

an iterable.

n

The index of the desired element to return.

or

If the iteror finishes before retuningn elements,this argument will be forced and returned.

...

passed along toiteror constructor.

Value

The nth element of the iteror or the result of forcingor.

See Also

take consume collect

Examples

it <- iteror(1:10)# Returns 5nth(it, 5, NA)it2 <- iteror(letters)# Returns 'e'nth(it2, 5, NA)it3 <- iteror(letters)# Returns default value of NAnth(it3, 42, NA)it4 <- iteror(letters)# Returns default value of "foo"nth(it4, 42, or="foo")

Count the number of times an iterable object is TRUE

Description

Returns the number of elements from an iterable object thatevaluate toTRUE.

Usage

quantify(obj, ...)

Arguments

obj

an iterable object

...

further arguments passed toiteror.

Value

the number ofTRUE elements

See Also

reduce

Examples

it <- iteror(c(TRUE, FALSE, TRUE))quantify(it) # 2set.seed(42)x <- sample(c(TRUE, FALSE), size=10, replace=TRUE)quantify(x) # Equivalent to sum(x)

Wrap an iteror to appear as a Python iterator or vice versa.

Description

This requires thereticulate package to be installed.

Usage

## S3 method for class 'iteror'r_to_py(x, convert = FALSE, ...)## S3 method for class 'python.builtin.object'iteror(obj, ...)

Arguments

x

An iterable object.

convert

does nothing.

...

Passed along toiteror(x, ...).

obj

A Python object (as viewed by packagereticulate.)

Value

r_to_py(it) returns a Python iterator.

Methoditeror.python.builtin.object returns aniteror.

Examples

pit <- reticulate::r_to_py(iseq(2, 11, 5))reticulate::iter_next(pit, NULL)reticulate::iter_next(pit, NULL)reticulate::iter_next(pit, NULL)# create an R iterator and ask Python to sum ittriangulars <- icount() |> i_accum() |> i_limit(10)builtins <- reticulate::import_builtins()builtins$sum(triangulars) # r_to_py is called automatically# create a generator in Python and sum it in Rpit <- reticulate::py_eval("(n for n in range(1, 25) if n % 3 == 0)")sum(iteror(pit))

Record and replay iterators

Description

Therecord function records the values issued by a specifiediterator to a file or connection object. Theireplay functionreturns an iterator that will replay those values. This is useful foriterating concurrently over multiple, large matrices or data frames that youcan't keep in memory at the same time. These large objects can be recordedto files one at a time, and then be replayed concurrently using minimalmemory.

Usage

record(iterable, con, ...)

Arguments

iterable

The iterable to record to the file.

con

A file path or open connection.

...

passed along toiteror(iterable, ...)

Details

Originally from theitertools package.

Value

NULL, invisibly.

Examples

suppressMessages(library(foreach))m1 <- matrix(rnorm(70), 7, 10)f1 <- tempfile()record(iteror(m1, by='row', chunkSize=3), f1)m2 <- matrix(1:50, 10, 5)f2 <- tempfile()record(iteror(m2, by='column', chunkSize=3), f2)# Perform a simple out-of-core matrix multiplyp <- foreach(col=ireplay(f2), .combine='cbind') %:%       foreach(row=ireplay(f1), .combine='rbind') %do% {         row %*% col       }dimnames(p) <- NULLprint(p)all.equal(p, m1 %*% m2)unlink(c(f1, f2))

Compute the sum, product, or general reduction of an iterator.

Description

reduce(obj, fun) applies a 2-argument functionfun betweensuccessive elements of obj. For example iffun is+,⁠reduce(it, ⁠+⁠, init=0)⁠ computes0 + nextElem(it) + nextElem(it) + nextElem(it) + ... until the iterator finishes,and returns the final value.

i_accum(obj) returns the iterator containingeach intermediate result. The default settingsproduce a cumulative sum.

sum.iteror(it) is equivalent toreduce(it, `+`)

prod.iteror(it) is equivalent toreduce(it, `*`).

Usage

reduce(obj, fun = `+`, init = 0, ...)## S3 method for class 'iteror'reduce(obj, fun = `+`, init = 0, ...)i_accum(obj, fun = `+`, init = 0, ...)## S3 method for class 'iteror'sum(..., na.rm = FALSE)## S3 method for class 'iteror'prod(..., na.rm = FALSE)

Arguments

obj

an iterable object

fun

A function of as least two arguments.

init

A starting value.

...

Extra parameters will be passed to each call tofun.

na.rm

Whether to drop NA values when computing sum or prod.

Value

The result of accumulation.

Author(s)

Peter Meilstrup

Examples

it <- icount(5)total <- reduce(it, `+`) # 15it <- icount(5)reduce(it, paste0, "") # "12345"it <- icount(5)reduce(it, `*`, init=1) # 120# triangular numbers: 1, 1+2, 1+2+3, ...take(i_accum(icount()), 10, 'numeric')

Return the first n elements of an iterable object in a vector.

Description

Returns the firstn elements of an iterableobject as a list.Ifn is larger than the number of elements inobject, theentire iterator is consumed.

Usage

take(obj, n = 1, mode = "list", ...)## Default S3 method:take(obj, n = 1, mode = "list", ...)## S3 method for class 'iteror'take(obj, n = 1, mode = "list", ...)

Arguments

obj

An iterable object.

n

The maximum number of elements to extract from the iteror.

mode

The mode of vector to return.

...

Further arguments may be passed along to theiteror constructor.

Details

A functiontake first appeared in packageitertools2.It is basically an alias foras.list but defaults to n=1.

Value

a list of the firstn items of the iterableobj

See Also

concat as.vector.iteror

as.vector.iteror

Examples

take(1:10, 3) # 1 2 3take(icount(), 10) # 1:10take(icount(5), 10) # 1 2 3 4 5

[8]ページ先頭

©2009-2025 Movatter.jp