Home > Software engineering >  Pattern matching or Map Monads
Pattern matching or Map Monads

Time:09-21

I was wondering what do you guys think is more idiomatic and better in terms of performance.

Over a monad of type Option or Try, use pattern matching or map and getOrElse in case you want to control the side effect.

So what do you think is better this:

maybeConnectTimeout
  .map(connectTimeout => session.connect(connectTimeout))
  .getOrElse(session.connect())

Or this

maybeConnectTimeout match {
  case Some(connectTimeout) => session.connect(connectTimeout)
  case None => session.connect()
}

CodePudding user response:

what ... is ... better in terms of performance.

According to odersky pattern match

I am surprised that the pattern match gets so little love here. Not only is it by far the fastest (probably at least 10 times as fast as the alternatives), it is also the clearest.

  • Related