功能函子

问题描述:

在我的方式了解函子在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 
} 
  1. 甚至没有写入Function1Functor,我能够做到从SBT控制台以下:
  2. (X :Int)=> x * 2 map(_ * 2)

    这应该怎么办?

开始=>

  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")