I have following if’s in scala:
myList is some List[String]
if (myList.isEmpty) return x > 5
if x < 0 return false
if (myList.head == "+") return foo()
if (myList.head == "-") return bar()
Is it possible to do it with pattern matching?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To me this one is prettier:
But I’m not sure if this doesn’t confront with logical flow (e.g. early exit when list is empty — does it break something or not in your context?), if so, feel free to say so or downvote.