Is it true that applying the list1 ::: list2
operator to two lists corresponds to appending all of the contents of list1
to list2
?
scala> val a = List(1,2,3)
a: List[Int] = List(1, 2, 3)
scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6)
scala> a:::b
res0: List[Int] = List(1, 2, 3, 4, 5, 6)
Is there any other use for this operator? I could not find anything on :::
in the Scala documentation and am wondering the extent of its formal name and use.
Yes, it is just the list concatenation operator. They are nil terminated linked lists so conceptually all it is really doing is taking the last cons cell of the first list and pointing it to the head of the second list instead of Nil.
You can also use the more generic ++ operator instead of ::: if you prefer. The end result is the same but technically you are making calls on different objects. Operators ending in a : are right associative in Scala so using a ++ b is the same as a.++(b) or basically a.append(b) as opposed to a ::: b being right associative is translated to b.:::(a) which can be read as b.prepend(a).
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句