Lists

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

Collections

  • Mutable collections: can be updated or extended in place
    • Array: mutable sequence of objects with the same type
  • Immutable collections: never change
    • List: immutable sequence of objects with the same type
Introduction to Scala

Lists have a type parameter

Array

scala> val players = Array("Alex", "Chen", "Marta")
players: Array[String] = Array(Alex, Chen, Marta)

List

scala> val players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
Introduction to Scala

How Lists are useful while immutable

  • List has methods, like all of Scala collections

    • Method: a function that belongs to an object
  • There are many List methods

    • myList.drop()
    • myList.mkString(", ")
    • myList.length
    • myList.reverse
1 http://bit.ly/scala_list_documentation
Introduction to Scala

How Lists are useful while immutable

scala> val players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
scala> val newPlayers = "Sindhu" :: players
newPlayers: List[String] = List(Sindhu, Alex, Chen, Marta)
Introduction to Scala

How Lists are useful while immutable

scala> var players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
scala> players  = "Sindhu" :: players
players: List[String] = List(Sindhu, Alex, Chen, Marta)
Introduction to Scala

cons (::)

  • Prepends a new element to the beginning of an existing List and returns the resulting List
scala> val players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
scala> val newPlayers = "Sindhu" :: players
newPlayers: List[String] = List(Sindhu, Alex, Chen, Marta)
  • An append operation exists, but its rarely used
1 http://bit.ly/append_list_inefficient
Introduction to Scala

Nil

  • Nil is an empty list
scala> Nil
res0: scala.collection.immutable.Nil.type = List()
Introduction to Scala

Nil

  • A common way to initialize new lists combines Nil and ::
scala> val players = "Alex" :: "Chen" :: "Marta" :: Nil
players: List[String] = List(Alex, Chen, Marta)
scala> val playersError = "Alex" :: "Chen" :: "Marta"
<console>:11: error: value :: is not a member of String
       val playersError = "Alex" :: "Chen" :: "Marta"
Introduction to Scala

Concatenating Lists

  • ::: for concatenation
val playersA = List("Sindhu", "Alex")
val playersB = List("Chen", "Marta")

val allPlayers = playersA ::: playersB
println(playersA + " and " + playersB + " were not mutated,") println("which means " + allPlayers + " is a new List.")
List(Sindhu, Alex) and List(Chen, Marta) were not mutated,

which means List(Sindhu, Alex, Chen, Marta) is a new List.
Introduction to Scala

Scala nudges us towards immutability

Polar bear nudging a panda bear

The word "immutability"

Introduction to Scala

Pros and cons of immutability

Pros

  • Your data won't be changed inadvertently
  • Your code is easier to reason about
  • You have to write fewer tests

Cons

  • More memory required due to data copying
Introduction to Scala

Let's practice!

Introduction to Scala

Preparing Video For Download...