Add a field to the last value of a seq


I have a requirement to display add a filed in the xml root tag which is of sequence and add that filed only to last iteration of sequence.
I ahve a classs
case class Account(accTran: Seq[AccTran])

case class AccTran(Tran, BookDate,Tamt)

Object class AccTran {
def apply(a: Abc): AccTran =
Acctran(Tran =a.tran,BookDate=a.bookDate,Tamt=a.tamt)


case class Abc( tran,bookDate,tamt)

case class AccBal(balanceAmt: Bigdecimal)

When i get value from the class Abc i will be having Abc sorted based on bookdate.
Account class would be written to xml as seq of AccTran fields

My requirement is to add a field BalanceAmt field only to the last iteration of AccTran seq.

So in which class i can add this field BalanceAmt and write

So output xml should look like with





Please let me know how i can accomplish this.


What have you tried? How far did you get yourself? Do you have any working code that does something close to what you need to do but not quite? Can you show that? What did you try to overcome the part where you got stuck? Can you show what you tried?

Also, in what context do you have this problem, and how much experience do you have in programming in scala and in programming in general?

Yes tried with sliding and copy option and i was able to add field at last sequence.
I am new to scala programming, just started a year back

def addBalanc(trans: Seq(Abc), bal: Seq(Balance)): Seq(tranBal) = {
val sortTran = trans.sortby(_.bookDAte)
val lasttran = sortTran.last

sortTran ++ lastTran sliding(2,1).map {
case(a,b) if lasttran.contains(a) => tran.copy(balAmt = bal.balAmt)
Case(a,_)=> a

What’s the error you’re getting there? I can’t reproduce it, because the code above is full of syntax errors.