2017-04-18 68 views
0

在我的方式瞭解函子在Scala中,我碰到的功能函子與我周圍有2個問題:功能函子

  1. 什麼是在功能1仿函數類型參數此簽名?
implicit def Function1Functor[R]: Functor[({type l[a]=(R) => a})#l] = new Functor[({type l[a]=(R) => a})#l] { 
    def fmap[A, B](r: R => A, f: A => B) = r andThen f 
} 
  • 甚至沒有寫入Function1Functor,我能夠做到從SBT控制檯以下:
  • (X :Int)=> x * 2 map(_ * 2)

    這應該怎麼辦?

    回答

    0
    1. Weird nested structural type in generics

    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) 
      

      如果您後者的作品,也許你的SBT控制檯自動導入Scalaz功能;檢查你的build.sbt的線如

      initialCommands in console := "import scalaz._, Scalaz._" 
      

      順便說一句,控制檯應爲您提供一個提示上實際類型

      scala> {(x: Int) => x * 2} map (_ * 2) 
      res1: Int => Int = scalaz.std.FunctionInstances$$anon$3$$Lambda$1884/[email protected] 
      

      如果你想檢查隱含的解決過程,您可以切換以下選項,例如,在build.sbt中:

      scalacOptions ++= Seq("-Xlog-implicits")