scala> //For loop with filter
scala> val subList = List(
| ("math", 80),
| ("history", 70),
| ("science", 65)
| )
subList: List[(String, Int)] = List((math,80), (history,70), (science,65))
scala> //Syntax : for(Generator Filters)
scala> for(
| info <- subList
| //First Filter
| if info._1 != "math"
| //Second Filter
| if info._2 != 65
| ){
| println("info -> " + info)
| }
info -> (history,70)
scala>
|
| //Nested for loop with Filter
scala> //val subList:List[(String, List[Int])] = List(
scala> val subList = List(
| ("math", List(50, 51, 52)),
| ("science", List(60, 61, 62)),
| ("history", List(80, 90, 100))
| )
subList: List[(String, List[Int])] = List((math,List(50, 51, 52)), (science,List(60, 61, 62)), (history,List(80, 90, 100)))
scala> for (
| info <- subList
| //Need semicolon to indicate each nested loop
| //If Braces is used instead of Paranthesis,
| //semicolon can be avoided
| if info._1 != "math";
| mark <- info._2
| if mark != 61
| ){
| println("subject -> " + info._1 + ", mark -> " + mark)
| }
subject -> science, mark -> 60
subject -> science, mark -> 62
subject -> history, mark -> 80
subject -> history, mark -> 90
subject -> history, mark -> 100
scala>
|
| //Nested for loop with mid stream variable binding
scala> //val subList:List[(String, List[Int])] = List(
scala> val subList = List(
| ("math", List(50, 51, 52)),
| ("science", List(60, 61, 62)),
| ("history", List(80, 90, 100))
| )
subList: List[(String, List[Int])] = List((math,List(50, 51, 52)), (science,List(60, 61, 62)), (history,List(80, 90, 100)))
scala> for {
| info <- subList
| //Midstream variable binding
| //Much easier to refer 'subject' in println
| //statement rather than using info._1
| subject = info._1
| //Note : Semicolon is no more needed
| // as we are using Braces for the
| // 'for' expression
| if subject != "math"
| mark <- info._2
| if mark != 61
| }{
| println("subject -> " + subject + ", mark -> " + mark)
| }
subject -> science, mark -> 60
subject -> science, mark -> 62
subject -> history, mark -> 80
subject -> history, mark -> 90
subject -> history, mark -> 100