# Tutorial: Parallel programming with foreach

August 30, 2013
By

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

Exegetic Analytics extols the wonders of foreach package for iterative operations that go beyond the standard "for" loop in R. For example, here's a neat (if not optimally efficient) construct using filters to calculate the primes less than 100:

foreach(n = 1:100, .combine = c) %:% when (isPrime(n)) %do% n

The open-source team at Revolution Analytics created the foreach package with parellelization in mind, and the tutorial goes on to use the doMC and doSNOW packages to speed up the foreach-based loops by running the iterations in parallel on a cluster. You can find the code used to do so, and benchmarks of the results, at the blog post linked below.

Exegetic Analytics: The Wonders of foreach