我有一個集合,我想匹配任何一個元素的另一個變量。我知道我可以手動執行此操作是這樣的:如何匹配一組可能的值?
fruits = Set("a", "b", "c", "d")
toMatch = ("a", "fruit")
toMatch match {
case (("a" | "b" | "c" | "d", irrelevant)) => true
}
但是,有沒有辦法在比賽語句中使用的水果,所以我不必手動展開
編輯:我目前使用一個如果要做這種情況下,我想知道是否有一些語法糖,我可以用來做內聯
fruits = Set("a", "b", "c", "d")
toMatch = ("a", "fruit")
toMatch match {
case ((label, irrelevant)) if fruits.contains(label) => true
}
如果沒有其他的答案,伊利諾伊州紀念誰用,如果作爲解決方案迴應的第一人!抱歉,這裏缺乏清晰度。
EDIT2:這樣做的原因,如果你想知道是
fruits = Set("a", "b", "c", "d")
vegetables = Set("d", "e", "f")
toMatch = ("a", "fruit")
toMatch match {
case ((label, "fruit")) if fruits.contains(label) => true
case ((label, "vegetable")) if vegetables.contains(label) => true
}
我想這兩種情況結合起來,所以我有一個條件每個返回類型
我目前使用的if語句這樣做,但不知道是否有一些語法糖要做到這一點 – Nicomoto 2014-10-29 21:48:40
你的意思是像'fruits.contains(「A」)'(或其他)? – 2014-10-29 21:50:47