Scala中的函数函子

Scala中的函数函子,scala,functor,Scala,Functor,在学习Scala中的函子的过程中,我遇到了函数函子,我有两个问题: Function1的函子类型参数中的此签名是什么 甚至不用编写Function1Functor,我就可以从sbt控制台执行以下操作: (x:Int)=>x*2映射(*2) 这怎么可能呢 看 感觉很奇怪: scala> (x: Int) => x * 2 map (_ * 2) <console>:11: error: value map is not a member of Int (x: Int)

在学习Scala中的函子的过程中,我遇到了函数函子,我有两个问题:

  • Function1的函子类型参数中的此签名是什么
  • 甚至不用编写Function1Functor,我就可以从sbt控制台执行以下操作:
  • (x:Int)=>x*2映射(*2)

    这怎么可能呢

  • 感觉很奇怪:

    scala> (x: Int) => x * 2 map (_ * 2)
    <console>:11: error: value map is not a member of Int
      (x: Int) => x * 2 map (_ * 2)
                     ^
    
    scala> { (x: Int) => x * 2 } map (_ * 2)
    <console>:11: error: value map is not a member of Int => Int
     { (x: Int) => x * 2 } map (_ * 2)
    
    顺便说一下,控制台应该为您提供关于实际类型的提示

    scala> {(x: Int) => x * 2} map (_ * 2)
    res1: Int => Int = scalaz.std.FunctionInstances$$anon$3$$Lambda$1884/1646077266@29e39141
    
    如果要检查隐式解析过程,可以切换以下选项,例如in build.sbt:

    scalacOptions ++= Seq("-Xlog-implicits")
    
  • scala> {(x: Int) => x * 2} map (_ * 2)
    res1: Int => Int = scalaz.std.FunctionInstances$$anon$3$$Lambda$1884/1646077266@29e39141
    
    scalacOptions ++= Seq("-Xlog-implicits")