First for the ternary operator.
ofs ? c.recordHit | c.recordMiss
Is to equivalent to:
if (ofs.isDefined) c.recordHit else c.recordMiss
Or:
ofs.fold(c.recordMiss)(_ => c.recordHit)
It's provided by Scalaz and implemented as an implicit class (OptionOps
) that provides a ?
method for Option
that returns a Conditional
class that has a |
method. This is a case where I'm not sure the extra concision is worth the confusion, but people use it, so it's worth knowing about.
For the state update, consider the following simplified example:
val inc: State[Int, Unit] = for { i <- get[Int]; _ <- put(i + 1) } yield ()
It might look at first like we're "discarding" the state and only yielding unit, but the value of inc
is actually the entire state-manipulating computation (in this case one that doesn't return any relevant or interesting value), including the update.