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

  • SEARCH
  • Home
  • 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 9205379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:56:14+00:00 2026-06-17T23:56:14+00:00

Yesterday I was suddenly enlighted and understood how and why people use ‘map’ method

  • 0

Yesterday I was suddenly enlighted and understood how and why people use ‘map’ method with Option to compare values. Yes, I’m a bit slow, sorry 🙂

I revised these very nice links and came to the question I would like to ask.

http://twitter.github.com/effectivescala

http://blog.tmorris.net/posts/scalaoption-cheat-sheet

In my Lift webapp I have some Option[User] and Option[Server] variables. I’m trying to find out if this User is admin of this Server by the following check

if(user.map(_.id) == server.map(_.adminId))

But I noticed that in case of ‘user’ is None and ‘server’ is also None this check succeeds which is not good for me (if any of them is None I’d like this check to fail). I could add user.isDefined condition but I feel there is more correct way to do it. Could you tell how to accomplish it in Scala way?

  • 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-17T23:56:15+00:00Added an answer on June 17, 2026 at 11:56 pm

    You could do it with pattern matching (which in this case is probably the clearest way):

    (user, server) match {
      case (Some(user), Some(server)) if user.id == server.adminId =>
        // both ids are matching, handle this case here
      case _ =>
        // no match, handle this case here
    }
    

    You could also try as a one-liner but here I don’t advise it as it’s pretty obfuscated:

    if ( user.flatMap{ user => server.map(_.adminId == user.id) }.getOrElse( false ) ) {
      // both ids are matching, handle this case here
    }
    else {
      // no match, handle this case here
    }
    

    Finally, if you only have to handle the case where the ids match (and would just do nothing if there is not), using a for comprehension is not too bad of an option (no pun intended):

    for ( user <- user; server <- server if user.id == server.adminId ) {
      // both ids are matching, handle this case here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

yesterday afternoon Visual Studio 2008 suddenly started taking a really long time to open
Yesterday I testet a lot on my device. Suddenly I get this error message
I installed VS 2010 RC yesterday, and suddenly, SQL Server CE isn't loading files
Ok, I am very confused. Yesterday it worked just fine. Today suddenly I start
Yesterday I asked Are GUIDs generated on Windows 2003 safe to use as session
I have a strange problem. Everything was working fine untill yesterday and suddenly just
In my C# class that was working well and suddenly from yesterday when I
Yesterday I had a huge problem - suddenly my cocos2d project stopped compiling. You
Yesterday was working on a iPhone app with a tab bar when suddenly IB
Yesterday it was working and showing me option for building on Device , iphone

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.