1

I'm new in scala language I have a listbuffer :

    var oldQuestions: Seq[Question] = section.questions
    var newQuestions: ListBuffer[Question] = new ListBuffer()

So all I need is,to loop over the newQuestions list and access to one question based on her id and delete it. Any help would be very appreciated.

1 Answer 1

3

Don't use vars, and mutable collections. It is a really, really rare occurrence that you actually need either of those in scala. So, for now, until you get sufficient grip on the language to be able to tell when those rare cases happen, just pretend these things don't exist. Learn to write good functional code before you explore mutability.

To answer your question:

  val newQuestions: Seq[Question] = section.questions.filterNot(_.id == idToDelete)
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your help, i'm not familiar with scala, i'm trying to learn it and work on a project in the same time.
I figured that much. Thus my advice. While learning, stay away from mutability.
Some people are more Roman than the Pope - sometimes you need mutability
@thebluephantom Sometimes you do ... But not before you are familiar with the language enough to correctly recognize those cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.