Skip to content

Commit 7d29070

Browse files
authored
Merge pull request #76 from t-kalinowski/refactor-iterable-seq
Refactor iterable handling for seq
2 parents b6d63d6 + f3371af commit 7d29070

7 files changed

Lines changed: 555 additions & 279 deletions

File tree

R/r2f-closures.R

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ is_function_call <- function(x) {
7171

7272
is_sapply_call <- function(x) is.call(x) && identical(x[[1L]], quote(sapply))
7373

74-
is_seq_along_call <- function(x) {
75-
is.call(x) && identical(x[[1L]], quote(seq_along))
76-
}
77-
7874

7975
new_local_closure <- function(fun, name = NULL) {
8076
stopifnot(is.function(fun), is.null(name) || is_string(name))
@@ -668,8 +664,8 @@ compile_sapply_assignment <- function(
668664

669665
if (index_iterable) {
670666
seq_val <- r2f(seq_call, scope, calls = "for", hoist = hoist)
671-
if (!is.null(seq_val@value) && seq_val@value@rank == 1L) {
672-
iterable_len_expr <- seq_val@value@dims[[1L]]
667+
if (!is.null(seq_val@value)) {
668+
iterable_len_expr <- value_length_expr(seq_val@value)
673669
}
674670
formal_vars <- list(
675671
Variable(mode = "integer", name = formal_names[[1L]])
@@ -682,73 +678,7 @@ compile_sapply_assignment <- function(
682678
}
683679
iterable_value <- iterable_val@value
684680

685-
infer_iterable_len <- function(expr, value) {
686-
while (is_call(expr, quote(`(`)) && length(expr) == 2L) {
687-
expr <- expr[[2L]]
688-
}
689-
690-
if (passes_as_scalar(value)) {
691-
return(1L)
692-
}
693-
694-
if (is_call(expr, quote(`:`)) && length(expr) == 3L) {
695-
args <- whole_doubles_to_ints(as.list(expr)[-1L])
696-
start <- args[[1L]]
697-
end <- args[[2L]]
698-
return(call("+", call("abs", call("-", end, start)), 1L))
699-
}
700-
701-
if (is_call(expr, quote(seq))) {
702-
ee <- match.call(seq.default, expr)
703-
ee <- whole_doubles_to_ints(ee)
704-
from <- ee$from
705-
to <- ee$to
706-
by <- ee$by
707-
708-
if (!is.null(from) && !is.null(to) && identical(from, to)) {
709-
return(1L)
710-
}
711-
712-
if (!is.null(by) && is_scalar_integerish(by)) {
713-
by_val <- as.integer(by)
714-
if (by_val == 0L) {
715-
stop("invalid '(to - from)/by'", call. = FALSE)
716-
}
717-
}
718-
719-
if (is_scalar_integerish(from) && is_scalar_integerish(to)) {
720-
from_val <- as.integer(from)
721-
to_val <- as.integer(to)
722-
delta <- to_val - from_val
723-
if (delta == 0L) {
724-
return(1L)
725-
}
726-
if (!is.null(by) && is_scalar_integerish(by)) {
727-
by_val <- as.integer(by)
728-
if (sign(delta) != sign(by_val)) {
729-
stop("wrong sign in 'by' argument", call. = FALSE)
730-
}
731-
return(abs(delta %/% by_val) + 1L)
732-
}
733-
}
734-
735-
if (is.null(by)) {
736-
return(call("+", call("abs", call("-", to, from)), 1L))
737-
}
738-
return(call("+", call("abs", call("%/%", call("-", to, from), by)), 1L))
739-
}
740-
741-
dims <- value@dims
742-
if (is.null(dims) || any(map_lgl(dims, is_scalar_na))) {
743-
return(NA_integer_)
744-
}
745-
if (value@rank == 1L) {
746-
return(dims[[1L]])
747-
}
748-
reduce(dims, \(d1, d2) call("*", d1, d2))
749-
}
750-
751-
iterable_len_expr <- infer_iterable_len(seq_call, iterable_value)
681+
iterable_len_expr <- value_length_expr(iterable_value)
752682

753683
iterable_tmp <- scope@get_unique_var(
754684
mode = iterable_value@mode,

0 commit comments

Comments
 (0)