Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:08:50+00:00 2026-06-04T18:08:50+00:00

I am working on a method that has 3 possible outcomes for multiple items:

  • 0

I am working on a method that has 3 possible outcomes for multiple items: Error, Invalid and Success. For each of these I need to return a json list identifying which items were in error, invalid and successful.

My current attempt follows. I have used Object to represent the class my objects are as fully explaining would take too long. The Object class has a method process which returns a boolean to indicate success or error and throws an exception when the object is invalid:

def process(list: List[Objects]) = {
    val successIds = new ListBuffer[Int]();
    val errorIds = new ListBuffer[Int]();
    val invalidIds = new ListBuffer[Int]();

    list.foreach( item => {
        try {
            if (item.process) {
                successIds ++ item.id
            } else {
                errorIds ++ item.id
            }
        } catch {
            case e: Exception => invalidIds ++ item.id
        }
    })

    JsonResult(
        Map("success" -> successIds, 
            "failed" -> errorIds, 
            "invalid" -> invalidIds)
    ) 
}

Problem is using Mutable data structures isn’t very “Scala-y”. I would prefer to build up these lists in some more functional way but I am quite new to scala. Any thoughts or hints as to how this might be done?

My though is using something like the flatMap method that takes a tuple of collections and collates them in the same way the flatMap method does for a single collection:

def process(list: List[Objects]) = {

    val (success, error, invalid) = list.flatMap( item => {
        try {
            if (item.process) {
                (List(item.id), List.empty, List.empty)
            } else {
                (List.empty, List(item.id), List.empty)
            }
        } catch {
            case e: Exception => 
                (List.empty, List.empty, List(item.id))
        }
    })

    JsonResult(
        Map("success" -> success, 
            "failed" -> error, 
            "invalid" -> invalid)
    ) 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:08:51+00:00Added an answer on June 4, 2026 at 6:08 pm

    flatMap isn’t what you need here – you need groupBy:

    def process(list: List[Objects]) = {
    
      def result(x: Objects) =  
        try if (x.process) "success" else "failed"
        catch {case _ => "invalid"}     
    
      JsonResult(list groupBy result mapValues (_ map (_.id)))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the application that I'm currently working on, it is possible for multiple users
I am working on a method that will allow me to pull in the
I'm working on an extension method that's only applicable to reference types. I think,
I'm currently working with a textbox that has a background. I was wondering if
Is it possible to return 2 selects with 1 Iqueryable? I have a method
I'm working on a project that has me approximating text rendered as an image
I am working on a C++ app which internally has some controller objects that
I'm working on a project that uses MSTest for unit testing and I need
I am working on a lab and need some assistance if possible. I have
I am working in a project that has two main parts: a class library

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.