title: Exercises in programming style with Scala
body: <p>I've started to read the "<a href="https://github.com/crista/exercises-in-programming-style" rel="nofollow">Exercises in programming style</a>" book recently and one of the tasks there is to implement each programming style in a language of your choice. I decided to go with Scala (I'm fairly new to it) and I'm already stuck with the first "good old school" style. The constraints are:</p>
  
  <blockquote>
    <ul>
    <li><p>Very small amount of primary memory, typically orders of magnitude   smaller than the data that needs to be processed/generated.    (The example sets the limit to 1024 cells)</p></li>
    <li><p>No labels -- i.e. no variable names or tagged memory addresses. All we have is memory that is addressable with numbers.</p></li>
    </ul>
  </blockquote>
  
  <p>Original example (which reads a file line by line and counts the words) is in Python and goes like this:</p>
  
  <pre><code>data = []
  data.append([])    # data[1] is line (max 80 characters)
  data.append(None)  # data[2] is index of the start_char of word
  data.append(0)     # data[3] is index on characters, i = 0
  data.append(False) # data[4] is flag indicating if word was found
  data.append('')    # data[5] is the word
  data.append('')    # data[6] is word,NNNN
  data.append(0)     # data[7] is frequency
  ...
  
  f = open(sys.argv[1])
  # Loop over input file's lines
  while True:
      data[1] = [f.readline()]
  ...
  </code></pre>
  
  <p>So we see there are some variables (f and data) but the main idea is to keep it to a minimum and use python array as a bunch of "memory addresses".</p>
  
  <p>Is it even possible to implement old-school-programming style (no variable names or tagged memory addresses) in Scala? Specifically is there a way to avoid "line" variable when reading file content?</p>
  
  <pre><code>for (line &lt;- Source.fromFile("example.txt").getLines) {
    println(line.toUpperCase)
  }
  </code></pre>
  
  <p>Reading the file content into an array similar to the original example doesn't work because it's doesn't have an extractor (<em>value data is not a case class, nor does it have an unapply/unapplySeq member</em>).</p>
  
  <p>P.S. I'm very well aware of the fact that the whole task is probably a 5-liner in Scala but that's not the point.</p>