如何使用模式匹配在 Scala 中获取非空列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34585248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use pattern match get a nonEmpty list in scala?
提问by LoranceChen
I'm use case x :: Nil => ...attempt to make sure the list is nonEmpty, but it just match single element list.How can I use pattern match get a nonEmpty list?
我case x :: Nil => ...试图确保列表是非空的,但它只匹配单个元素列表。如何使用模式匹配获取非空列表?
UPDATED
I'm sorry,it seems I lose something, there is a special scene use the match inner,
更新
对不起,我好像丢了东西,有一个特殊的场景使用匹配内部,
object AccountResult{
def unapply(account: AccountResult): Option[(String, List[String])] = ???
}
//ignore accountResult define please
accountResult match {
case AccountResult(_, x :: _) => ...
}
how can I match accountResult which List[String] (x :: _) value is not Nil? and then get the matched List[String] value
如何匹配 List[String] (x :: _) 值不是 Nil 的 accountResult?然后获取匹配的 List[String] 值
回答by Shadowlands
Instead of specifying only the empty list with Nil, specify something that can be any list, eg.:
不是只用Nil指定空列表,而是指定可以是任何列表的内容,例如:
case x :: tail => ... // tail is a local variable just like x, holding the tail of the list
or simply:
或者干脆:
case x :: _ => ...
if you don't care about or won't be using the tail.
如果你不关心或不会使用尾巴。
These patterns will match any list with at leastone element (rather than exactly oneelement as per your existing pattern). Similarly, the pattern:
这些模式将匹配具有至少一个元素的任何列表(而不是根据您现有的模式正好匹配一个元素)。同样,模式:
case x :: y :: the_rest => ...
will match any list with at leasttwo elements.
将匹配具有至少两个元素的任何列表。
Edit(response to your comment):
编辑(回复您的评论):
You can assign to a variable within a case pattern, using "@". So, for (a typical usage) example that you may have seen already:
您可以使用“ @”分配给案例模式中的变量。因此,对于您可能已经看到的(典型用法)示例:
case acc@AccountResult(_, x :: tail) => ... // do something with 'acc'
or, matching the usage you are seeking per your comment:
或者,根据您的评论匹配您正在寻求的用法:
case AccountResult(_, phone@(x :: tail)) => ... // do something with 'phone'
回答by Nyavro
To check if list is non empty you can pattern match this way:
要检查列表是否非空,您可以通过这种方式进行模式匹配:
list match {
case Nil => false
case _ => true
}
Or
或者
list match {
case Nil => false
case x::xs => true
}
回答by Visiedo
If you just want to assign a whole non-empty list to a val, without splitting head and tail, you just add a case matching an empty list, and another one assigning the list to a variable name like this:
如果您只想将整个非空列表分配给一个 val,而不拆分头和尾,您只需添加一个与空列表匹配的案例,另一个将列表分配给一个变量名,如下所示:
accountResult match {
case List() => ??? // empty case
case myAccountResult => ??? //myAccountResult will contain the whole non-empty list
}
Nildoes the job as well, matching an empty list
Nil也可以完成这项工作,匹配一个空列表
accountResult match {
case Nil => ??? // empty case
case myAccountResult => ??? //myAccountResult will contain the whole non-empty list
}
回答by thoredge
If this is something you use often you can create a custom matcher like this:
如果这是你经常使用的东西,你可以像这样创建一个自定义匹配器:
object NonEmpty {
def unapply(l: List[_]) = l.headOption.map(_ => l)
}
This can be used like this:
这可以像这样使用:
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)

