I have a function that receives a String like the following:-10.0 -10.0,10.0 10.0,0.0 0.0And needs to translate that into a Seq[Coordinate] that would look like this:Seq(    Coordinate(-10.0, -10.0),    Coordinate( 10.0,  10.0),    Coordinate(  0.0,   0.0))Here is the function that I wrote to achieve this:def toCoordinates(coordinates: String): Seq[Coordinate] = {  val coordinatesList = coordinates.split(,)  coordinatesList flatMap { coordinate =>    coordinate.split(\\s+) match {      case Array(lat:String, lon:String) => Some(Coordinate(lat.toDouble, lon.toDouble))      case _                             => None    }  }}I see that if someone sends foo bar as a coordinate, my code would fail miserably, although I don't see a way to prevent this without some if conditions.Any idea on how I can implement this check while still having functional code? Or any other tips that might be helpful for a Scala newbie.
Convert a String with comma separated latitudes and longitudes to a Seq[Coordinate]
beginner;parsing;scala;coordinate system
Instead of writing lat.toDouble and lon.toDouble, I would use s.split(\\s+).map(_.toDouble) to convert all elements.  The type annotation within the match seems excessive to me here.To discard invalid coordinates such as foo bar, you would have to catch a NumberFormatException.Note that split(\\s+) is not tolerant of spaces after commas.  For example,  1.0 2.0.split(\\s+) would produce Array(, 1.0, 2.0), which would be a three-element array and thus considered invalid.def toCoordinates(ss: String): Seq[Coordinate] = {  ss.split(,).flatMap { s =>    try {      s.split((?<!^)\\s+).map(_.toDouble) match {        case Array(lat, lon) => Some(Coordinate(lat, lon))        case _               => None             }    } catch {      case e: NumberFormatException => None    }  }}