Tail-Recursion to get a Map of word counts I want to read a file and store the number of occurrences of each word in a Map, using tail recursion. I came up with the following; it seems to work; does it look like it's right?

def countWordsinFile(reader:java.util.Scanner, wordCounts:Map[String,Int]): Map[String,Int] = {
    if (!reader.hasNext()) wordCounts
    else {
        val word = reader.next()
        countWordsinFile(reader, wordCounts + (word -&gt; {wordCounts.getOrElse(word,0)+1}))
    }
}
// the function is called with a reader from a .txt file and wordCounts is an empty Map()


Is this tail-recursive? If not, then how can I make it so?
Yes, this is tail-recursive. Just annotate countWordsInFile with @tailrec and the compiler will check that for you.
Btw: why not name the method countWords? The Scanner is not limited to read from files.