jacques y jacques y - 12 days ago 6
Scala Question

Scala - Constructs map from two lists at initialization

I'm new in Scala programming.

I would like to have this kind of immutable map :

Map[ (Int,Int), (List[BoolVar]) ]


From these two lists :

val courseName = List("Course1","Course2")
val serieName = List("Serie1","Serie2")


My goal :

Map[0][0] // List[BoolVar] for "Course1""Serie1"
Map[0][0](0) // a BoolVar from "Course1""Serie1" List
....


I tried this but the syntax is false :

val test = Map[ (Int,Int), (List[BoolVar]) ](
for (course <- List.range(0,courseName.length) )
for( serie <- List.range(0,serieName.length) )
yield (course,serie) ->
for (indice <- List.range(0, 48))
yield BoolVar( courseName(course) + " - " + serieName(serie) )
);


Thanks for your help

Answer

Is that what you are looking for ?? Just a few minor changes. But it will use round brackets

val courseName = List("Course1","Course2")
val serieName = List("Serie1","Serie2")

 val m = {
   for {
course <- List.range(0,courseName.length) 
    serie <- List.range(0,serieName.length) 
   } yield (course, serie) -> {
    for (indice <- List.range(0, 48))
            yield ( courseName(course) + " - " + serieName(serie) ) 
    }
}.toMap

println( m )