Skip to content

Commit

Permalink
Big README updated, methods are solid
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew T. Warkentin committed Dec 21, 2020
1 parent c41592b commit 989d11c
Show file tree
Hide file tree
Showing 27 changed files with 701 additions and 138 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ Description: This package provides S3 methods for many common R generics.
special naming semantics.
License: MIT + file LICENSE
Imports:
coro,
R6,
rlang
rlang,
utils
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
testthat (>= 3.0.0)
Config/testthat/edition: 3
26 changes: 26 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# Generated by roxygen2: do not edit by hand

S3method("!=",R6)
S3method("$",R6)
S3method("$<-",R6)
S3method("%%",R6)
S3method("%/%",R6)
S3method("*",R6)
S3method("+",R6)
S3method("-",R6)
S3method("/",R6)
S3method("<",R6)
S3method("<=",R6)
S3method("==",R6)
S3method(">",R6)
S3method(">=",R6)
S3method("[",R6)
S3method("[<-",R6)
S3method("[[",R6)
S3method("[[<-",R6)
S3method("^",R6)
S3method(all,equal.R6)
S3method(as.data.frame,R6)
S3method(length,R6)
S3method(names,R6)
S3method(str,R6)
export(is.iterable)
export(iter)
81 changes: 35 additions & 46 deletions R/arithmetic.R
Original file line number Diff line number Diff line change
@@ -1,87 +1,76 @@
`+.R6` <- function(...) {
if (is.function(.subset2(..1, ".add"))) {
.subset2(..1, ".add")(..2)
#' @export
`+.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__add__"))) {
.subset2(e1, ".__add__")(e2)
} else {
rlang::abort(
glue("No `+` method defined for R6 instance")
paste0("No `+` method defined for R6 instance")
)
}
x
}

`-.R6` <- function(...) {
if (is.function(.subset2(..1, ".sub"))) {
.subset2(..1, ".sub")(..2)
#' @export
`-.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__sub__"))) {
.subset2(e1, ".__sub__")(e2)
} else {
rlang::abort(
glue("No `-` method defined for R6 instance")
paste0("No `-` method defined for R6 instance")
)
}
x
}

`*.R6` <- function(...) {
if (is.function(.subset2(..1, ".mul"))) {
.subset2(..1, ".mul")(..2)
#' @export
`*.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__mul__"))) {
.subset2(e1, ".__mul__")(e2)
} else {
rlang::abort(
glue("No `*` method defined for R6 instance")
paste0("No `*` method defined for R6 instance")
)
}
x
}

`/.R6` <- function(...) {
if (is.function(.subset2(..1, ".div"))) {
.subset2(..1, ".div")(..2)
#' @export
`/.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__div__"))) {
.subset2(e1, ".__div__")(e2)
} else {
rlang::abort(
glue("No `/` method defined for R6 instance")
paste0("No `/` method defined for R6 instance")
)
}
x
}

`^.R6` <- function(...) {
if (is.function(.subset2(..1, ".pow"))) {
.subset2(..1, ".pow")(..2)
#' @export
`^.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__pow__"))) {
.subset2(e1, ".__pow__")(e2)
} else {
rlang::abort(
glue("No `^` method defined for R6 instance")
paste0("No `^` method defined for R6 instance")
)
}
x
}

`**.R6` <- function(...) {
if (is.function(.subset2(..1, ".pow"))) {
.subset2(..1, ".pow")(..2)
#' @export
`%%.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__mod__"))) {
.subset2(e1, ".__mod__")(e2)
} else {
rlang::abort(
glue("No `^` method defined for R6 instance")
paste0("No `%%` method defined for R6 instance")
)
}
x
}

`%%.R6` <- function(...) {
if (is.function(.subset2(..1, ".mod"))) {
.subset2(..1, ".mod")(..2)
#' @export
`%/%.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__intdiv__"))) {
.subset2(e1, ".__intdiv__")(e2)
} else {
rlang::abort(
glue("No `%%` method defined for R6 instance")
paste0("No `%/%` method defined for R6 instance")
)
}
x
}

`%/%.R6` <- function(...) {
if (is.function(.subset2(..1, ".intdiv"))) {
.subset2(..1, ".intdiv")(..2)
} else {
rlang::abort(
glue("No `%/%` method defined for R6 instance")
)
}
x
}
8 changes: 8 additions & 0 deletions R/as.data.frame.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' @export
as.data.frame.R6 <- function(x, ...) {
if (is.function(.subset2(x, ".__data__"))) {
.subset2(x, ".__data__")(...)
} else {
NextMethod("as.data.frame", x)
}
}
27 changes: 0 additions & 27 deletions R/callable.R

This file was deleted.

45 changes: 41 additions & 4 deletions R/iteration.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
iter <- function() {
NULL
#' Make R6 Instance Iterable
#'
#' @description This function uses `coro` to create a generator.
#' Using the returned object (a function), you can call this function to
#' create an iterator. The iterator can be called repeatedly
#' to produce batches of data. When all of the batches have been produced,
#' the function will return the symbol `exhausted` for each call thereafter.
#'
#' @details A `coro` iterator is compatible with `for` loops via the
#' `coro::loop()` function. `coro::collect()` can be used to collect the.
#' next `n` set of batches. Leaving `n` blank will drain all of the remaining
#' batches (assuming `length(x)` is not infinite). See `coro`
#' documentation for more details.
#'
#' @param x `R6` object.
#'
#' @export
iter <- function(x) {
stopifnot(inherits(x, "R6"))
stopifnot(
is.function(.subset2(x, ".__length__")),
is.function(.subset2(x, ".__getitem__"))
)
coro::generator(
function() {
for (i in 1:x$.__length__()) {
coro::yield(x$.__getitem__(i))
}
}
)
}

is.iter <- function() {
NULL
#' @export
#' @rdname iter
is.iterable <- function(x) {
if (
is.function(.subset2(x, ".__length__")) &
is.function(.subset2(x, ".__getitem__"))
) {
TRUE
} else {
FALSE
}
}
36 changes: 26 additions & 10 deletions R/misc.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
length
str
summary
autoplot
with
seq
format
as.list
is.na
anyNA
#' @export
length.R6 <- function(x) {
if (is.function(.subset2(x, ".__length__"))) {
.subset2(x, ".__length__")()
} else {
NextMethod("length", x)
}
}

#' @export
names.R6 <- function(x) {
if (is.function(.subset2(x, ".__names__"))) {
.subset2(x, ".__names__")()
} else {
NextMethod("names", x)
}
}

#' @export
str.R6 <- function(object, ...) {
if (is.function(.subset2(object, ".__str__"))) {
do.call(.subset2(object, ".__str__"), args = list(...))
} else {
NextMethod("str", object)
}
}
83 changes: 76 additions & 7 deletions R/relational.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
`<`
`>`
`<=`
`>=`
`==`
`!=`
all.equal
#' @export
`<.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__lt__"))) {
.subset2(e1, ".__lt__")(e2)
} else {
rlang::abort(
paste0("No `<` method defined for R6 instance")
)
}
}

#' @export
`>.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__gt__"))) {
.subset2(e1, ".__gt__")(e2)
} else {
rlang::abort(
paste0("No `>` method defined for R6 instance")
)
}
}

#' @export
`<=.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__le__"))) {
.subset2(e1, ".__le__")(e2)
} else {
rlang::abort(
paste0("No `<=` method defined for R6 instance")
)
}
}

#' @export
`>=.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__ge__"))) {
.subset2(e1, ".__ge__")(e2)
} else {
rlang::abort(
paste0("No `>=` method defined for R6 instance")
)
}
}

#' @export
`==.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__eq__"))) {
.subset2(e1, ".__eq__")(e2)
} else {
rlang::abort(
paste0("No `==` method defined for R6 instance")
)
}
}

#' @export
`!=.R6` <- function(e1, e2) {
if (is.function(.subset2(e1, ".__ne__"))) {
.subset2(e1, ".__ne__")(e2)
} else {
rlang::abort(
paste0("No `!=` method defined for R6 instance")
)
}
}

#' @export
`all.equal.R6` <- function(target, current, ...) {
if (is.function(.subset2(target, ".__ae__"))) {
.subset2(target, ".__ae__")(current, ...)
} else {
rlang::abort(
paste0("No `all.equal` method defined for R6 instance")
)
}
}
Loading

0 comments on commit 989d11c

Please sign in to comment.