Comment

Comment by hhandoko

> 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.
parent hhandoko 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton