How to create a seq

How would you create a sequence of Landmarks from those two classes:

case class Landmark(id: String, point: Point[_3D])
case class Landmarks(values: Seq[Landmark[_3D]])

val idLM0 = seqLMs(0).attribute(“key”).get.toString() // get the first ID
val Pt0: Point[_3D] = Point(seqLMs(0).text.split(" “)(0).toDouble, seqLMs(0).text.split(” “)(1).toDouble, seqLMs(0).text.split(” ")(2).toDouble)
val LM0 = Landmark(idLM0, Pt0)

// Create a Seq of Landmark

def createLandmarks(LM0: Landmark[_3D], seqLMs: NodeSeq): Landmarks = {
// initial Seq
val LMsSeq: Seq[Landmark[_3D]] = Seq(LM0)

for (i <- 1 to seqLMs.length-1){
  val idLM01 = seqLMs(i).attribute("key").get.toString() // get the first ID
  val Pt: Point[_3D] = Point(seqLMs(i).text.split(" ")(0).toDouble, seqLMs(i).text.split(" ")(1).toDouble,
    seqLMs(i).text.split(" ")(2).toDouble)
  val LM = Landmark(idLM01, Pt) // use zip to create the LM

 LMsSeq =+  Seq(LM) // ERROR HERE



I tried to create an initial Point (called LM0 here) and then I tried to iterate but I got an error when trying to append.

Thank you

Assuming you meant += instead of =+, I see a couple of problems here:

LMsSeq += Seq(LM) // ERROR HERE
  1. You’re trying to change an immutable collection. If you want
    mutability (odds are you don’t, but you might), look into
> import scala.collection.mutable._
import scala.collection.mutable._

> val buf: Buffer[Int] = new ArrayBuffer
buf: scala.collection.mutable.Buffer[Int] = ArrayBuffer()

> buf
res0: scala.collection.mutable.Buffer[Int] = ArrayBuffer()

> buf += 1
res1: buf.type = ArrayBuffer(1)

> buf += 42
res2: buf.type = ArrayBuffer(1, 42)

> buf += 99
res3: buf.type = ArrayBuffer(1, 42, 99)
  1. You’ve said that LMsSeq is a Seq[Landmark[_3D]], but you’re
    trying to add a Seq[Landmark[_3D]] (presumably, I’m guessing about
    what LM is). If you have a Buffer, you can add single thigs, like
    the above, or you can add a bunch with ++=:
> buf ++= Seq(4,5,6)
res5: buf.type = ArrayBuffer(1, 42, 99, 4, 5, 6)
1 Like

Thank you very much it is working !

because seq is immuatable you need ArrayBuffer