scala - Populating an immutable List -
Here I populate two lists where each list is either unstable or unchanging:
< Code> var mutableList = Scala.Collection.mutable.mustable list [string] () // & gt; MutableList: scala.collection.mutable.MutableList [string] = MutableList () // | For (a & lt; - 1 to 100) {mutableList + = a.toString} println (mutableList.size); // & gt; 100 Variable Unchanged List = List [String] () // & gt; Invariant list: list [string] = list () (a & lt; - 1 to 100) {non-inventory list: + a.toString} println (unchanging album); // & gt; 0 When I print the size of the irreversible, enter its output 0. The reason for this is that a new reference has been made inside the loop, which does not point to an invariant list? Is there a functional equivalent to populate an inventory from inside the loop?
As Gabor responds in a comment, you use fold Want to do this, or want to continue with more yield , what does not explain why you are getting size 0. The reason is that unchanging list: + a.toString is returning a new list every time, which you are not using. The unchanging list is exactly the same, irreversible.
Keep in mind that everything in Scala has expression and therefore gives something. Therefore, you can understand by adding produce as
forEach ) to your regular val is immutable = (for a & lt; -1 to 100) yield a.toString like some desugars like this:
(1 to 100) .map (_toString)