Sła*_*osz 1 functional-programming scala
我在scala中跟随if:
myList是一些List [String]
if (myList.isEmpty) return x > 5
if x < 0 return false
if (myList.head == "+") return foo()
if (myList.head == "-") return bar()
Run Code Online (Sandbox Code Playgroud)
是否可以通过模式匹配来实现?
这有点尴尬,但应该工作:
myList match {
case Nil => x > 5
case _ if x < 0 => false
case "+" :: _ => foo()
case "-" :: _ => bar()
}
Run Code Online (Sandbox Code Playgroud)
请注意,您的匹配并非详尽无遗.