Scala polymorphic function for filtering an input List of Either

Viewed 1255

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.

2 Answers
Related