Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last element in Scala List?

Tags:

list

scala

In Scala, List is implemented as linked list. And it has 2 parts, head and tail. Last cell in List contains Nil.

Nil in Scala is itself a singleton and extends List[Nothing], as documented here

As Nil is a singleton, does it mean end element of all List instances in Scala has same object.

like image 431
Mandroid Avatar asked Dec 19 '22 00:12

Mandroid


2 Answers

Yes and No.

While in fact the end marker is always the singleton Nil, the same object, the last element is the one just before.

scala> val a = 1 :: 2 :: Nil
a: List[Int] = List(1, 2)

scala> a.last
res10: Int = 2

You might argue about the terminology, but coders are often positivists in that regard, and truth is, what the code says.

like image 174
user unknown Avatar answered Dec 27 '22 05:12

user unknown


Yes, all lists end with the empty list known as Nil.

You can see that this is true by trying to create a list without Nil at the end.

val a = 1 :: 2 // fails

val a = 1 :: Nil // succeeds

val a = scala.collection.immutable.::(1, Nil) // succeeds

The last case calls the case class constructor for :: which extends List, hence, creating a List. The code for the case class :: is...

final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
  override def tail : List[B] = tl
  override def isEmpty: Boolean = false
}

And the operation :: is defined within the List object, which calls the case class :: to create a list for you when doing something like 1 :: Nil.

As seen here:

  def ::[B >: A] (x: B): List[B] =
    new scala.collection.immutable.::(x, this)

You can also see how the code for the map operation on List iterates through the List until reaching Nil.

Snippet taken from map function in List class

while (rest ne Nil) {
          val nx = new ::(f(rest.head), Nil)
          t.tl = nx
          t = nx
          rest = rest.tail
        }

(ne stands for not equal)

like image 33
Jordan Cutler Avatar answered Dec 27 '22 05:12

Jordan Cutler