+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ Config/testthat/edition: 3
Config/Needs/website: tidyverse/tidytemplate
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2.9000
RoxygenNote: 7.2.0
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(as_iterator,default)
S3method(as_iterator,python.builtin.object)
S3method(print,coro_async)
S3method(print,coro_async_generator)
S3method(print,coro_generator)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# coro (development version)

* `coro::as_iterator()` method added for reticulate Python objects, enabling
usage like: `coro::loop(for (elem in my_py_object) ...)` (#37, @t-kalinowski)

# coro 1.0.2

* The exhaustion sentinel is now `as.symbol(".__exhausted__.")`
Expand Down
16 changes: 7 additions & 9 deletions R/iterator.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,10 @@ as_iterator.default <- function(x) {
}
}

on_load(on_package_load("reticulate", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it reticulate doesn't plan to add these methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think {coro} is a better home for these methods. reticulate already provides an as_iterator() function which is not a generic and does not adhere to the coro iterator protocol. Exporting a method for a same-named generic in reticulate invites confusion, and also the complexity of an awkward delayed registration dance that can be completely avoided if these methods live here.

if (!s3_has_method("coro::as_iterator", "python.builtin.iterator")) {
s3_register("coro::as_iterator", "python.builtin.iterator", function(x) {
function() {
reticulate::iter_next(x, completed = exhausted())
}
})
}
}))


#' @export
as_iterator.python.builtin.object <- function(x) {
x <- reticulate::as_iterator(x)
function() reticulate::iter_next(x, exhausted())
}
5 changes: 5 additions & 0 deletions flowery.Rproj → coro.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

Expand Down
34 changes: 25 additions & 9 deletions man/iterator.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions tests/testthat/test-iterator.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ def first_n(n):
out <- c(out, x)
})
expect_equal(out, 1:3)


})


test_that("can use reticulate iteratables", {
skip_if_not_installed("reticulate")
skip_on_cran()
skip_on_ci()

i <- 0L
loop(for (el in reticulate::py_eval("range(3)")) {
i <- i + el
})
expect_equal(i, 3L)
})


点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载