Reputation: 11741
I want to filter out bad input from input data. I'm currently using scala.util.Try
to wrap any exceptions. Following is a simple example where 3I throws a NumberFormatException
. I was wondering if there is a better way of doing this in Scala ?
val data = List ( ("Joe", "20"), ("James", "30"), ("Pete", "3I") )
scala> val parsedData = data.map{ d => Try{Person( d._1, d._2.toInt ) }}
parsedData: List[scala.util.Try[Person]] = List(Success(Person(Joe,20)), Success(Person(James,30)), Failure(java.lang.NumberFormatException: For input string: "3I"))
scala> val validdata = parsedData.map{ x => x match {
| case Success(s) => Some(s)
| case Failure(f) => None }
| }
validdata: List[Option[Person]] = List(Some(Person(Joe,20)), Some(Person(James,30)), None)
scala> validdata.flatten
res13: List[Person] = List(Person(Joe,20), Person(James,30))
Upvotes: 7
Views: 4436
Reputation: 20415
One approach that bypasses the use of Try
, as follows,
for ( d <- data if d._2.forall(_.isDigit) ) yield Person(d._1, d._2.toInt)
Yet this may not prove as scalable as @m-z approach.
Upvotes: 0
Reputation: 55569
Use collect
to keep only the values that match the pattern you desire:
parsedData collect { case Success(x) => x }
This will also work, though I don't think it's quite as clear:
parsedData.flatMap(_.toOption)
Upvotes: 16