Showing posts with label aaeab| Array. Show all posts
Showing posts with label aaeab| Array. Show all posts

Friday, December 2, 2016

Which Sequence to use?

Sequences
  1. List #373
    1. Fast addition & removal at the beginning of List #373
    2. Use ListBuffer if appending to end of the list is needed
  2. Array
  3. ListBuffer
    1. Use this when an element is needed to be appended (Is Constant Time) to end of list
  4. ArrayBuffer

Tuesday, October 4, 2016

Typed Pattern, Type Erasure : Example...

scala> ////////////////////////////////////////////////////////////////

scala> //  Typed Pattern : Pattern Matching a Type

scala> ////////////////////////////////////////////////////////////////

scala> def patternFn(x: Any) = x match {
     |     //
     |     case b: List[_] => println("List...")
     |
     |     // This do not work because of type erasure #318
     |     case c: Map[Int, Int] => println("This is wrong...")
     |
     |     case d: Map[_, _] => println("Map: This does work")
     |
     |     // We dot have issue of Type Erasure with Array
     |     case e: Array[Int] => println("Array of Int...")
     |
     |     case f: Array[_] => println("Array of other types...")
     | }
<console>:28: warning: non-variable type argument Int in type pattern scala.collection.immutable.Map[Int,Int] (the underlying of Map[Int,Int]) is unchecked since it is eliminated by erasure
           case c: Map[Int, Int] => println("This is wrong...")
                   ^
<console>:30: warning: unreachable code
           case d: Map[_, _] => println("Map: This does work")
                                       ^
patternFn: (x: Any)Unit

scala>

scala> // Not expected result because of type erasure

scala> patternFn(Map[Int, String](1 -> "a", 2 -> "b"))
This is wrong...

scala> // Expected result : For Array we do not have the issue of

scala> //      type erasure

scala> patternFn(Array[Int](1, 2, 3))
Array of Int...

scala> patternFn(Array[Double](1.2, 2,2))
Array of other types...

Friday, September 23, 2016

Array : Concatenate...

scala> // Concatenate an array : Example #236

scala> val a1 = Array(1, 2, 3)
a1: Array[Int] = Array(1, 2, 3)

scala> val a2 = Array(5, 6, 7, 8, 9)
a2: Array[Int] = Array(5, 6, 7, 8, 9)

scala>

scala> val result = a1 ++ a2
result: Array[Int] = Array(1, 2, 3, 5, 6, 7, 8, 9)

scala>

scala> // zip operator : Example #236

scala> // zip can be used to create an array of Pairs

scala> //   Note : Unmatched elements are left out

scala> val zipped = a1.zip(a2)
zipped: Array[(Int, Int)] = Array((1,5), (2,6), (3,7))

scala>

scala> for {
     |   (e1, e2) <- zipped
     | }{
     |   println("e1 -> " + e1 + ", e2 -> " + e2)
     | }
e1 -> 1, e2 -> 5
e1 -> 2, e2 -> 6
e1 -> 3, e2 -> 7

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