Showing posts with label c0155| apply() / Injector. Show all posts
Showing posts with label c0155| apply() / Injector. Show all posts

zipWithIndex() Example

ZipWithIndex() assigns and index to each element in array converting all the elements into a tuple of (element, index)

Example : Filter Header from Header+Data
scala> class Header(myHeader: Array[String])  {
     |   val temp = myHeader.zipWithIndex
     |   temp.foreach(println)
     |   val index = temp.toMap
     |   println(s"index -> $index")
     |   def apply(someHeader: Array[String], key:String):String = {
     |      someHeader(index(key)) 
     |   }
     | }
defined class Header

scala> val myString = "user,product,review"
myString: String = user,product,review

scala> val myList = myString.split(",")
myList: Array[String] = Array(user, product, review)

scala> val myHeader = new Header(myList)
(user,0)
(product,1)
(review,2)
index -> Map(user -> 0, product -> 1, review -> 2)
myHeader: Header = Header@2afb775a

scala> val someData = List("user,product,review", "user1,toy1,rating1", 
     |                               "user2,toy2,rating3")
someData: List[String] = List(user,product,review, user1,toy1,rating1, user2,toy2,rating3)

scala> val mapped = someData.map{ x =>
     |   val splitted = x.split(",")
     |   splitted
     | }
mapped: List[Array[String]] = List(Array(user, product, review), Array(user1, toy1, rating1), Array(user2, toy2, rating3))

scala> val dataOnly = mapped.filter { x =>
     |   myHeader(x, "user") != "user"
     | }
dataOnly: List[Array[String]] = List(Array(user1, toy1, rating1), Array(user2, toy2, rating3))
c116 > a12

Case Class & Companion Object

A Class coupled with its Companion Object can be used to implement Factory Pattern. Let us checkout a use case without Case Class
scala> :paste
// Entering paste mode (ctrl-D to finish)

// c116 -> a15
//== Understanding Companion object ================

// **Here 'object DbConnector' is a Companion object for
// 'class DbConnector'
class DbConnector {
  val url = "jdbc://..."
  val db = "mydb"
  val user = "myusername"
  val pwd = "mypwd"
  def connect()  = { println ("Connecting to DB...") }
}

object DbConnector {
  def apply() = new DbConnector()
}

// ** Note here we are not using new() to create
// an object for 'class DbConnector'. This is an
// example of Factor pattern
val conn = DbConnector()

// Exiting paste mode, now interpreting.

defined class DbConnector
defined module DbConnector
conn: DbConnector = DbConnector@208279af

scala> conn.connect()
Connecting to DB...
A Case class simplifies this process further. The Scala compiler provides Companion object without the need for us to explicitly providing the Companion object
scala> // c116 -> a20

scala> //== Case Class : Compiler creates a Companion object for 'case class'

scala> //== (without us explicity providing a Companion object)

scala> //==              there by implementing factor pattern

scala> case class DbConnectorV2 {
     |   val url = "jdbc://..."
     |   val db = "mydb"
     |   val user = "myusername"
     |   val pwd = "mypwd"
     |   def connect()  = { println ("Connecting to DB...") }
     | }
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
defined class DbConnectorV2

scala> // ** Note : Here also we are not using new(). So this is an example

scala> //           of factory pattern

scala> val conn = DbConnectorV2()
conn: DbConnectorV2 = DbConnectorV2()

scala> conn.connect()
Connecting to DB...
The compiler also generates toString(), hashCode(), equals() & apply() among many methods for the case class

The injector (or) default method : apply()

The apply method in a class can be called using the Round brackets...ie (). This helps to create a convention wherein a Method in an Object is called like a Function
scala> class SumOf {
     |    def apply(no1: Int, no2: Int) : Int = {
     |       no1 + no2
     |    }
     | }
defined class SumOf

scala> val sum = new SumOf()
sum: SumOf = SumOf@1b54903a

scala> // Calling the method apply() explicitly

scala> println(sum.apply(3, 2))
5

scala> // apply() method called without its name using

scala> // Round brackets...ie ()

scala> println(sum(3, 2))
5