0

I want to replace the below match with an if statement, preferably less verbose than this. I personally find if's to be easier to parse in the mind.

val obj = referencedCollection match{
            case None => $set(nextColumn -> MongoDBObject("name" -> name))
            case Some( collection) =>....}

Is there an equivalent if statement or some other method that produces the equivalent result?

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
  • 7
    If you prefer and want to use `if` in this situation, don't use scala, you will hit a wall soon or later. – Nicolas Oct 22 '13 at 18:56

4 Answers4

7

You can replace the pattern match by a combination of map and getOrElse:

ox match {
  case None => a
  case Some(x) => f(x)
}

can be replaced by

ox.map(f).getOrElse(a)
ziggystar
  • 28,410
  • 9
  • 72
  • 124
3

There is huge number of options (pun intended):

if (referencedCollection != None) { ... } else { ... }
if (referencedCollection.isDefined) { ... } else { ... } // @Kigyo variant
if (referencedCollection.isEmpty) { /* None processing */ } else { ... }
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
3

You probably ought not indulge yourself in continuing to use if in conditions like this. It's not idiomatic, rarely is faster, is more verbose, requires intermediate variables so is more error-prone, etc..

Oh, and it's a bad idea to use anything with $ signs!

Here are some other patterns that you might use in addition to match:

val obj = referenceCollection.fold( $set(nextColumn -> MongoDBObject("name" -> name) ){
  collection => ...
}

val obj = (for (collection <- referenceCollection) yield ...).getOrElse{
  $set(nextColumn -> MongoDBObject("name" -> name)
}

val obj = referenceCollection.map{ collection => ... }.getOrElse{
  $set(nextColumn -> MongoDBObject("name" -> name)
}

You can basically think of map as the if (x.isDefined) x.get... branch of the if and the getOrElse branch as the else $set... branch. It's not exactly the same, of course, as leaving off the else gives you an expression that doesn't return a value, while leaving off the getOrElse leaves you with an unpacked Option. But the thought-flow is very similar.

Anyway, fold is the most compact. Note that both of these have a bit of runtime overhead above a match or if statement.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • *Oh, and it's a bad idea to use anything with $ signs!* It's casbah dsl which tries to be as [close to original mongo](http://docs.mongodb.org/manual/reference/operator/update/set/) as it can. Likely you have to tell this to @brendan-w-mcadams – om-nom-nom Oct 23 '13 at 09:14
  • I choose fold as it conveys its meaning perfectly and concisely; even if the statement is presented outside it's context. – Jesvin Jose Oct 23 '13 at 18:25
  • Had to read http://stackoverflow.com/questions/12321164/option-fold-in-scala-2-10 and http://www.scala-lang.org/api/current/index.html#scala.Option to fully get it. – Jesvin Jose Oct 23 '13 at 18:29
1

You could do it like this:

val obj = if(referencedCollection.isDefined) { (work with referencedCollection.get) } else ...
Kigyo
  • 5,668
  • 1
  • 20
  • 24