You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OK -- not quite accurate, since using map or apply is just a for loop under the hood.
But, the point is that for loops that have significant logic in them are difficult to debug.
What is better is to take a page from the book of functional programming and write the
logic which executes on any given iteration of the loop into a function (or a number of functions)
and then apply that function(s) over the input list.
for (iin1:length(metric_dfs_by_net[[1]])) {
metric_dfs[[i]] <- do.call(cbind, lapply(unname(metric_dfs_by_net), "[[", i))
}
# assemble y-axis labelsto_plot<- c(sum, pe...# it goes on for many lines ....
write the logic inside of that loop into a function
The text was updated successfully, but these errors were encountered:
cmatKhan
changed the title
Avoid for loops in functions
Avoid significant logic inside of for loops
Jul 17, 2023
I'm not sure what exactly is happening here -- there are some nested for loops and the if statement is long. will need to run the code.
But, rather than using a print statement, in general using a logger is cleaner. Gives both you and the user a lot more control over how, where and when the message is printed. I like futile.logger in R.
OK -- not quite accurate, since using
map
orapply
is just a for loop under the hood.But, the point is that for loops that have significant logic in them are difficult to debug.
What is better is to take a page from the book of functional programming and write the
logic which executes on any given iteration of the loop into a function (or a number of functions)
and then apply that function(s) over the input list.
So, plot_metrics, rather than this:
write the logic inside of that loop into a function
The text was updated successfully, but these errors were encountered: