News for hackers. Stuff that matters.
Re:Copying data is wasteful, mutating data is dangerous by wbillingsley on Thursday January 09, 2020 @09:08PM (#22007852)
Scala's collections API also uses some mutable internal data structures locally (and temporarily) within some functions that present a pure API.

List[T] is one of the classic immutable data structures, and a List[T] cannot have its contents modified. But within the prependAll method, it can create a mutable List factory (ListBuffer) to do its work building the new list that it will return.

So, if you follow this (contrived) example in your IDE, calling this:

  val f = List(4, 5, 6).prependedAll[Int](Array(1, 2, 3))
will take you inside StrictOptimizedSeqOps, which does this:

  override def prependedAll[B >: A](prefix: IterableOnce[B]): CC[B] = {
    val b = iterableFactory.newBuilder[B]
    b ++= prefix
    b ++= this
    b.result()
  }
The mutable data structure doesn't escape the function, so to the caller it remains referentially transparent.

There are no comments on this comment yet.