Showing posts with label b4215| _. Show all posts
Showing posts with label b4215| _. Show all posts

Patter Matching Values, Variables & Types

When using match case, specific matching should appear before general mapping. In case clause, a word that start with a lowercase letter is considered as a new Variable. A word that starts with an Uppercase letter is considered to be a Type
//#PS:101
//psn > a40 > a20
scala> val mylist = Seq(3, 4, 5, 5.5, "mystring", "2ndstring", 
     |               1.7, 'something, true) 
mylist: Seq[Any] = List(3, 4, 5, 5.5, mystring, 2ndstring, 1.7, 'something, true)

scala> val mydata = 4           
mydata: Int = 4

scala> for ( data <- mylist){
     |   val mystr = data match {
     |     case 3           => s"Matches Integer '3'"
     |     //External variables are referred with backtick(``).
     |     //***Note : Not using `backtick`(like case mydata) is like a 
     |     //          catch all option
     |     case `mydata`    => s"Matches mydata '4'"
     |     // A word that start with an Upper case(Int here...) is considered
     |     // to be a Type
     |     case Int | _:Boolean      
     |                      => s"Matches any Integer/Boolean : $data"
     |     case "mystring"  => s"Matches String 'mystring'"
     |     case _:String    => s"Matches any string : $data"
     |     // As no type is given, Any is inferred
     |     case _           => s"Matches others : $data"  
     |   }
     |   println(mystr)
     | }
Matches Integer '3'
Matches mydata '4'
Matches others : 5
Matches others : 5.5
Matches String 'mystring'
Matches any string : 2ndstring
Matches others : 1.7
Matches others : 'something
Matches any Integer/Boolean : true

Partially Applied Function : Example

The arguments for a function can also be Applied at compile time ; hence the term Partially Applied Function
scala> // Function to multiply 3 arguments

scala> def multiply(x:Int, y:Int, z:Int) : Int = {
 | val result = x * y * z
 | result
 | }
multiply: (x: Int, y: Int, z: Int)Int

scala> // Pass all the arguments at run time

scala> val passallarguments = multiply _
passallarguments: (Int, Int, Int) => Int = <function3>

scala> passallarguments(1, 2, 3)
res15: Int = 6

scala> // Pass only 2 arguments at run time

scala> val passonly2arguments = multiply(_:Int, _:Int, 2)
passonly2arguments: (Int, Int) => Int = <function2>

scala> passonly2arguments(3, 4)
res16: Int = 24

scala> // This does not work

scala> val thisdonotwork = multiply
<console>:55: error: missing arguments for method multiply;
follow this method with `_' if you want to treat it as a partially applied function
 val thisdonotwork = multiply
 ^
c111 > a50 > a11

Data Conversion using : Map, for/yield etc...

scala> // Step 1

scala> val inputList = List(  "data=first data || key1=r1v1 || key2=",
     |                        "data=second data || key1=r2v1 || key2=r2v2",
     |                        "key1=r3v1"
     |                       )
inputList: List[String] = List(data=first data || key1=r1v1 || key2=, data=second data || key1=r2v1 || key2=r2v2, key1=r3v1)

scala> // Step 2

scala> val splitted = inputList.map{ x =>
     |   x.split("\\|\\|")
     |    .map(_.trim)
     | }
splitted: List[Array[String]] = List(Array(data=first data, key1=r1v1, key2=), Array(data=second data, key1=r2v1, key2=r2v2), Array(key1=r3v1))

scala> // Step 3

scala> val filteredList = splitted.map{ x =>
     |   val retval = for { element <- x
     |       val keyNval = element.split("=")
     |       if keyNval.size >= 2
     |     } yield {
     |       val splitted = element.split("=")
     |       // Create Tuple of Key and Value
     |       splitted(0) -> splitted(1)
     |     }
     |   retval
     | }
warning: there were 1 deprecation warning(s); re-run with -deprecation for details
filteredList: List[Array[(String, String)]] = List(Array((data,first data), (key1,r1v1)), Array((data,second data), (key1,r2v1), (key2,r2v2)), Array((key1,r3v1)))

scala> // Step 4

scala> val dnryList = filteredList.map{ x =>
     |   x.toMap
     | }
dnryList: List[scala.collection.immutable.Map[String,String]] = List(Map(data -> first data, key1 -> r1v1), Map(data -> second data, key1 -> r2v1, key2 -> r2v2), Map(key1 -> r3v1))

scala> // Step 5

scala> val filteredDnryList = dnryList.filter{ x =>
     |   if (x.getOrElse("data", ()) != ())
     |     true
     |   else
     |     false
     | }
filteredDnryList: List[scala.collection.immutable.Map[String,String]] = List(Map(data -> first data, key1 -> r1v1), Map(data -> second data, key1 -> r2v1, key2 -> r2v2))

scala> // Step 6

scala> val keyvaluepairs = filteredDnryList.map { x =>
     |   val data = x.getOrElse("data", "")
     |   (data, x)
     | }
keyvaluepairs: List[(String, scala.collection.immutable.Map[String,String])] = List((first data,Map(data -> first data, key1 -> r1v1)), (second data,Map(data -> second data, key1 -> r2v1, key2 -> r2v2)))

scala> // Step 7

scala> val words = keyvaluepairs.flatMap{ x =>
     |   //Underscore (_) is used as a Placeholder indicator
     |   val xsplitted = x._1.split(" ").map(_.trim)
     |   val wordNmeta = for { element <- xsplitted
     |     } yield {
     |       (element, x._2)
     |     }
     |   wordNmeta
     | }
words: List[(String, scala.collection.immutable.Map[String,String])] = List((first,Map(data -> first data, key1 -> r1v1)), (data,Map(data -> first data, key1 -> r1v1)), (second,Map(data -> second data, key1 -> r2v1, key2 -> r2v2)), (data,Map(data -> second data, key1 -> r2v1, key2 -> r2v2)))
c200 > a15