Functions and more about Functions

Work in progress
Previous slide : 2- Scala-basics

Before we get on with the collections lets have a word on

Mutable vs Immutable objects

Mutable instances is passed by reference.
Immutable instances is passed by value.

Array's

Immutable Array Declaration


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

scala> val b = Array("1","2","3")
b: Array[String] = Array(1, 2, 3)

scala> val c = Array("1",2,3.0)
c: Array[Any] = Array(1, 2, 3.0)

Immutable Fixed Length Arrays


// Create an array of specific Size
scala> val chars = Array.fill(256){0}
chars: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0 ....

// To create an array of size 256 with random numbers
scala> val rand = Array.fill(256){math.random}
rand: Array[Double] = Array(0.9874994240791204, 0.07789405624008006,....

// The section {} or {math.random} can be replaced with any function Block,
// Which would fill up or return the values required for the array.
scala>val elements = Array.fill(256){ function }

Mutable ArrayBuffers


scala> val buf = scala.collection.mutable.ArrayBuffer.empty[Int]
buf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> buf += 1
res2: buf.type = ArrayBuffer(1)

scala> buf += 10
res3: buf.type = ArrayBuffer(1, 10)

scala> buf.toArray
res4: Array[Int] = Array(1, 10)

Array Usage


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

scala> a(1)
res0: Int = 2

scala> a.head
res2: Int = 1

scala> a.last
res4: Int = 4

Array Usage continuation


// Appending to an Array
scala> a :+ 5
res7: Array[Int] = Array(1, 2, 3, 4, 5)

// Prepending to an Array
scala> 0 +: a
res9: Array[Int] = Array(0, 1, 2, 3, 4)

// Joining Two Arrays
scala> a ++ Array(10,11,12)
res10: Array[Int] = Array(1, 2, 3, 4, 10, 11, 12)

ArrayBuffer Usage

scala> val buf  =scala.collection.mutable.ArrayBuffer.empty[Int]

scala> buf +=1
res20: buf.type = ArrayBuffer(1)

scala> buf +=2
res21: buf.type = ArrayBuffer(1, 2)

scala> buf +=3
res21: buf.type = ArrayBuffer(1, 2, 3)

scala> buf append 6
scala> buf prepend 8

scala> buf
res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(8, 1, 2, 3, 6)

Traversing Arrays

scala> val a = 0 to 10 toArray
a: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> a.foreach(i:String=>println(i))

scala> a.foreach(println(_))

scala> a.foreach(println)

scala> a.foreach(i=> {
     | println(i)
     | }
     | )

Yield
Transforming Collections
The Map operation

scala> a.map(i=>i*10)
res2: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

scala> a.map(_*10)
res3: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

scala> val b = Array(1.0,2,"3",4.0,5,"6")
b: Array[Any] = Array(1.0, 2, 3, 4.0, 5, 6)

scala> b.map {
     |   case i:Int => i*10
     |   case i:Double => i*10.toInt
     |   case i:String => i.toInt *10
     |   case _ => 0
     | }
res5: Array[Double] = Array(10.0, 20.0, 30.0, 40.0, 50.0, 60.0)

The Filter operation

scala> a.filter(_%2!=0)
res6: Array[Int] = Array(1, 3, 5, 7, 9)

scala> a.filter(_%2==0)
res7: Array[Int] = Array(2, 4, 6, 8, 10)

scala> a.filter(i=> i%2==0)
res8: Array[Int] = Array(2, 4, 6, 8, 10)

scala> a.filter(i=> {
     | val reminder = i%2
     | reminder==0
     | }
     | )
res9: Array[Int] = Array(2, 4, 6, 8, 10)

Thank you


Next Slide : 4- Scala-functions