scala> import scala.util.Try
import scala.util.Try
scala> def tryToInt( s: String ) = Try(s.toInt).toOption
tryToInt: (s: String)Option[Int]
scala> tryToInt("123")
res0: Option[Int] = Some(123)
scala> tryToInt("")
res1: Option[Int] = None
More Related Contents:
- Composing Option with List in for-comprehension gives type mismatch depending on order
- What are the differences between asInstanceOf[T] and (o: T) in Scala?
- Write to multiple outputs by key Spark – one Spark job
- How does HashPartitioner work?
- Enforce type difference
- Task not serializable: java.io.NotSerializableException when calling function outside closure only on classes not objects
- Operator precedence in Scala
- Scala foreach strange behaviour
- What’s the difference between multiple parameters lists and multiple parameters per list in Scala?
- What’s the difference between == and .equals in Scala?
- Reading DataFrame from partitioned parquet file
- Scala: convert map to case class
- Build.scala, % and %% symbols meaning
- Result type of an implicit conversion must be more specific than AnyRef
- Perform a typed join in Scala with Spark Datasets
- Left Anti join in Spark?
- DataFrame-ified zipWithIndex
- How to access parameter list of case class in a dotty macro
- How do I parse command line arguments in Scala? [closed]
- How to print the contents of RDD?
- Is there an equivalent to SuppressWarnings in Scala?
- How to restrict actor messages to specific types?
- Spark2.1.0 incompatible Jackson versions 2.7.6
- Can Scala actors process multiple messages simultaneously?
- Coalesce reduces parallelism of entire stage (spark)
- How do I get an instance of the type class associated with a context bound?
- Scala – initialization order of vals
- SBT stop run without exiting
- What is the differences between Int and Integer in Scala?
- scala parallel collections degree of parallelism