为了理解而理解

Understanding for-comprehension

我有以下特点:

trait Command
trait Status
trait Tr{
    def getOpt(id: Long): Option[Status]
}

以及以下地图:

private val chains: mutable.Map[Long, Tr] = mutable.HashMap()
private val commandsId: mutable.Map[Command, Long] = mutable.HashMap()
private val commands: mutable.Map[Long, Command] = mutable.HashMap()

我要实现方法:

def getStatus(cmd: Command) = commandsId.get(cmd).flatMap(chains.get).flatMap{tr => 
    tr.status(id) //fail, Id was lost at commandsId.get(cmd) step
}

有没有紧凑的写法?我想理解会有所帮助,但我不确定...

   commandsId.get(cmd)
     .flatMap { id =>
        chains.get(id).map(_.status(id))
     }

或者,for理解:

  for {
     id <- commandsId.get(cmd)
     tr <- chains.get(id)
     status <- tr.status(id)
  } yield status