The code below works but pattern matching on Option b doesn’t look elegant.
Is there a way to avoid it while keeping the same semantics?
object A {
def apply(b: B): ValidationNEL[String, A] = ...
}
case class C(i: Int, a: Option[A])
object C {
def apply(i: Int, b: Option[B]): ValidationNEL[String, C] = b match {
case None => Success(C(i, None))
case Some(sb) => A(sb).map(bb => C(i, Some(bb)))
}
}
Using scalaz, you can fold over Option
There’s probably a way of simplifying the
sb => A(sb) map (bb => C(i, some(bb)))using pointfree style but this is often ugly in scala:Setup
First impl
Second Impl
If you declare first-class functions, you stand a better chance of composition. For example:
Then