Showing posts with label b9835| Implicit Arguments. Show all posts
Showing posts with label b9835| Implicit Arguments. Show all posts

Passing Implicit argument : Example

Syntax : apply() method in Future Class
Here last argument list is an implicit argument list

def
apply[T](body: ⇒ T)(implicit executor: ExecutionContext): Future[T]
Starts an asynchronous computation and returns a Future object with the result of that computation.

The result becomes available once the asynchronous computation is completed.
T        the type of the result
body     the asynchronous computation
executor the execution context on which the future is run
returns  the Future holding the result of the computation

Example : Executing 3 Threads using Future

// PS:39, psn: a15 > a50
scala> import scala.concurrent.Future
import scala.concurrent.Future
// Import the implicit argument : ExecutionContext
scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global

scala> def threadMethod(threadId : Int) = {
     |   println("Inside thread : " + threadId)
     |   threadId
     | }
threadMethod: (threadId: Int)Int

scala> (1 to 3) foreach { threadNo => 
     |   // Create Future object. Here apply() Factory method is called
     |   // 1st argument : Asychronous computation
     |   // 2nd argument : Implicit Argument
     |   //              : will be used from the import statement of 'ExecutionContext.implicits'
     |   val future = Future {
     |     threadMethod(threadNo)  
     |   }
     |   
     |   future onSuccess {
     |     case threadId:Int => println(s"Success : Thread returned : $threadId")     
     |   }
     |   future onFailure {
     |     case throwable:Throwable => println(s"Failure : Thread returned : $throwable")  
     |   }
     | }
Inside thread : 1
Success : Thread returned : 1

scala> Inside thread : 3
Inside thread : 2
Success : Thread returned : 3
Success : Thread returned : 2

Multiple Argument List : Example

A method will multiple argument list are also Variadic methods
scala> // #PS:37

scala> // When a function is defined with Multiple argument list(getMark()

scala> // in this case), 

scala> // (1) : last argument can be passed using curly bracket,

scala> // which gives a nice looking block syntax instead of using Round 

scala> // bracket

scala> //(2) : Implicit arguments can be passed for last argument list

scala> 

scala> // getMark() is defined to take 3 arguments

scala> // Arg 1 : String

scala> // Arg 2 : Int

scala> // Arg 3 : Function

scala> def getMark(subject: String, mark:Int)(printFn: (String, Int) => String) = {
     |   val finalMark = if (subject == "math") {
     |                     mark + 10
     |                   }else {
     |                     mark  
     |                   }
     |   printFn(subject, finalMark)                  
     | }
getMark: (subject: String, mark: Int)(printFn: (String, Int) => String)String

scala> 

scala> // Calling getMark() : Example 1 ( Last argument passed using curly bracket)

scala> getMark("math", 80){
     |                       (subject:String, mark:Int) => {
     |                         s"$subject mark is -> $mark"
     |                       }
     |                     }
res33: String = math mark is -> 90

scala> 

scala> // Calling getMark() : Example 2

scala> getMark("science", 70){ 
     |                         (subject:String, mark:Int) => {
     |                           if(mark <= 80) {
     |                             s"$subject mark is low -> $mark"  
     |                           }else {
     |                             s"$subject mark is good -> $mark"
     |                           }
     |                         }
     |                       }
res34: String = science mark is low -> 70