is there a better way to write the code below?
val t = map.get('type).getOrElse("");
if (t != "") "prefix" + t;
be interested in inline code something like
val t = map.get('type).getOrElse("").????
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.
Maphas its owngetOrElsemethod, so you can just write the following:Which accomplishes the same thing as the definition of
tin your first example.To address your comment: If you know your map will never contain the empty string as a value, you can use the following to add the
"prefix":Or, if you’re using Scala 2.10:
If your map can have
""values, this version will behave a little differently than yours, since it will add the prefix to those values. If you want exactly the same behavior as your version in a one-liner, you can write the following:This probably isn’t necessary, though—it sounds like you don’t expect to have empty strings in your map.