Showing posts with label aabam| _ Underscore. Show all posts
Showing posts with label aabam| _ Underscore. Show all posts

Monday, September 19, 2016

Partially Applied Function : Example...

scala> //# 188

scala> def total(math: Int, history:Int, science: Int) = {
     |     math + history + science
     | }
total: (math: Int, history: Int, science: Int)Int

scala>

scala> //Example 1 : Two Argument applied

scala> val twoArgApplied = total(50, 70, _:Int)
twoArgApplied: Int => Int = <function1>

scala> twoArgApplied(60)
res6: Int = 180

scala>

scala> //Example 2 : One Argument applied

scala> //A partiall applied function assigned to function literal

scala> val oneArgApplied = total(50, _: Int, _:Int)
oneArgApplied: (Int, Int) => Int = <function2>

scala> oneArgApplied(30, 40)
res7: Int = 120

scala>

scala>

scala> //Example 3 : No Argument applied

scala> val noArgApplied = total _
noArgApplied: (Int, Int, Int) => Int = <function3>

scala> noArgApplied(40, 50, 60)
res8: Int = 150

scala>

scala> //Example 4 : Another example where no argument is applied

scala> //Note this 'println _' is different from the placeholder

scala> //syntax 'println(_)

scala> val input = List(10, 20, 30)
input: List[Int] = List(10, 20, 30)

scala> input.foreach(println _)
10
20
30

scala> //A shortcut for the previous example

scala> input.foreach(println)
10
20
30

Sunday, September 18, 2016

Function Literal, Function Value, Placeholder Syntax : Example...

scala> // Function Literal Example #183

scala> val input = List(1, 2, 3, 4)
input: List[Int] = List(1, 2, 3, 4)

scala>

scala> //Way 1 : Assigning the function literal to a function value

scala> //f1 : Function value      : Functional values are instances

scala> //(x:iInt, y:Int) => x + y : Function Literal(Is simply a

scala> //                           definition...Similar to class)

scala> val f1 = (x:Int, y:Int ) => x + y
f1: (Int, Int) => Int = <function2>

scala> input.reduce(f1)
res108: Int = 10

scala>

scala> //Way 2 : Passing function Literal directly

scala> input.reduce((x:Int, y:Int) => x + y)
res109: Int = 10

scala>

scala> //Way 3 : Skipping the type information

scala> //This is acheived through 'Target Typing' #186

scala> input.reduce((x, y) => x + y)
res110: Int = 10

scala>

scala> //Way 4 : Using Placeholder syntax #186

scala> //Can be used when we use the arguments only once

scala> //in the function literal

scala> //Note here... first '_' refers the First Parameter

scala> //             and the second '_' refers the Second

scala> //             Parameter

scala> input.reduce(_ + _)
res111: Int = 10

scala>

scala> //Way 4 : Another example using Placeholder Syntax

scala> input.foreach(println(_))
1
2
3
4

scala>

scala>

scala> //Way 5 : Omitting Placeholder

scala> //This only works for Single arument Function Literal

scala> input.foreach(println)
1
2
3
4

catch Expression : Example

#170
scala> def getMark(subject: String):Int = {
     |    subject match {
     |       case "math" => 80
     |       case "science" => 75
     |       case _ => 70
     |    }
     | }
getMark: (subject: String)Int

scala>

scala> getMark("math")
res74: Int = 80

scala> getMark("history")
res75: Int = 70

Monday, September 12, 2016

Tuple Example : Tuple2, Tuple3, _ etc..

scala> /*
     |    Creating Tuple #86
     |    Note : A Tuple can contain elements of different type
     |  */
     | val tuple2:Tuple2[String, Int] = new Tuple2("maths", 75)
tuple2: (String, Int) = (maths,75)

scala> val tuple2 = ("maths", 75)
tuple2: (String, Int) = (maths,75)

scala>

scala> val tuple3:Tuple3[String, Int, String] = new Tuple3("english", 75, "pass")
tuple3: (String, Int, String) = (english,75,pass)

scala> val tuple3 = ("english", 75, "pass")
tuple3: (String, Int, String) = (english,75,pass)

scala>

scala> /*
     |    Accessing Tuple
     |    Note here _1 & _3 are fields. Unlike List we do not have apply()
     |    method to access the fields...the reason being each element of
     |    the tuple might be different type..whereas apply() method always
     |    returns an element of the same type
     | */
     | tuple2._1
res61: String = maths

scala> tuple3._3
res62: String = pass