2

I am learning scala, and I am trying to do the equivalent of this C++ code in scala, but I get a compile error. Here's the c++ code:

for(reels[0] = 0; reels[0] < 10; reels[0]++)
  doStuff();

I have tried to implement this loop in scala like:

var reels: Array[Int] = new Array[Int](5)

for(reels(0) <- 0 until 10)
  doStuff

This results in a compile error on the for loop:

variable reels is not a case class, nor does it have an unapply/unapplySeq member

If I replace reels(0) with a regular Int variable, it works fine. Why is that, and more importantly, how would I iterate the array element. More specifically, what I want to do is nest 5 loops, and have each loop iterate exactly one of the reels one at a time. I do not want to define 5 separate variables to get around the problem.

Thanks in advance.

Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
Victor
  • 35
  • 4
  • 1
    This SO post has nice explanation of <- scroll down past accepted answer to longer version with more explanation http://stackoverflow.com/questions/3754089/scala-for-comprehension – Barry Dec 23 '14 at 21:25
  • While James DW already answered your question, I would strongly advise you to start trying to think in a functional way. I am saying so, as you firstly tried using `Array[T]` and store it in a `var`, which are both mutable structures. Learning Scala by applying Object-oriented or Imperative paradigms you are used from C++ might feel comfortable. Scalas true power, especially if it comes to lists like in your example, lies in collection combinators or (tail)recursive calls though. – Alexander Weber Dec 23 '14 at 23:26
  • Actually, I don't believe my question has been answered. What I have is 5 reels, but for simplicity, let's say 2 reels, each with 10 stops. I want to say: reels(0) goes from 0 to 9, and then reels(1) goes from 0 to 9. I don't want a variable to go from 0 go 1 (ie go to each reel). Thanks. – Victor Dec 24 '14 at 07:42

5 Answers5

1

This might be what you want:

// create your list of numbers
val reels = 0 to 9
// do something with each one.
reels.foreach {
  doStuff
}

EDIT: Based on your comment above you may want to try a for comp.

 def doStuff(v: Int) = println(v)
 val reel = Seq(0 to 9, 0 to 9)
 for {
   outer <- reel
   inner <- outer
 }
  yield doStuff(inner)
James DW
  • 1,815
  • 16
  • 21
0

May be this approach will help you:

var reels: Array[Int] = new Array[Int](5)

def doStuff(): Unit = {
  print(reels(0) + " ")
}
(0 until 10).foreach(i => { reels(0) = i; doStuff })

// produces
// 0 1 2 3 4 5 6 7 8 9 

Also pay attention to @Barry's comment link. Especially to @missingfaktor's answer.

Community
  • 1
  • 1
user5102379
  • 1,492
  • 9
  • 9
0
reels.foreach { item =>
  //do something i.e. - println(item)
}
Maxim
  • 7,268
  • 1
  • 32
  • 44
0

Based on your comment above is this what you are looking for? No explicit variable going from reels1 at index 0 to reel2 at index 1?

val reels1: Array[Int] = new Array[Int](10)
val reels2: Array[Int] = new Array[Int](10)

val reels = Seq(reels1,reels2)
 reels.foreach{ reel =>
  for(item <- reel){
   doStuff
   }
  }
Barry
  • 1,800
  • 2
  • 25
  • 46
  • No, not quite. I don't want reels1 to be an array. I want reels to be an array. I want reels(1) to iterate through the values 1 to 10, and reels(2) to iterate through the values 1 to 10. I don't need to save all the values 1 to 10 at the same time on reels. I want the array, "reels", to hold two numbers at a time, and I want to set those two numbers by two nested for loops. – Victor Dec 25 '14 at 14:29
  • After typing your code into the scala interpreter, this is close to what I want, but not quite there. But thank you. It is helpful. – Victor Dec 25 '14 at 14:38
  • OK great - I can modify further but not sure I am clear exactly what you want. Do you want Array of Arrays? So reel is Array that contains two items where each item itself is an array? Then you want to iterate through every item in the top level array and all of its sub-arrays? – Barry Dec 25 '14 at 14:59
  • Okay, I think this will illustrate what I want to do better. Imagine I have a 2 dimensional matrix, and I want to traverse every element on the matrix. Say the matrix is a 10x10 to keep consistent with the example. I want to define an array (A point) that that traverses every element of the matrix. So, at any one time, the point has 2 coordinates. A row, and a column. I want two for loops to iterate the coordinates on the point so that every element in the matrix is visited. Thank you for your help. – Victor Dec 26 '14 at 08:19
0

I found an answer, but it seems somewhat cumbersome to me. Perhaps because I am not used to doing things this way. What I wanted was something like this:

{Array.tabulate(10, 10...){(reel1, reel2, ... reel5) => /*This is what I wanted to avoid doing. define a variable for each dimension I will be using*/ do_stuff(reel1, reel2,... reel5)}}

Victor
  • 35
  • 4