官术网_书友最值得收藏!

Function Calls

Now, we'll look at how function calls are implemented in Scala.

Syntax Goodies

Scala provides flexible syntax and it is worth dedicating a few minutes to this concept.

Named Parameters

The following is a function, f(a:Int, b:Int). We can call this function using the named parameter syntax: f(a = 5, b=10). If we swap the parameters but leave the correct names, the method will still be correct.

It is possible to combine positional and named function calls—the first few arguments can be positional.

For example:

def f(x:Int, y:Int) = x*2 + y
f(x=1,y=2) // 4
f(y=1,x=2) // 5

Default Parameters

When specifying a function, we can set default parameters. Then, later, when we call this function, we can omit parameters and the compiler will substitute defaults:

def f(x:Int, y:Int=2) = x*2 + y
f(1) // 4

It is possible to create a comfortable API with the help of the combination of named and default arguments. For example, for case classes with N components, the compiler generates a copy method with N arguments; all of them have defaults:

case class Person(firstName: String, lastName: String)
val p1 = Person("Jon","Bull")
val p2 = p1.copy(firstName = "Iyan")

Now, let's transform code in the Or and Otherwise combinators to use the copy method instead of the Processed constructor.

  1. Change the case expression to type, checking (processed:Processed) or adding the bind variable to the case class pattern (processed@Processed(… ))
  2. In the case body, use the copy method instead of the Processed constructor:
    • The resulting code should be as per the following cases:
    • If the student uses type check in the case expression:
         case processed:Processed =>processed.copy(nextMode = Or(processed.nextMode,snd))
    • If the student uses bind variable:
        case processed@Processed(answer,nextMode,endOfDialog) =>
           processed.copy(nextMode = Or(nextMode,snd))
  3. Do the same transformation for the second match statement.

The full code looks like this:

case class Or(frs: ChatbotMode, snd: ChatbotMode) extends ChatbotMode{
override def process(message: String, 
           effects: EffectsProvider): LineStepResult = {
   frs.process(message, effects) match {
   case processed@Processed(answer,nextMode,endOfDialog) =>
   processed.copy(nextMode = Or(nextMode,snd))
   case Failed => snd.process(message,effects) match {
   case processed@Processed(answer,nextMode,endOfDialog) =>
   processed.copy(nextMode=Or(nextMode,frs))
   case Failed => Failed
   }
   }
   }
   }
}

Currying Forms (Multiple Argument Lists)

Currying is a term used for describing the transformation of a function with multiple arguments into a function with one argument. We will describe this process in detail in the next chapter.

It is vital for syntax that we can use multiple argument lists:

def f1(x:Int,y:Int) = x + y 
def f2(x:Int)(y:Int) = x + y

Here, f2 is in its curried form. It has the same semantics as f1, but can be called with a different syntax. This is useful when you need visually separate arguments.

Special Magic Methods

The following table shows the various magic methods:

主站蜘蛛池模板: 商都县| 邵阳市| 乡宁县| 昭通市| 贞丰县| 平阴县| 土默特右旗| 南汇区| 中山市| 凌云县| 乐平市| 山阳县| 民丰县| 彩票| 会东县| 昭苏县| 南通市| 泽普县| 酒泉市| 新丰县| 聂拉木县| 锦屏县| 溧阳市| 江达县| 右玉县| 镇雄县| 西城区| 库伦旗| 葫芦岛市| 乌鲁木齐县| 阿鲁科尔沁旗| 英吉沙县| 正镶白旗| 安庆市| 即墨市| 扎鲁特旗| 囊谦县| 广东省| 九台市| 十堰市| 五大连池市|