Run functions using regular expressions on the entire vector, not on individual values. The Regex code can be compiled at runtime and run much faster. It is also a better design.
Existing Code
# - comments are also ugly formatted;
else { #block_names were not provided, infer from filename
infer the names from filenames, stripping off the extension from end
# and the dot at the beginning (if any)
outputs[[i]]$metadata["block_name"] <-
sub("^\\.?/?(.*)\\.[[:alnum:]]+$", "\\1", files[i])
}
Modified Design
# - move code outside the for-loop;
# - can be also inside a new utility function;
if(is.null(block_names)) {
# Why not save the results directly in block_names?
# Nice comments as well:
# Block_names were not provided:
# - infer the names from filenames;
# - strip off the extension from end
# and the dot at the beginning (if any);
block_names = sub("^\.?/?(.*)\.[[:alnum:]]+$", "\1", files);
}
You can use now block_names in the for-loop directly, without checking for NULL anymore.
Note:
move the whole processing of meta-data to new utility functions: will be a separate issue;
GitHub fails to preserve spaces/indentation in the code: but the comments are still nicer;
Regular Expressions
Run functions using regular expressions on the entire vector, not on individual values. The Regex code can be compiled at runtime and run much faster. It is also a better design.
Existing Code
# - comments are also ugly formatted; else { #block_names were not provided, infer from filename
infer the names from filenames, stripping off the extension from end
Modified Design
# - move code outside the for-loop; # - can be also inside a new utility function; if(is.null(block_names)) { # Why not save the results directly in block_names? # Nice comments as well: # Block_names were not provided: # - infer the names from filenames; # - strip off the extension from end # and the dot at the beginning (if any); block_names = sub("^\.?/?(.*)\.[[:alnum:]]+$", "\1", files); }
You can use now block_names in the for-loop directly, without checking for NULL anymore.
Note: