scala> val subjectList = List(
| ("math", 70),
| ("science", 60),
| ("history", 80)
| )
subjectList: List[(String, Int)] = List((math,70), (science,60), (history,80))
scala> //Syntax #165
scala> //for {clauses} yeild {body}
scala> val filteredList =
| for {
| info <- subjectList
| if info._1 != "math"
| }yield{
| info._1
| }
filteredList: List[String] = List(science, history)
Showing posts with label aakad| for(). Show all posts
Showing posts with label aakad| for(). Show all posts
Saturday, September 17, 2016
for Expression : Returning value using 'yield'
for Expression : Filter, Nested For Loop, Midstream variable binding...
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
for Expression : Iterating through Collection, Generator, Generator Expression...
Note : Although for is an Expression, in this case, we are simply using it as a loop and not returning any value
scala> val subjects = List("science", "math", "Physics")
subjects: List[String] = List(science, math, Physics)
scala> // Generator : 'subject <- subjects' is the generator as
scala> // it generates some value #160
scala> //Generator Expression :'subjects' is the Generator
scala> // Expression
scala> for (subject <- subjects)
| println("subject -> ", subject)
(subject -> ,science)
(subject -> ,math)
(subject -> ,Physics)
scala>
| //'Ranges' Example
scala> //Here '1 to 5' will result in 'Ranges' object
scala> for (no <- 1 to 5){
| println("no -> ", no)
| }
(no -> ,1)
(no -> ,2)
(no -> ,3)
(no -> ,4)
(no -> ,5)
Monday, September 12, 2016
Array Example : Value & Type Paramaterization
scala> // Parameterize Class instance with Value & Type
scala>
scala> /*******************************************
| Creating & Initializing an Array #77
| *******************************************/
| //Option 1
| // Here 'String' is Parameterized Type
| // '2' is a Parameterized Value
| // Square Bracket ([]) is used to Parameterize a Type
| val subject:Array[String] = new Array[String](2)
subject: Array[String] = Array(null, null)
scala> subject.update(0, "english")
scala> subject.update(1, "math")
scala>
scala> //Option 2
scala> val subject = new Array[String](3)
subject: Array[String] = Array(null, null, null)
scala> // During assignment Parenthesis calls the 'update()' method
scala> // passing both the Array index & value assigned
scala> subject(0) = "english"
scala> subject(1) = "math"
scala>
scala> // Option 3
scala> // Here Parenthesis in 'Array' calls the apply() factory
scala> // method of the Companion Object (Here Array is a
scala> // Companion Object rather than the Class)
scala> val subject = Array("english", "math")
subject: Array[String] = Array(english, math)
scala> /*******************************************
| Accessing an Array #77
| *******************************************/
|
| //Option 1
| for(i <- (0).to(1)) {
| // Note : Parenthesis is used to access an Array element
| // unlike Square brackets that is used in Java
| // #79
| print(subject.apply(i))
| }
englishmath
scala>
scala> //Option 2
scala> // If a method ('to' in our case) takes only 1 parameter
scala> // '.' and Parenthesis cna be omitted (We should also have a
scala> // receiver for the method call...in our case 'i')
scala> for(i <- 0 to 1) {
| //Note here..the apply() method is called implicitly
| print(subject(i))
| }
englishmath
Subscribe to:
Posts (Atom)