Scala: understanding the ::: operator

rookie

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.

Shane Delmore

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Understanding Future in Scala

来自分类Dev

Another Scala CanBuildFrom issue: a collection enrichment operator that wraps another of a different type

来自分类Dev

()与{}的Scala

来自分类Dev

Understanding <a href="#!"

来自分类Dev

= operator的重载

来自分类Dev

替代-> operator()

来自分类Dev

Understanding a simple regex

来自分类Dev

Incorrect understanding of buffer in RxJava

来自分类Dev

Understanding Regular Expression in Javascript

来自分类Dev

Understanding backtracking (maze algorithm)

来自分类Dev

Problems understanding sequential Erlang

来自分类Dev

Understanding Type of `flip ($)`

来自分类Dev

Trouble understanding reference definitions

来自分类Dev

Understanding for loop logic

来自分类Dev

understanding the behaviour of cfflush

来自分类Dev

Understanding python import of gevent

来自分类Dev

Understanding the basic for Linq queries

来自分类Dev

Understanding the Haskell as-pattern

来自分类Dev

Understanding Java Variable/Decimals

来自分类Dev

Understanding multiple threads and EventWaitHandle

来自分类Dev

C ++是operator!=定义了operator ==时自动提供

来自分类Dev

'std :: operator中的'operator <<'不匹配

来自分类Dev

Is it OK to define operator<< or operator>> for FILE&?

来自分类Dev

运算符重载,operator + vs operator + =

来自分类Dev

'std :: operator中的'operator <<'不匹配

来自分类Dev

Scala调试

来自分类Dev

Scala:“ getOrElse”

来自分类Dev

Scala for ( ) vs for { }

来自分类Dev

Scala转换