Aserian
Aserian

Reputation: 1127

Keeping a counter immutably in scala?

My professor has a rule that if we use any mutation whatsoever, we get a 0 on the assignment. This means I cannot keep a simple counter. Is there some way to be able to keep a counter immutably?

I should note that the assignment is not "create an immutable counter in scala", it is somewhat more involved, this is just a hiccup I've come across.

Upvotes: 0

Views: 133

Answers (1)

Aserian
Aserian

Reputation: 1127

The answer by Luis Miguel Mejía Suárez was what I needed,

val lineCount = longFileIterator.foldLeft(0)((acc, line) => {
  //process line
  acc + 1
})

Upvotes: 1

Related Questions