如何使用模式匹配在scala中获取一个nonEmpty列表?

问题描述 投票:10回答:4

我使用case x :: Nil => ...尝试确保列表是非空的,但它只匹配单个元素列表。如何使用模式匹配获取非空列表?

更新 对不起,看来我输了一些东西,有一个特殊的场景使用了比赛内线,

object AccountResult{
  def unapply(account: AccountResult): Option[(String, List[String])] = ???
}

//ignore accountResult define please 
accountResult match {
  case AccountResult(_, x :: _) => ... 
}

如何匹配accountResult哪个List [String](x :: _)值不是Nil?然后获取匹配的List [String]值

scala pattern-matching
4个回答
18
投票

而不是仅使用Nil指定空列表,指定可以是任何列表的内容,例如:

case x :: tail => ... // tail is a local variable just like x, holding the tail of the list

或者干脆:

case x :: _ => ...

如果你不关心或不会使用尾巴。

这些模式将匹配具有至少一个元素的任何列表(而不是根据现有模式恰好一个元素)。同样,模式:

case x :: y :: the_rest => ...

将匹配任何列表至少有两个元素。

编辑(回复您的评论):

您可以使用“@”分配案例模式中的变量。因此,对于您可能已经看过的(典型用法)示例:

case acc@AccountResult(_, x :: tail) => ... // do something with 'acc'

或者,根据您的评论匹配您正在寻找的用法:

case AccountResult(_, phone@(x :: tail)) => ... // do something with 'phone'

15
投票

要检查列表是否为空,您可以通过以下方式匹配模式:

list match {
   case Nil => false
   case _ => true
}

要么

list match {
  case Nil => false
  case x::xs => true  
}

3
投票

如果您只想将整个非空列表分配给val,而不分割头部和尾部,则只需添加一个匹配空列表的大小写,另一个将列表分配给变量名称,如下所示:

accountResult match {
  case List() => ??? // empty case
  case myAccountResult => ??? //myAccountResult will contain the whole non-empty list  
}

Nil也做了这个工作,匹配一个空列表

accountResult match {
  case Nil => ??? // empty case
  case myAccountResult => ??? //myAccountResult will contain the whole non-empty list  
}

2
投票

如果这是您经常使用的东西,您可以创建这样的自定义匹配器:

object NonEmpty { 
  def unapply(l: List[_]) = l.headOption.map(_ => l)
}

这可以这样使用:

scala> List() match { case NonEmpty(l) => println(l) }
scala.MatchError: List() (of class scala.collection.immutable.Nil$)
  ... 33 elided

scala> List(43) match { case NonEmpty(l) => println(l) }
List(43)

scala> List(43, 32) match { case NonEmpty(l) => println(l) }
List(43, 32)
© www.soinside.com 2019 - 2024. All rights reserved.