Skip to content

Commit d62aac9

Browse files
Early returns to reduce nesting and improve readability (#6542)
1 parent 7419796 commit d62aac9

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

R/programming.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ list2lang = function(x) {
1414
stopf("'x' must be a list")
1515
if (is.AsIs(x))
1616
return(rm.AsIs(x))
17-
asis = vapply(x, is.AsIs, FALSE)
18-
char = vapply(x, is.character, FALSE)
17+
asis = vapply_1b(x, is.AsIs)
18+
char = vapply_1b(x, is.character)
1919
to.name = !asis & char
2020
if (any(to.name)) { ## turns "my_name" character scalar into `my_name` symbol, for convenience
2121
if (any(non.scalar.char <- lengths(x[to.name])!=1L)) {
@@ -24,7 +24,7 @@ list2lang = function(x) {
2424
x[to.name] = lapply(x[to.name], as.name)
2525
}
2626
if (isTRUE(getOption("datatable.enlist", TRUE))) { ## recursively enlist for nested lists, see note section in substitute2 manual
27-
islt = vapply(x, only.list, FALSE) #5057 nested DT that inherits from a list must not be turned into list call
27+
islt = vapply_1b(x, only.list) #5057 nested DT that inherits from a list must not be turned into list call
2828
to.enlist = !asis & islt
2929
if (any(to.enlist)) {
3030
x[to.enlist] = lapply(x[to.enlist], enlist)

src/programming.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
#include "data.table.h"
22

33
static void substitute_call_arg_names(SEXP expr, SEXP env) {
4-
R_len_t len = length(expr);
5-
if (len && isLanguage(expr)) { // isLanguage is R's is.call
6-
SEXP arg_names = getAttrib(expr, R_NamesSymbol);
7-
if (!isNull(arg_names)) {
8-
SEXP env_names = getAttrib(env, R_NamesSymbol);
9-
int *imatches = INTEGER(PROTECT(chmatch(arg_names, env_names, 0)));
10-
const SEXP *env_sub = SEXPPTR_RO(env);
11-
SEXP tmp = expr;
12-
for (int i=0; i<length(arg_names); i++, tmp=CDR(tmp)) { // substitute call arg names
13-
if (imatches[i]) {
14-
SEXP sym = env_sub[imatches[i]-1];
15-
if (!isSymbol(sym))
16-
error(_("Attempting to substitute '%s' element with object of type '%s' but it has to be 'symbol' type when substituting name of the call argument, functions 'as.name' and 'I' can be used to work out proper substitution, see ?substitute2 examples."), CHAR(STRING_ELT(arg_names, i)), type2char(TYPEOF(sym)));
17-
SET_TAG(tmp, sym);
18-
}
19-
}
20-
UNPROTECT(1); // chmatch
21-
}
22-
for (SEXP tmp=expr; tmp!=R_NilValue; tmp=CDR(tmp)) { // recursive call to substitute in nested expressions
23-
substitute_call_arg_names(CADR(tmp), env);
4+
if (!length(expr) || !isLanguage(expr))
5+
return; // isLanguage is R's is.call
6+
SEXP arg_names = getAttrib(expr, R_NamesSymbol);
7+
if (!isNull(arg_names)) {
8+
SEXP env_names = getAttrib(env, R_NamesSymbol);
9+
int *imatches = INTEGER(PROTECT(chmatch(arg_names, env_names, 0)));
10+
const SEXP *env_sub = SEXPPTR_RO(env);
11+
SEXP tmp = expr;
12+
for (int i=0; i<length(arg_names); i++, tmp=CDR(tmp)) { // substitute call arg names
13+
if (!imatches[i])
14+
continue;
15+
SEXP sym = env_sub[imatches[i]-1];
16+
if (!isSymbol(sym))
17+
error(_("Attempting to substitute '%s' element with object of type '%s' but it has to be 'symbol' type when substituting name of the call argument, functions 'as.name' and 'I' can be used to work out proper substitution, see ?substitute2 examples."), CHAR(STRING_ELT(arg_names, i)), type2char(TYPEOF(sym)));
18+
SET_TAG(tmp, sym);
2419
}
20+
UNPROTECT(1); // chmatch
21+
}
22+
for (SEXP tmp=expr; tmp!=R_NilValue; tmp=CDR(tmp)) { // recursive call to substitute in nested expressions
23+
substitute_call_arg_names(CADR(tmp), env);
2524
}
2625
}
26+
2727
SEXP substitute_call_arg_namesR(SEXP expr, SEXP env) {
2828
SEXP ans = PROTECT(MAYBE_REFERENCED(expr) ? duplicate(expr) : expr);
2929
substitute_call_arg_names(ans, env); // updates in-place

0 commit comments

Comments
 (0)