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 8354373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:31:48+00:00 2026-06-09T09:31:48+00:00

Seeking a more elegant solution I have this piece of code, I just use

  • 0

Seeking a more elegant solution

I have this piece of code, I just use it in test cases where it isn’t necessary to do any error handling. What it does is:

  • take an input list of strings
  • parse them using the DSJSonmapper.parseDSResult method
  • filters them and extracts the Right value from each Either (Left is an Exception)

The code is as follows:

  def parseDs(ins: List[String]) = {
    def filterResults[U, T](in: List[Either[U, T]]): List[T] = {
      in.filter(y => y.isRight).map(z => z.right.get)
    }
    filterResults(ins.map(x => DSJsonMapper.parseDSResult(x)))
  }

Now, I haven’t done an awful lot of polymorphic functions, but this works. However I feel like it’s a bit ugly. Has anyone got a better suggestion, how to accomplish the same thing.

I’m aware this is going to come down to a case of personal preference. But suggestions are welcome.

  • 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-09T09:31:50+00:00Added an answer on June 9, 2026 at 9:31 am

    collect is made for exactly this kind of situation:

    def filterMe[U,T](in: List[Either[U,T]]): List[T] = in.collect{
      case Right(r) => r
    }
    

    In fact, it’s so good at this you may want to skip the def and just

    ins.map(DSJsonMapper.parseDsResult).collect{ case Right(r) => r }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of an opinion seeking question, so there may not be a
This is more of an general architecture problem. If you have a cron job
I am going to start updating this to help those seeking to use this
This isn't much of a coding question as opposed to seeking help on going
I've been seeking for this problem through all google, stackoverflow and more. And I
Just seeking confirmation here : apache Thrift protocol does not seem to support running
I'm seeking a solution to splitting a string which contains text in the following
I have tried to ask a variant of this question before. I got some
I have a model and in that model I'm generating a more complex field
I am seeking concrete and unique features that Lisp (any dialect) offers to a

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.