2016-09-03 46 views
0

我看到了這樣的代碼。 從REPL測試來看,它看起來像「/:」遍歷字符並重復調用incr。但是我找不到這個語法的任何文檔。 /:這裏的語法是什麼意思?迭代的Scala語法?或者是其他東西?

val chars = List('a','b') 
def incr(acc:Map[Char, Int], c:Char) = { 
    val count = (acc get c).getOrElse(0) + 1 
    acc + ((c, count)) 
} 

(Map[Char,Int]() /: chars)(incr) 
+3

檢查scaladoc的地圖;)http://www.scala-lang.org/api/current/index.html#[email protected] /:[B](z:B)(op:(B,A)=> B):B – jopasserat

+0

看起來像是foldLeft吧? – johnsam

+0

這只是一個像其他方法調用一樣的方法調用。它不是*特殊的語法。事實上,斯卡拉的事情很少。 –

回答

1

documentation

* Note: `/:` is alternate syntax for `foldLeft`; `z /: xs` is the same as 
    * `xs foldLeft z`