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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2.9000
Depends: R (>= 4.2.0)
Imports:
R6
R6,
S7
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

export(LRUCache)
export(get_dims)
export(list_of)
export(seq_along0)
export(seq_len0)
importFrom(R6,R6Class)
importFrom(utils,getFromNamespace)
importFrom(utils,hashtab)
146 changes: 73 additions & 73 deletions R/list_of.R
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
## copied from: https://github.com/lawremi/wizrd/blob/main/R/utils.R
#
##' @title Create a list property
##' @description
##' Create a list property.
##' @param class The class of the list.
##' @param ... The properties of the list.
##' @export
#list_of <- function(class, ...) {
# new_list_property(of = class, ...)
#}
#
#new_list_property <- function(
# ...,
# validator = NULL,
# default = if (isTRUE(named)) {
# quote(setNames(list(), character()))
# } else {
# quote(list())
# },
# of = S7::class_any,
# named = NA,
# min_length = 0L,
# max_length = Inf
#) {
# prop <- S7::new_property(
# S7::class_list,
# ...,
# validator = function(value) {
# c(
# if (
# !identical(of, S7::class_any) &&
# !all(vapply(value, S7:::class_inherits, logical(1L), of))
# ) {
# paste("must only contain elements of class", S7:::class_desc(of))
# },
# if (!is.null(of_validator)) {
# msgs <- unlist(lapply(value, of_validator))
# if (length(msgs) > 0L) {
# paste(
# "element(s) failed validation:",
# paste0("'", unique(msgs), "'", collapse = ", ")
# )
# }
# },
# if (isTRUE(named) && is.null(names(value))) {
# "must have names"
# },
# if (identical(named, FALSE) && !is.null(names(value))) {
# "must not have names"
# },
# if (length(value) < min_length || length(value) > max_length) {
# paste0("must have length in [", min_length, ", ", max_length, "]")
# },
# if (!is.null(validator)) {
# validator(value)
# }
# )
# },
# default = default
# )
# prop$of <- of
# if (inherits(of, "S7_property")) {
# of_validator <- of$validator
# of <- of$class
# } else {
# of_validator <- NULL
# }
# prop$named <- named
# class(prop) <- c("list_S7_property", class(prop))
# prop
#}
#
# copied from: https://github.com/lawremi/wizrd/blob/main/R/utils.R

#' @title Create a list property
#' @description
#' Create a list property.
#' @param class The class of the list.
#' @param ... The properties of the list.
#' @export
list_of <- function(class, ...) {
new_list_property(of = class, ...)
}

new_list_property <- function(
...,
validator = NULL,
default = if (isTRUE(named)) {
quote(setNames(list(), character()))
} else {
quote(list())
},
of = S7::class_any,
named = NA,
min_length = 0L,
max_length = Inf
) {
prop <- S7::new_property(
S7::class_list,
...,
validator = function(value) {
c(
if (
!identical(of, S7::class_any) &&
!all(vapply(value, getFromNamespace("class_inherits", "S7"), logical(1L), of))
) {
paste("must only contain elements of class", getFromNamespace("class_desc", "S7")(of))
},
if (!is.null(of_validator)) {
msgs <- unlist(lapply(value, of_validator))
if (length(msgs) > 0L) {
paste(
"element(s) failed validation:",
paste0("'", unique(msgs), "'", collapse = ", ")
)
}
},
if (isTRUE(named) && is.null(names(value))) {
"must have names"
},
if (identical(named, FALSE) && !is.null(names(value))) {
"must not have names"
},
if (length(value) < min_length || length(value) > max_length) {
paste0("must have length in [", min_length, ", ", max_length, "]")
},
if (!is.null(validator)) {
validator(value)
}
)
},
default = default
)
prop$of <- of
if (inherits(of, "S7_property")) {
of_validator <- of$validator
of <- of$class
} else {
of_validator <- NULL
}
prop$named <- named
class(prop) <- c("list_S7_property", class(prop))
prop
}

1 change: 1 addition & 0 deletions R/xlamisc-package.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#' @importFrom R6 R6Class
#' @importFrom utils getFromNamespace
"_PACKAGE"
16 changes: 16 additions & 0 deletions man/list_of.Rd

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