简体   繁体   中英

Pattern Match build multiple Seqs

I'm looking at a way of building up multiple Seqs using a pattern match like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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