简体   繁体   English

模式匹配可建立多个序列

[英]Pattern Match build multiple Seqs

I'm looking at a way of building up multiple Seqs using a pattern match like this: 我正在寻找一种使用像这样的模式匹配建立多个Seq的方法:

statuses.map(result => {
    (status.matchStatus, status.source) match {
      case ("Matched", Some(API.name)) => //Add status to a matchedApi seq
      case ("Matched", Some(MANUAL.name)) => //Add status to a matchedManual seq
      case ("Changed", Some(API.name)) => //Add status to a changedApi seq
      case ("Changed", Some(ENTRY.name)) => //Add status to a changedManual seq          
    }
})

Does anyone know if this is theoretically possible or am I going completely the wrong way of building these lists up? 有谁知道这在理论上是否可行,还是我完全错误地建立了这些列表?

If it's acceptable to have dynamic groups you can group you values by status + source tuple 如果可以使用动态组,则可以按状态+源元组对值进行分组

val groups = statuses.groupBy(x => (x.matchStatus, x.source))

and after access it like this 然后像这样访问它

val matchedApi = groups.get("Matched", Some(API.name)).getOrElse(Seq.empty)
val changedManual = groups.get("Changed",Some(MANUAL.name)).getOrElse(Seq.empty)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM