MathMarEcol / Welcome

A repo for the MME labs markdown book outlining all the protocols, procedures and misc information
https://mathmarecol.github.io/Welcome/
0 stars 2 forks source link

Can we avoid for loops? #6

Open PhDyellow opened 5 years ago

PhDyellow commented 5 years ago

@jaseeverett, I notice in the programming section, we teach people to use for loops. For parallel processing, it is much better to be using lapply.

We could also use foreach.

Moving from a for loop to lapply or foreach is a major refactor, because the for loop expects variables outside the loop to be initialised, and expects to be modifying those variables during the loop. lapply and foreach create a new scope inside, so that generally you use them as functions that take inputs and return lists.

Moving from lapply to parLapply or future_lapply is only a minor refactor, with an extra line of code at the start of the script to specify the parallel model.

jaseeverett commented 5 years ago

The programming section (and the Github and Rstudio sections) are direct copies from the OHI book. I intend to go back and adapt them more thoroughly for our purposes.

Your title suggests you are advocating to not teach for loops? I agree with your sentiments re it not being useful for parallel processing, but for new programmers I think the for loop is conceptually much easier to grasp.

I would advocate for keeping the Studio and programming sections for beginners (so maybe I need to remove some irrelevant content) and those who may have come from other languages where for loops are directly analogous (such as myself who came from a MATLAB background only 12 months ago).

But I think we should include a new chapter in the book about moving to parallel processing. Some basic tips, a list of functions to substitute, packages to investigate etc.

I'll add it as a TODO. I wanted to get some bare bones together, and then at a future lab meeting I'm going to call for volunteers to modify chapters to suit our needs.