Skip to content

Instantly share code, notes, and snippets.

@shyan1
Last active April 12, 2025 08:19
Show Gist options
  • Select an option

  • Save shyan1/a6216a11f7906e8bcd944fa835c941c3 to your computer and use it in GitHub Desktop.

Select an option

Save shyan1/a6216a11f7906e8bcd944fa835c941c3 to your computer and use it in GitHub Desktop.
Scala 3 notes

Collections

  • when you have large lists, you may want to use a Vector or ArrayBuffer instead of a List.
    Vector is immutable just like List, but it works much faster with large lists when you need to access an element directly, like list(10_000_000). And ArrayBuffer is like a mutable version of Vector, so you’ll use it when you have a sequence that you’ll constantly be modifying.
val a = List(1,2,3)
val b = a :+ 4
val c = b ++ List(5,6)    // List(1,2,3,4,5,6)

val a = List(2,3)
val b = 1 :: a
val c = 0 :: b      // List(0,1,2,3)

val a = List(1,2,3,4,5)
val b = a.filter(_ > 2)   // List(3,4,5)

// def updated[B >: A](index: Int, elem: B): List[B]
val a = List(1,2,3)
val b = a.updated(0, 100)   // List(100, 2, 3)

  • Of the other sequence classes, the most important ones to know are:
    • Vector is an immutable, indexed sequence
    • ArrayBuffer is a mutable, indexed sequence
            Seq
             |
           /   \  
    LinearSeq   IndexedSeq
       |            | 
      List         Vector
  • Use List or Vector when you want an immutable sequence

  • Prefer Vector over List when (a) you need to randomly access elements in the sequence, (b) the size gets large, or (c) when you’ll be constantly appending elements to the sequence

  • Use ArrayBuffer when you want a mutable sequence class (for instance, when you know that you will constantly add, remove, and up- date elements)

  • Vector and ArrayBuffer are typically your “go to” classes

  • List and Vector are used in an FP style, and in OOP when you know you’re sequence won’t be mutated

  • ArrayBuffer is used in an OOP style

  • All of that being said, the List class is a nice class to use when you’re working with small sequences.

for loops

for/do

val ints = List(1,2,3,4,5)

for 
    i <- ints 
do 
    val j = i * 10
    println(j)


val names = List("adam", "alex", "bob")
for name <- names do
    val capName = name.capitalize
    println(capName)

for Expression (aka for comprehension)

for/yield expression

val xs = List(1,2,3)
val ys = for x <- xs yield x*2

val names = List("luka", "leia")
val capNames = for name <- names yield name.capitalize

val fruits = List("apple", "banana", "cherry")
val capFruits = 
    for
        f <- fruits
        if f.length > 5
        if f.length < 10
        if f.startsWith("c")
    yield
        f.capitalize

map method

val strings = List("a", "bb", "ccc")
// this for loop and foreach method call are equivalent:
for s <- strings do println(s.length) 
strings.foreach(s => println(s.length))

val lengths = for s <- strings yield s.length
val lengths = strings.map(_.length)

filter method

val ints = Vector(1,2,3,4,5)

val smallInts = ints.filter(_ < 3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment