score:10
There is this:
def oneOf[A](tup: (Option[A], Option[A])) = tup._1.orElse(tup._2)
That will return the first option that is defined, or None if neither is.
Edit:
Another way to phrase the same thing is
def oneOf[A](tup: (Option[A], Option[A])) =
tup match { case (first, second) => first.orElse(second) }
It's longer, but perhaps more readable.
score:2
I want always to extract always the
Some
value if it exists, and otherwise get theNone
You can just use orElse
def orOption[T](p: (Option[T], Option[T])): Option[T] = {
val (o1, o2) = p
o1 orElse o2
}
However, this does decide what to do if there exists two Some
values:
scala> orOption((Some(1), Some(2)))
res0: Option[Int] = Some(1)
You should probably use pattern matching and then decide what to do if there are two Some
values, like throw an exception. Alternatively, consider using a better encoding for the result type than Option
.
score:4
This should work:
def f(t: (Option[Int], Option[Int])): Option[Int] = t match {
case (Some(n), _) => Some(n)
case (_, Some(n)) => Some(n)
case _ => None
}
Source: stackoverflow.com
Related Query
- Scala Tuple Option
- Scala represent an Option tuple
- Unpacking a Tuple of Option in scala
- scala tuple unpacking
- Scala Some v. Option
- Convert Option to Either in Scala
- Scala Option - Getting rid of if (opt.isDefined) {}
- How to append or prepend an element to a tuple in Scala
- Scala transform list of tuples to a tuple of lists
- Scala - Two Lists to Tuple List
- Simple question about tuple of scala
- Scala - pattern-matching a tuple of related types
- Check for None in Scala Option type isEmpty method
- How to slice a tuple in scala
- Scala Map from tuple iterable
- Scala Tuple Deconstruction
- Difference between Java Optional and Scala Option
- Scala Regex enable Multiline option
- Convert Scala Option to Java Optional
- Packing scala tuple to custom class object
- scala - add a tuple to listBuffer
- Converting a tuple of options to an option of tuple with Scalaz or Shapeless
- Can I name a tuple (define a structure?) in Scala 2.8?
- Scala tuple memory overhead
- Enumeration concept in Scala - Which option to take?
- How to get the option to create a new Scala Worksheet in IntelliJ?
- How to init List of tuple and add item in scala
- Scala split string to tuple
- Scala Tuple to String (using mkString)
- Scala - finding a specific tuple in a list
More Query from same tag
- Inheriting a trait twice
- Drop Constant columns Non numeric data
- Deploying Scala to NixOps
- Why is "Unable to find encoder for type stored in a Dataset" when creating a dataset of custom case class?
- In thymeleaf using link to pass a hidden value
- how to return a require call from macro in scala.js
- scala copy update fields into case classes
- Sbt testOnly from Windows console
- Converting a row in a column to LocalDate in Spark
- Code sharing between spark workers
- Implicit in Scala 'for' enumerator?
- List[Row] to RDD[CassandrRow] conversion in scala
- Scala inconclusive Assertion
- Best way to write Scala methods signature dealing with exceptions
- Create a subset of a Dataframe
- Check multiple exceptions in Scala retry control structure
- Add a Scala Map into another Scala Map
- Clarifying cross version behavior in Scala
- Use forall instead of filter on List[A]
- Simple question about tuple of scala
- Regex pattern equality
- Generic recursive type in Scala
- Equivalent to Ruby's #tap method in Scala
- How to reference subclasses of static Java classes with generics in Scala
- Scala read csv file and sort the file
- Gradle, IntelliJ and ScalaTest plugin: suddenly broken
- Scala: Convenience construtor, a constructor that is lazy and requires no delay
- Unable to find encoder for Decimal type stored in a DataSet
- Spark standalone cluster read parquet files after saving
- Spark Scala Jaas configuration