0
val indices: List[Int] = List() val featValues: List[Double] = List() for (f <- feat) { val q = f.split(':') if (q.length == 2) { println(q.mkString("\n")) // works fine, displays info indices :+ (q(0).toInt) featValues :+ (q(1).toDouble) } } println(indices.mkString("\n") + indices.length) // prints nothing and 0? 

indices and featValues are not being filled. I'm at a loss here.

2
  • Why does your question title mention "array" when there are no Array in evidence? Commented Apr 24, 2014 at 17:45
  • I was going back and forth between the two thinking that it was similar to Java to see if that would correct the issue. Just confusion on my part. Commented Apr 24, 2014 at 18:20

1 Answer 1

5

You cannot append anything to an immutable data structure such as List stored in a val (immutable named slot).

What your code is doing is creating a new list every time with one element appended, and then throwing it away (by not doing anything with it) — the :+ method on lists does not modify the list in place (even when it's a mutable list such as ArrayBuffer) but always returns a new list.

In order to achieve what you want, the quickest way (as opposed to the right way) is either to use a var (typically preferred):

var xs = List.empty[Int] xs :+= 123 // same as `xs = xs :+ 123` 

or a val containing a mutable collection:

import scala.collection.mutable.ArrayBuffer val buf = ArrayBuffer.empty[Int] buf += 123 

However, if you really want to make your code idiomatic, you should instead just use a functional approach:

val indiciesAndFeatVals = feat.map { f => val Array(q0, q1) = f.split(':') // pattern matching in action (q0.toInt, q1.toDouble) } 

which will give you a sequence of pairs, which you can then unzip to 2 separate collections:

val (indicies, featVals) = indiciesAndFeatVals.unzip 

This approach will avoid the use of any mutable data structures as well as vars (i.e. mutable slots).

Sign up to request clarification or add additional context in comments.

4 Comments

Just wondering then, what about this: stackoverflow.com/questions/7500081/…
What are you referring to specifically? The use of Array?
Yes, is it not immutable?
You are right: Array is not immutable; it's the same old array as you have in Java; I would not use it in newly written Scala code unless you need to for performance or compatibility reasons. There are not many cases where a purely functional/immutable approach isn't as good or better than an imperative/mutable one. Typically you only resort to an algorithm that uses mutability when you absolutely have to, e.g. for optimization once you've determined the bottlenecks in your code, or if it's just much simpler (which can be the case with some algorithms).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.