Library contents

Scala Vector.appended

Appending a stop to an immutable Vector produces a new sequence for the draft route.

A simple example

Vector.appended

  1. Start with An immutable list containing A
  2. Do this Add B to a new copy
  3. Result New list A, B
Creating a new collection keeps the original list intact.

Syntax

Scala syntax
published.appended(stop)
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

Add a stop to a published bus route

A planner starts with a published route that riders can already see. She needs to add the museum to a draft while keeping the original schedule available to other passengers.

Result[Station, Museum]
Lesson.scala
object Lesson {
    def main(args: Array[String]): Unit = {
        val published = Vector("Station")
        val draft = published.appended("Museum")
        println(draft.mkString("[", ", ", "]"))
    }
}
Output
[Station, Museum]

The draft has two stops while the published route still has just the station.

Advertisements

Extend a draft shortlist

The school office is editing a mutable rehearsal list. Attempt to append one name to the roster. A published read-only list requires a working copy.

GivenEditable list [Ada]
Worked resultName added

Protect a published result

A teacher opens an unmodifiable snapshot of the final roster. Attempt to append one name to the roster. A published read-only list requires a working copy.

GivenUnmodifiable list [Ada]
Worked resultMake a working copy

What to watch for

Keep the returned sequence. The published Vector retains its original elements.

Documentation for Vector.appended