4
我願做以下的斯卡拉:斯卡拉mkString除了最後一個
val l = List("An apple", "a pear", "a grapefruit", "some bread")
... some one-line simple function ...
"An apple, a pear, a grapefruit and some bread"
什麼將它寫這種方式最短的路?
我最好的嘗試,到目前爲止是:
def makeEnumeration(l: List[String]): String = {
var s = ""
var size = l.length
for(i <- 0 until size) {
if(i > 0) {
if(i != size - 1) { s += ", "
} else s += " and "
}
s += l(i)
}
s
}
但它是相當繁瑣的。任何想法?
我不知道init和last。即使使用它們在算法上沒有效率,在這種情況下它們也很有用。 –