1

I am trying to learn Scala, so can anyone tell me how to convert the following in scala:

for (int t = 0; true; t++)

Thank you in advance.

2
  • 3
    This doesn't make sense. The Java code you presented is the head declaration of an infinite loop, which counts the variable t upwards, starting from zero. But you left out the body of the loop. What is your loop supposed to do? When will it stop? Commented May 8, 2012 at 12:07
  • 1
    Oddly enough, i think this is something which is more natural in Scala than in Java - in Scala, you can write this as an iteration (or a mapping or whatever) over an infinite collection containing the natural numbers. Commented May 8, 2012 at 12:11

4 Answers 4

19

With imperative style you can write (as you do in Java):

var t = 0
while(true) {
  t+=1
  ...
}

With lazy functional this could be:

def ints(n: Int = 0): Stream[Int] = Stream.cons(n, ints(n+1))
ints().map(t => ...)

Using built-in functions:

Iterator.from(0).map ( t => .... )

The common use case with such infinite structures, is to take infinite stream or iterator, perform some operations on it, and then take number of results:

Iterator.from(0).filter(t => t % 1 == 0).map(t => t*t).take(10).toList 
Sign up to request clarification or add additional context in comments.

3 Comments

As far as I know there is no ++ method on Int in Scala, you should write t += 1 instead of t++.
I'd use foreach instead of map, since the latter creates a new collection, so will eat memory particularly if what you're doing is infinite, i.e. Iterator from 0 foreach { t => ... }.
@LuigiPlinge yes, I though the same (the first version of answer was with foreach), but then I though that side effects with infinite structures is a dangerous thing, especially for novice
8

As I mentioned in the comments, your question does not seem to make much sense - please add more detail.

For now, the closest Scala translation I can come up with would be:

Stream from 0

1 Comment

That does much more than OP's code. I propose while (true) :)
4

You can use while or for.

You can use for

for(i<-0 to 100) {
  println(i)
}

or you use until when you want to increment by N number

for(i <- 5 until 55 by 5) {
    println(i)
}

or you better use while

var i = 0
while(true) {
  ...
  i+=1
}

or also do-while

var i = 0
do {
    ...
    i += 1
} while(true)

Have a look at : http://www.simplyscala.com/ and test it out by yourself

Also, in my blog I did some posts about imperative scala where I used for and while loops you can have a look there.

http://carlosqt.blogspot.com/search/label/Scala

Comments

2

A simple for comprehension in scala looks mostly this way:

for (i <- 0 until 10) {
  // do some stuff
}

3 Comments

I don't recommend telling people to use for-comprehension as a surrogate for for loops.
I agree with you, but I thought this might be something he can start with.
I don't see the problem; using while-loops instead seems like premature optimization. (Programming in Scala refers to for-expressions without a yield as "for-loops", so it seems they were intended to be used as such.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.