score:3
Accepted answer
A NodeSeq
is not a List
, so trying to pattern match it as one will fail. If you wanted to match it using unapplySeq
of some kind, you would need to do it in this way:
def exactlyOne[A](xs: Seq[A]): Option[A] = xs match {
case Seq(head) => Some(head)
case _ => None
}
scala> exactlyOne(<root>hello</root>)
res5: Option[scala.xml.Node] = Some(<root>hello</root>)
scala> exactlyOne(<root>hello</root><foo>world</foo>)
res6: Option[scala.xml.Node] = None
Source: stackoverflow.com
Related Query
- Scala XML Pattern matching and Attributes
- Using Scala pattern matching to extract XML elements with a certain name, regardless of content
- scala xml pattern matching on same-name elements
- How is pattern matching in Scala implemented at the bytecode level?
- Why does pattern matching in Scala not work with variables?
- Scala multiple type pattern matching
- Scala pattern matching on sequences other than Lists
- Scala pattern matching confusion with Option[Any]
- How does Pattern Matching in Scala overcome duplication that switch case causes?
- What exactly did Scala improve with pattern matching in 2.10?
- Pattern matching on generic type in Scala
- Pattern matching against Scala Map type
- Pattern matching structural types in Scala
- Is there any fundamental limitations that stops Scala from implementing pattern matching over functions?
- Scala pattern matching against URLs
- Scala Pattern Matching with Sets
- Pattern matching on a list in Scala
- Scala - complex conditional pattern matching
- Scala - pattern matching with conditional statements?
- Scala pattern matching with lowercase variable name
- Scala Get First and Last elements of List using Pattern Matching
- Scala Option: map vs Pattern Matching
- Case classes, pattern matching and curried constructors in Scala
- Scala pattern matching Java enum value
- What is the advantage of using scala pattern matching instead of java switch case?
- Scala pattern matching keep saying "match is not exhaustive!"
- Scala - designation of a matched item in pattern matching
- Pattern matching over non-case class in Scala
- Unexpected Scala pattern matching syntax
- How to assign name to intermediate whilst pattern matching in Scala
More Query from same tag
- how to handle hbase connection closure on spark when worker shuts down in scala
- Feed streaming HTTP to Scala's XMLEventReader?
- Why is casting from long to date not allowed in spark?
- Asynchronous results in Play 2.0 Framework (scala)
- how do I fix the filter map and mapWithState function
- Completing a Promise[T] with Async in Scala and Play2
- What is the input for `f` function? in Optional exercise?
- android development with scala
- SBT: input task to generate a test source by name
- .zip three futures in Scala
- spark scala type mismatch with zipwithIndex in groupbykey
- java.lang.NoClassDefFoundError: grizzled/slf4j/Logger
- Pattern Match on Case Objects with Type Members
- Creating a user defined function in Spark to process a nested structure column
- Slick query very slow
- Why Scala match is superior to if statements?
- generate doubles between a and b
- Spark SQL join really lazy?
- Having a custom QueryStringBindable in the scope with Play 2.4.3
- type constraints and reifications regarding to joinLeft of Either
- Making a dataframe row from a list buffer into one column
- PartialFunction in Scala
- ScalaCheck specificy minimum successful tests for property
- How to implement Java Interface (for Kafka) in Scala?
- Generic way to implement a Pk Writes in Play Scala
- How to do this in Scala way: get the first element from Option[Seq[String]]
- Add JAR files to a Spark job - spark-submit
- Funtional way to find empty intervals in a list of tuples with start and end times
- How to build processing in Scala depending on the multiple future Completion
- Spark streaming : com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Class is not registered: scala.Tuple2$mcJZ$sp