-
Notifications
You must be signed in to change notification settings - Fork 12
Closed
Description
Hi @lionel-,
I am just playing with coro
and it was perhaps surprising to me that when looping over an iterator
with loop()
and for
, that the yields from the iterator are different than if I call collect()
. Is this behaviour expected? Am I misunderstanding/misusing these functions? Should iterators only ever return single-element objects?
collect()
returns a list with an element for each column-value for a given row, while loop()/for
return the expected value which is a single row of data.
library(R6)
library(coro)
Foo <- R6Class(
classname = "Foo",
public = list(
data = mtcars,
.length = function() {
nrow(self$data)
},
.getitem = function(i) {
self$data[i, ]
}
)
)
gen <- generator(
function() {
x <- Foo$new()
for (i in 1:x$.length()) {
yield(x$.getitem(i))
}
}
)
iter <- gen()
collect(iter, 1)
#> [[1]]
#> [1] 21
#>
#> [[2]]
#> [1] 6
#>
#> [[3]]
#> [1] 160
#>
#> [[4]]
#> [1] 110
#>
#> [[5]]
#> [1] 3.9
#>
#> [[6]]
#> [1] 2.62
#>
#> [[7]]
#> [1] 16.46
#>
#> [[8]]
#> [1] 0
#>
#> [[9]]
#> [1] 1
#>
#> [[10]]
#> [1] 4
#>
#> [[11]]
#> [1] 4
loop(for (i in iter) {
print(i)
})
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
#> mpg cyl disp hp drat wt qsec vs am gear carb
# << TRUNCATED >>
Metadata
Metadata
Assignees
Labels
No labels