Synopsis Count words in a line.
Examples Here is a clever, albeit rather dense, solution that illustrates several Rascal concepts.
module demo::common::WordCount::CountInLine3

public int countInLine3(str S){
  return (0 | it + 1 | /\w+/ := S);
We use a Rascal:Reducer that is a recipe to reduce the values produced by one or more generators to a single value:
  • 0 is the initial value of the reducer
  • The pattern match /\w+/ := S matches all words in S.
  • Reduction is done by it + 1. In the latter it is a keyword that refers to the value that has been reduced sofar. Effectively, the matches are reduced to a match count.
Let's try it:
rascal>import demo::common::WordCount::CountInLine3;
rascal>countInLine3("Jabberwocky by Lewis Carroll");
int: 4
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit