【问题标题】:Workflow of the Higher Order Function in ScalaScala中高阶函数的工作流程
【发布时间】:2017-11-07 11:32:49
【问题描述】:

我刚刚开始学习 Scala 中的高阶函数,我不明白这里的输出是如何出现的,根据文档 https://docs.scala-lang.org/tour/higher-order-functions.html,高阶函数是一个将函数作为参数或返回一个函数的函数功能。所以我的问题是,

  • 如果在我程序的multiplyBy2 函数体中我的b=a(b 分配给a),那么为什么f(a) 是50(检查第1 行)和之后a 应为 25(检查第 2 行)。
  • 如果我在第 3 行中不提及 f:Int=>Any 而不是仅提及 f:Int,为什么会引发错误

我对工作流程感到困惑,我已经尝试过很多次了。如果有人通过描述该程序的整个工作流程来帮助我,我将不胜感激。谢谢。

 package function    
    object HigherOrderFunction 
    {

   def main(args: Array[String]):Unit = 
   {  
       functionExample(25, multiplyBy2)                  
   }  



   def functionExample(a:Int, f:Int=>Any):Unit = //Line 3
   {  
        println(f(a))          //Line 1                         
        println(a)             //Line 2
   }  



   def multiplyBy2(b:Int):Int = 
   {
        println(b) // Assigned a = b, So, a is 25 Now
        b*2       
       // println(b) // Type Mismatch: Int, Required: Unit
   }  
}

输出

25
50
25

【问题讨论】:

    标签: scala function higher-order-functions


    【解决方案1】:

    要理解上面的代码流程,让我们一步一步地移动每个方法。

    就像你定义了函数,其中 functionExample 是一个高阶函数,因为它接受一个函数作为参数。

    scala> def functionExample(a:Int, f:Int=>Any):Unit = //Line 3
         |    {  
         |         println(f(a))          //Line 1                         
         |         println(a)             //Line 2
         |    }  
    functionExample: (a: Int, f: Int => Any)Unit
    
    scala> def multiplyBy2(b:Int):Int = 
         |    {
         |         println(b) // Assigned a = b, So, a is 25 Now
         |         b*2       
         |        // println(b) // Type Mismatch: Int, Required: Unit
         |    }  
    multiplyBy2: (b: Int)Int
    

    在此之后我们尝试调用:

    scala> functionExample(25, multiplyBy2)      
    25
    50
    25
    

    1) 第一次调用 functionExample 并设置 a=25 并将函数与 multiplyBy2 绑定,它接受 int 参数类型并返回 Any(如声明 f: Int => Any)

    2) 第一个 println 用于 f(a),它被转换为 multiplyBy2(25)。

    3) 在 multiplyBy2 函数中,我们首先打印接收到的参数,即 25。

    所以第一个输出值为 25。

    4) 接下来,multiplyBy2 函数返回 b*2,这使其成为 25*2 = 50,这是 f(a) 返回的值,并且由于示例代码中的 Line1 而打印出来。

    5) 最后,在 Line2 中,我们打印在 functionExample 中收到的参数。所以它打印 25。

    因此输出结果是:

    25
    50
    25
    

    【讨论】:

      【解决方案2】:

      在这里,您将获得一些可遵循的步骤。很容易摆脱你的困惑。

      注释行中的步骤

      package function
      
          object HigherOrderFunction 
      {
      
         def main(args: Array[String]):Unit = 
         {  
             functionExample(25, multiplyBy2) // Passing a function as parameter  
         }  
      
      
      
         def functionExample(a:Int, f:Int=>Int):Unit = 
         {  
              println(f(a))  // STEP-2: Here f=multiplyBy2, So multiplyBy2=25*2, which is 50          // Calling that function
              println(a)     // STEP-3: Here Simply first Argument is printed out 25
      
         }  
      
      
      
         def multiplyBy2(b:Int):Int = 
         {
      
             println(b)     // STEP-1: Here b=a, So the value of b is 25    
             b*2
         }  
      }
      

      【讨论】:

        【解决方案3】:

        结果正常, 首先你的 a=25,然后你在println(f(a)) 中应用 f(a),所以你打印 25,然后你返回 a*2 = 50。然后你 println(f(a)) 即 50,最后你打印 a = 25 从来没有改变是因为当你调用 multiplyBy2 时,它不会改变参数的值。

        对于第二个问题,它只是函数的签名,因为f:Int(f是参数值)但是f:Int=>Any(f是参数函数)。

        你可以把你的函数写成 def functionExample(a:Int)(f:Int=>Int):Unit = //Line 3 {enter code here`enter code here println(f(a)) //Line 1 println(a) `enter code here`//Line 2 }

        【讨论】:

          【解决方案4】:

          你可以用它的实现替换一个函数来推理它:

          第 1 步:

          object HigherOrderFunction 
          {
             def main(args: Array[String]):Unit = 
             {  
                 println(multiplyBy2(25))   //Line 1                         
                 println(25)                //Line 2
             }  
             def multiplyBy2(b:Int):Int = 
             {
                  println(b) // Assigned a = b, So, a is 25 Now
                  b*2
             }  
          }
          

          第 2 步:

          object HigherOrderFunction 
          {
             def main(args: Array[String]):Unit = 
             {  
                 functionExample(25, multiplyBy2)
                 println({println(25); 25*2}) 
                 // Evaluates the "value" passed in println, which causes 25 to be printed, 
                 // then prints the result of the value, 50.
                 println(25)
             }    
          }
          

          我希望现在你清楚为什么会看到 25 50 25 打印出来了 :)

          【讨论】:

            猜你喜欢
            • 2021-02-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多