Match multiple cases classes in scala
Looks like you don't care about the values of the String parameters, and want to treat B and C the same, so:
def matcher(l: Foo): String = {
l match {
case A() => "A"
case B(_) | C(_) => "B"
case _ => "default"
}
}
If you must, must, must extract the parameter and treat them in the same code block, you could:
def matcher(l: Foo): String = {
l match {
case A() => "A"
case bOrC @ (B(_) | C(_)) => {
val s = bOrC.asInstanceOf[{def s: String}].s // ugly, ugly
"B(" + s + ")"
}
case _ => "default"
}
}
Though I feel it would be much cleaner to factor that out into a method:
def doB(s: String) = { "B(" + s + ")" }
def matcher(l: Foo): String = {
l match {
case A() => "A"
case B(s) => doB(s)
case C(s) => doB(s)
case _ => "default"
}
}
How to use multiple cases in Match (switch in other languages) cases in Python 3.10
According to https://docs.python.org/3/whatsnew/3.10.html#pep-634-structural-pattern-matching, you use a |
between patterns.
case 'Egide' | 'Eric':
How to reference a variable when multiple cases are combined?
You can replace your first case to:
case (Some(name), Some(Gender.Male) | Some(Gender.FeMale))=> s"$name gender is either male or female"
Update - even better - Thanks to @Astrid
case (Some(name), Some(Gender.Male | Gender.FeMale)) => s"$name gender is either male or female"
Multiple case pattern matching
Your syntax is pretty close. Try giving your lambda multiple signatures, which syntactically looks very similar to a case:
defmodule TestMerge do
def merge(map1, map2) do
Map.merge(map1, map2, fn
_k, l1, l2 when is_list(l1) and is_list(l2) ->
#they are lists, do something with them
:lists
_, _, _ ->
#they are other thing
:not_lists
end)
end
end
To test, we call it with all four possibilities (the keys indicate which combination, e.g. both maps have a list after the :both
key, the first map has a list after the :first
key, but not the second map, etc.):
iex> TestMerge.merge(%{first: [], last: 1, both: [], neither: 1},
...> %{first: 2, last: [], both: [], neither: 1})
%{both: :lists, first: :not_lists, last: :not_lists, neither: :not_lists}
Is there a way to include and operator in a case/match condition?
How about doing something like this?
cond1 = 'key' in dict_1
cond2 = 'key' in dict_2
match (cond1, cond2):
case (True, True):
...
case (True, False):
...
case (False, True):
...
case (False, False):
...
C# 8 switch expression: Handle multiple cases at once?
As of C#9, you can do exactly what you wanted via "disjunctive or
" patterns:
private static GameType UpdateGameType(GameType gameType) => gameType switch
{
GameType.RoyalBattleLegacy or GameType.RoyalBattleNew => GameType.RoyalBattle,
GameType.FfaLegacy or GameType.FfaNew => GameType.Ffa,
_ => gameType;
};
Further reading:
- What's new in C# 9.0: pattern matching enhancements
- Patterns C# reference
Multiple Patterns in 1 case
Given that you've tagged your question with the smlnj tag, then yes, SML/NJ supports this kind of patterns. They call it or-patterns and it looks like this:
case str of
("+" | "-") => print "PLUS MINUS"
| ("*" | "/") => print "MULT DIV"
Notice the parentheses.
The master branch of MLton supports it too, as part of their Successor ML effort, but you'll have to compile MLton yourself.
val str = "+"
val _ =
case str of
"+" | "-" => print "PLUS MINUS"
| "*" | "/" => print "MULT DIV"
Note that MLton does not require parantheses. Now compile it using this command (unlike SML/NJ, you have to enable this feature explicitly in MLton):
mlton -default-ann 'allowOrPats true' or-patterns.sml
can I combine multiple string matching cases in scala?
Yes you can simply use or (|)
to match
one of the pattern
,
scala> "hi" match { case "hello" | "hi" => println("fantastic") case _ => println("very very bad")}
fantastic
scala> "hello" match { case "hello" | "hi" => println("fantastic") case _ => println("very very bad")}
fantastic
scala> "something else" match { case "hello" | "hi" => println("fantastic") case _ => println("very very bad")}
very very bad
You can also use regex to pattern match, especially useful when there are many criterias to match,
scala> val startsWithHiOrHello = """hello.*|hi.*""".r
startsWithHiOrHello: scala.util.matching.Regex = hello.*|hi.*
scala> "hi there" match { case startsWithHiOrHello() => println("fantastic") case _ => println("very very bad")}
fantastic
scala> "hello there" match { case startsWithHiOrHello() => println("fantastic") case _ => println("very very bad")}
fantastic
scala> "non of hi or hello there" match { case startsWithHiOrHello() => println("fantastic") case _ => println("very very bad")}
very very bad
Refer to Scala multiple type pattern matching and Scala match case on regex directly
Related Topics
How to Display Data from Firebase Faster
Lesser Than or Greater Than in Swift Switch Statement
@Noescape Attribute in Swift 1.2
How to Create Swift Class for Category
How to Make Class Methods/Properties in Swift
Why Use Required Initializers in Swift Classes
Find Difference in Seconds Between Nsdates as Integer Using Swift
What's the Difference Between Struct Based and Class Based Singletons
Swift 2/iOS 9 - Libz.Dylib Not Found
Any Way to Iterate a Tuple in Swift
Swift Protocol Error: 'Weak' Cannot Be Applied to Non-Class Type
Show Am/Pm in Capitals in Swift
How to Add Alamofire Url Parameters
How to Create a Generic Array Extension That Sums Number Types in Swift