You can use the Try combination and display / filter.
Try to include the calculation in Success if they behave as expected, or Failure if an exception is thrown. Then you can filter what you want - in this case, successful calculations, but you can also filter out cases of errors for logging, for example.
The following code is a possible starting point. You can run and study it at scastie.org to find out if it suits your needs.
import scala.util.Try object Main extends App { val in = List("1", "2", "3", "abc") val out1 = in.map(a => Try(a.toInt)) val results = out1.filter(_.isSuccess).map(_.get) println(results) }
gamsd source share