Library contents

Scala Vector.lift

Read one name from an ordered roster after checking its length.

A simple example

Vector.lift

  1. Start with A, B, C
  2. Do this Read the second element
  3. Result B
Counting A as the first element puts B next in the sequence.

Syntax

Scala syntax
names.lift(index)
Run these examples in Scala

Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.

scalac Lesson.scala
scala Lesson

Read the second name on the pickup roster

A teacher calls children in their recorded pickup order. The office asks for the second name on the roster, so the teacher checks that the list contains at least two entries before reading it.

ResultMira
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val names = Vector("Asha", "Mira", "Sam")
        names.lift(1).foreach(println)
    }
}
Output
Mira

Mira occupies the second position on the three-person roster.

Advertisements

Prepare the next workshop roster

A teacher reads a workshop roster in arrival order. Who or what occupies the second position? Count from the front of the list.

GivenMira · Ivo · Bea
Worked resultIvo

Work from the delivery round

A courier has three labelled stops in route order. Who or what occupies the second position? Count from the front of the list.

GivenPark · Mill · Pier
Worked resultMill

What to watch for

lift returns None outside the available range. Handle that case when a name is required.

Documentation for Vector.lift