在JavaScript中有不錯的spread operator。從MDN相當於JavaScript數組傳播的斯卡拉
實施例:
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
是否有在Scala中的等效?
在JavaScript中有不錯的spread operator。從MDN相當於JavaScript數組傳播的斯卡拉
實施例:
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
是否有在Scala中的等效?
如何
val lyrics = Seq("head") ++ parts ++ Seq("and", "toes")
總是有patch()
。它的論點有點神祕,因爲它具有更廣泛,更普遍的應用領域。
val parts = List("shoulders", "knees")
val lyrics = List("head", "and", "toes")
lyrics.patch(1, parts, 0) // res0: List(head, shoulders, knees, and, toes)
這可能會實現:
val parts = List("shoulders", "knees")
val lyrics = "head" :: parts.::("and").::("knees")
然而,這僅適用於List
型
這也許是興趣(更散佈在功能參數的情況下):https://開頭stackoverflow.com/questions/15170646/spread-parameters-in-scala?rq=1 – Thilo