2014-01-07 34 views
0

我正在map3Functional Programming in Scala_必須追蹤[咖喱]方法

// Exercise 3: The apply method is useful for implementing map3, map4, and so on 
    // and the pattern is straightforward. Implement map3 and map4 using only 
    // unit, apply, and the curried available on functions. 
    def map3[A,B,C,D](fa: F[A], 
        fb: F[B], 
        fc: F[C])(f: (A, B, C) => D): F[D] = { 
    def foo: (A => B => C => D) = (f _).curried // compile-time error 
    def fooF: F[A => B => C => D] = unit(foo) 
    val x: F[B => C => D] = apply(fooF)(fa) 
    val y: F[C => D] = apply(x)(fb) 
    val z: F[D] = apply(y)(fc) 
    z 
    } 

一(1)編譯時以上,注意的行發生錯誤:

[error] C:\...\Applicative.scala: _ must follow method; cannot follow f.type 
[error]  def foo: (A => B => C => D) = (f _).curried 
[error] 

          ^

我能夠通過這篇文章成功獲得curried版本的功能 - Using FunctionX#curried

但是,我不明白上面的編譯時錯誤。

回答

6

錯誤是因爲f這裏是一個函數,而不是一個方法; f _語法是將方法轉換爲函數,在此不需要。簡單地寫def foo: A => B => C => D = f.curried

+0

謝謝,這工作(不能接受),@胡。如果'f'是一種方法,'f _'會做什麼? –

+0

它將它變成相應的功能,參見規範中的§6.7。它也會將thunks轉換爲空函數:'def foo [A](v:=> A):()=> A = v _' – Hugh

+0

謝謝。另外,你(或任何人)對我的'map3'實現有什麼意見?批評讚賞。 –