Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:24:42+00:00 2026-06-11T16:24:42+00:00

is there a better way to write the code below? val t = map.get(‘type).getOrElse();

  • 0

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("").????
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:24:44+00:00Added an answer on June 11, 2026 at 4:24 pm

    Map has its own getOrElse method, so you can just write the following:

    val t = map.getOrElse('type, "")
    

    Which accomplishes the same thing as the definition of t in 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":

    map.get('type).map("prefix" + _).getOrElse("")
    

    Or, if you’re using Scala 2.10:

    map.get('type).fold("")("prefix" + _)
    

    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:

    map.get('type).filter(_.nonEmpty).map("prefix" + _).getOrElse("")
    

    This probably isn’t necessary, though—it sounds like you don’t expect to have empty strings in your map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there is a better way to write my code below.
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better way to write the below, other then using a switch
Is there a better way to write this spec? This works but I don't
Is there a better way to write the following function? Having the '#' +
I am wondering if there is a better way to write this JavaScript (Jquery)
is there any way to write this foreach in linq or another better way,
Is there a better way to code this? def __contains__(self, e): return e in
Please review my brief example code below and tell me if there is better,
Is there better way to delete a parameter from a query string in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.