5

is it possible to match Option[Map[String,String]] for some key at once (e.g. without nested matches)?

The following snippet is how it is now:

val myOption:Option[Map[String,String]] = ...
myOption match {
  case Some(params) =>
    params get(key) match {
      case Some(value) => Ok(value)
      case None => BadRequest
  case None => BadRequest     
}
giampaolo
  • 6,906
  • 5
  • 45
  • 73
Johnny Everson
  • 8,343
  • 7
  • 39
  • 75

3 Answers3

9

Sure! Just flatMap that sh*t!

def lookup(o: Option[Map[String, String]], k: String) =
  o.flatMap(_ get k).map(Ok(_)).getOrElse(BadRequest)

If you're using Scala 2.10 you can fold over the Option:

def lookup(o: Option[Map[String, String]], k: String) =
  o.flatMap(_ get k).fold(BadRequest)(Ok(_))
Community
  • 1
  • 1
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
3
(for (params <- myOption; value <- params.get(key)) yield Ok(value)).getOrElse(BadRequest)
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
1

You should be able to do this using a couple of higher-order functions. I think this does what you want:

myOption.collect {
  case m if (m contains key) => Ok(m(key))
} getOrElse BadRequest

collect takes a partial function, and the getOrElse handles the case where the partial function returned None, which translates it to your BadRequest case.

IgnisErus
  • 386
  • 1
  • 5
  • 2
    Downvoter: Care to explain why my answer is incorrect or misleading so I can fix it? – IgnisErus Sep 06 '12 at 14:34
  • It's verbose and unidiomatic, and performs the map lookup twice. – Travis Brown Sep 06 '12 at 15:12
  • 3
    @TravisBrown - _["Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect."](http://stackoverflow.com/privileges/vote-down)_ (i.e. not "I don't find it aesthetically pleasing" or "Less-than-optimally efficent.") A comment explaining that this answer is non-idiomatic and does two map look-ups instead of one would have been much more helpful than an unexplained down-vote. – DaoWen Sep 06 '12 at 15:18
  • 1
    @DaoWen: I think it's fair for me to reserve the right to interpret "dangerous" and "incorrect" however I think reasonable. In this case there are two elegant, idiomatic solutions (mine and Rex's) that will be immediately transparent to any experienced Scala programmer. This one works, technically, but it requires a lot of extra parsing on the part of anyone else who stumbles across it. – Travis Brown Sep 06 '12 at 15:31
  • @TravisBrown - You're right. I can agree that it's reasonable to interpret *unidiomatic* as *incorrect* — that's fair. However, the poster shouldn't need to ask for clarification for a down-vote on a working, non-sloppily-posted answer. – DaoWen Sep 06 '12 at 15:39
  • @DaoWen: Agreed—I try to comment immediately whenever I downvote, but it's a busy morning, etc. My apologies. – Travis Brown Sep 06 '12 at 15:40
  • Thank you for the clarification. Now I see that in this case `flatMap` is definitely a better choice than `collect`. – IgnisErus Sep 06 '12 at 15:44