I have a situation where I want to perform a series of operations (with side-effects) where I am only interested if they produced an error or not, and therefore their return type would be an Option[Error]
.
I want to chain several of those operations in a for comprehension, which leads to something like that:
def foo(data: Data): Option[Error] = {
val result = for {
_ <- op1(data).toLeft(())
_ <- op2(data).toLeft(())
} yield ()
result.swap.toOption
}
def op1(data: Data): Option[Error] = ???
def op2(data: Data): Option[Error] = ???
It works but it seems not right. Is there a better way to implement something like that?