Is there a way to initialize multiple variables from array or List in Scala?
scala
Solution
How about this:
scala> val numbers = List(1, 2, 3)
numbers: List[Int] = List(1, 2, 3)
scala> val List(hours, minutes, seconds) = numbers
hours: Int = 1
minutes: Int = 2
seconds: Int = 3
Problem
What I want to do is basically the following in Java code: ``` String[] tempStrs = generateStrings(); final int hour = Integer.parseInt(tempStrs[0]); final int minute = Integer.parseInt(tempStrs[1]); final int second = Integer.parseInt(tempStrs[2]); ``` However, `tempStrs` is just a temporary variable, which is not used anymore. Then, this can be expressed in the following code in F#: ``` let [| hour; minute; second |] = Array.map (fun x -> Int32.Parse(x)) (generateStrings()) ``` Is there a similar way to do this in Scala? I know this can be done in Scala by ``` val tempInts = generateStrings().map(_.toInt) val hour = tempInts(0) val minute = tempInts(1) val second = tempInts(2) ``` But is there a shorter way like F# (without temp variable)? Edit: I used ``` var Array(hour, minute, second) = generateStrings().map(_.toInt) ``` Of course, using `val` instead of `var` also worked.