|
|
|
|
|
by hhandoko
3314 days ago
|
|
> Scala core library is littered with :: +: and other nonsensical operators. They are very useful. With the two you mentioned (:: and +:) they can be used for pattern matching in addition to concatenation (of a list / sequence), for example: scala> val list = 1 :: 2 :: Nil
list: List[Int] = List(1, 2)
scala> val x = list match {
| case head :: _ => head
| case _ => 0
| }
x: Int = 1
scala> val seq = 1 +: Seq(2, 3) :+ 4
seq: Seq[Int] = List(1, 2, 3, 4)
scala> val y = seq match {
| case a +: _ :+ b => a + b
| case _ => 0
| }
y: Int = 5
It can be a bit confusing at first, especially for mutable vs immutable collection operators. But you end up remembering some, if not most of it, after using them a number of times. |
|
Scala on whole is optimized to have lesser no. of characters in code, in contrast to verbosity of Java. Case classes and lambdas are what good came out of it, I understood the concept and now I can use them. Operator overloading and Implicit are what move pendulum to other side, they are not some novel concept and make very readable code, unreadable.