sys.info <- function() { Sys.info() }
sys.getenv <- function() { Sys.getenv() }
sys.setenv <- function(var, val) { Sys.setenv(var = val) }
sys.time <- function() { Sys.time() }
sys.date <- function() { Sys.Date() }
sys.sleep <- function(n) { Sys.sleep(n) }
sys.which <- function(s) { Sys.which(s) }
sys.timezone <- function() { Sys.timezone() }
sys.setlocale <- function() { Sys.setlocale() }
struct <- function(x, new_class) {
if (is.null(x)) {
return(x)
}
old <- oldClass(x)
if (is.null(old)) {
class(x) <- new_class
} else {
class(x) <- union(old, new_class)
}
return(x)
}
let_type <- function(x, new_class) {
class(x) <- ""
class(x) <- x |> new_class()
return(x)
}
typed_vec <- function(...) {
x <- list(...)
all_typed <- all(vapply(x, function(item) inherits(item, "typed_vec"), logical(1)))
if (all_typed && length(x) > 0) {
combined_data <- unlist(lapply(x, function(item) item$data), recursive = FALSE)
return(structure(
list(data = combined_data),
class = "typed_vec"
))
}
structure(
list(data = x),
class = "typed_vec"
)
}
length.typed_vec <- function(x) {
length(x$data)
}
`[[.typed_vec` <- function(x, i) {
x$data[[i]]
}
apply.typed_vec <- function(X, FUN, ...) {
results <- lapply(X$data, FUN, ...)
typed_vec(results)
}
vec_apply <- function(f, ...) {
args <- list(...)
args <- lapply(args, function(x) {
if (!inherits(x, "typed_vec")) {
typed_vec(x)
} else {
x
}
})
lengths <- vapply(args, length, integer(1))
n <- max(lengths)
if (any(lengths == 0)) {
return(structure(
list(data = list()),
class = "typed_vec"
))
}
if (any(n %% lengths != 0)) {
stop("Incompatible vector lengths")
}
recycled <- lapply(args, function(x) {
if (length(x) == n) {
x$data
} else {
rep(x$data, length.out = n)
}
})
results <- vector("list", n)
for (i in seq_len(n)) {
elements <- lapply(recycled, `[[`, i)
results[[i]] <- do.call(f, elements)
}
all_typed <- all(vapply(results, function(item) inherits(item, "typed_vec"), logical(1)))
if (all_typed && length(results) > 0) {
combined_data <- unlist(lapply(results, function(item) item$data), recursive = FALSE)
return(structure(
list(data = combined_data),
class = "typed_vec"
))
}
structure(
list(
data = results
),
class = "typed_vec"
)
}
vec_apply_fun <- function(fun_vec, ...) {
if (!inherits(fun_vec, "typed_vec")) {
fun_vec <- typed_vec(fun_vec)
}
args <- list(...)
args <- lapply(args, function(x) {
if (!inherits(x, "typed_vec")) {
typed_vec(x)
} else {
x
}
})
lengths <- c(length(fun_vec), vapply(args, length, integer(1)))
n <- max(lengths)
if (any(lengths == 0)) {
return(structure(
list(data = list()),
class = "typed_vec"
))
}
if (any(n %% lengths != 0)) {
stop("Incompatible vector lengths")
}
funs <- if (length(fun_vec) == n)
fun_vec$data
else
rep(fun_vec$data, length.out = n)
recycled_args <- lapply(args, function(x) {
if (length(x) == n) x$data
else rep(x$data, length.out = n)
})
results <- vector("list", n)
for (i in seq_len(n)) {
f <- funs[[i]]
params <- lapply(recycled_args, `[[`, i)
results[[i]] <- do.call(f, params)
}
all_typed <- all(vapply(results, function(item) inherits(item, "typed_vec"), logical(1)))
if (all_typed && length(results) > 0) {
combined_data <- unlist(lapply(results, function(item) item$data), recursive = FALSE)
return(structure(
list(data = combined_data),
class = "typed_vec"
))
}
structure(
list(data = results),
class = "typed_vec"
)
}
reduce.typed_vec <- function(vec, f, init = NULL) {
if (!inherits(vec, "typed_vec")) {
vec <- typed_vec(vec)
}
n <- length(vec)
if (n == 0) {
if (is.null(init)) {
stop("Cannot reduce empty vector without initial value")
}
return(init)
}
if (is.null(init)) {
accumulator <- vec$data[[1]]
start_index <- 2
} else {
accumulator <- init
start_index <- 1
}
if (start_index > n) {
return(accumulator)
}
for (i in start_index:n) {
accumulator <- f(accumulator, vec$data[[i]])
if (inherits(accumulator, "typed_vec")) {
accumulator <- accumulator$data[[1]]
}
}
return(structure(
list(data = list(accumulator)),
class = "typed_vec"
))
}
sum.typed_vec <- function(x, ...) {
reduce(x, `+`)
}
print.typed_vec <- function(x, ...) {
n <- length(x$data)
if (n == 0) {
cat("Empty typed_vec\n")
return(invisible(x))
}
if (n == 1) {
el <- x$data[[1]]
if (is.function(el)) {
cat("<function>\n")
} else {
print(el)
}
return(invisible(x))
}
cat("typed_vec [", n, "]\n", sep = "")
for (i in seq_len(n)) {
cat("[", i, "] ", sep = "")
el <- x$data[[i]]
if (is.function(el)) {
fname <- tryCatch(
deparse(substitute(el)),
error = function(e) "<function>"
)
cat("<function>\n")
} else {
print(el)
}
if (i < n) cat("\n")
}
invisible(x)
}
get.typed_vec <- function(a, name) {
a$data[[1]][[name]]
}
get.data <- function(a, name) {
a$data[[1]]
}
get.list <- function(a, name) {
a$data[[1]][[name]]
}
get.any <- function(a, name) {
a[[name]]
}
print.Integer <- function(i) {
cat(unclass(i))
invisible(i)
}
print.Character <- function(c) {
cat(unclass(c))
invisible(c)
}
print.Boolean <- function(b) {
cat(unclass(b))
invisible(b)
}
print.Number <- function(n) {
cat(unclass(n))
invisible(n)
}
`%==%.default` <- function(x, y) {
unclass(x) == unclass(y)
}