The Fortress programming language has some very powerful features. Guy Steele's slides here (slides 52 onwards) illustrate some of its features including how to split a sentence into words in parallel.
Here we look at some ways to use Groovy and GPars to solve that problem.
First we can write an imperative sequential version:
And test it as follows:
Now, we can ask ourselves a question. Is this solution in its current form easy to turn into a parallel solution? The answer is NO. Each step in our loop relies on the result from the previous step. So, if I split the sentence into say two pieces, and I am processing the start of the second piece, I don't know whether a character or space finished the previous piece. I.e. the order in which I process information is important in the current algorithm and to make it work easily in parallel, we need to change the algorithm to be equivalent but where order is no longer important. Technically we require that
So what we need to do (credit to Guy's article) is alter our algorithm so that it can be
Technically, we want to alter our algorithm to be associative. Or stating it another way, the order in which I process information is important in the current algorithm.
This produces the output:
and gives me confidence that my algorithm is a monoid.