Scala: how to understand the flatMap method of Try?

后端 未结 6 2479
情深已故
情深已故 2021-02-20 05:29

The flatMap method of the Success is implemented like this:

  def flatMap[U](f: T => Try[U]): Try[U] =
    try f(value)
    catch {
      case NonFatal(e) =&g         


        
6条回答
  •  梦如初夏
    2021-02-20 06:17

    A regular flatMap takes a sequence of sequences, and put all the elements into one big "flat" sequence

    It would be fair to replace word sequence to monad here, because this operation doesn't relate only to collection, actually collections are also monads. Think of Try as collection that can contain either Success value of Failure

提交回复
热议问题