I had asked this question on Javaranch, but couldn’t get a response there. So posting it here as well:
I have this particular requirement where the increment in the loop variable is to be done by multiplying it with 5 after each iteration. In Java we could implement it this way:
for(int i=1;i<100;i=i*5){}
In scala I was trying the following code-
var j=1
for(i<-1.to(100).by(scala.math.pow(5,j).toInt))
{
println(i+" "+j)
j=j+1
}
But its printing the following output:
1 1
6 2
11 3
16 4
21 5
26 6
31 7
36 8
….
….
Its incrementing by 5 always. So how do I got about actually multiplying the increment by 5 instead of adding it.
Let’s first explain the problem. This code:
is equivalent to this:
So, by the time you get to mutate
j,incrementandbyRangehave already been computed. AndRangeis an immutable object — you can’t change it. Even if you produced new ranges while you did theforeach, the object doing theforeachwould still be the same.Now, to the solution. Simply put,
Rangeis not adequate for your needs. You want a geometric progression, not an arithmetic one. To me (and pretty much everyone else answering, it seems), the natural solution would be to use aStreamorIteratorcreated withiterate, which computes the next value based on the previous one.EDIT: About Stream vs Iterator
StreamandIteratorare very different data structures, that share the property of being non-strict. This property is what enablesiterateto even exist, since this method is creating an infinite collection1, from whichtakeWhilewill create a new2 collection which is finite. Let’s see here:These infinite collections are possible because the collection is not pre-computed. On a
List, all elements inside the list are actually stored somewhere by the time the list has been created. On the above examples, however, only the first element of each collection is known in advance. All others will only be computed if and when required.As I mentioned, though, these are very different collections in other respects.
Streamis animmutabledata structure. For instance, you can print the contents ofs2as many times as you wish, and it will show the same output every time. On the other hand,Iteratoris a mutable data structure. Once you used a value, that value will be forever gone. Print the contents ofi2twice, and it will be empty the second time around:Stream, on the other hand, is alazycollection. Once a value has been computed, it will stay computed, instead of being discarded or recomputed every time. See below one example of that behavior in action:So
Streamcan actually fill up the memory if one is not careful, whereasIteratoroccupies constant space. On the other hand, one can be surprised byIterator, because of its side effects.(1) As a matter of fact,
Iteratoris not a collection at all, even though it shares a lot of the methods provided by collections. On the other hand, from the problem description you gave, you are not really interested in having a collection of numbers, just in iterating through them.(2) Actually, though
takeWhilewill create a newIteratoron Scala 2.8.0, this new iterator will still be linked to the old one, and changes in one have side effects on the other. This is subject to discussion, and they might end up being truly independent in the future.