I have a problem with a solution, but somehow the feeling that I reinvented the wheel.
assert(createTest("ha".some.right, "ba".some.right) == \/-(Some(Test(Some("ha"),Some("ba")))))
assert(createTest("ha".some.right, none.right) == \/-(Some(Test(Some("ha"),None))))
assert(createTest(none.right, "ba".some.right) == -\/(Error))
assert(createTest(none.right, none.right) == \/-(Some(Test(None,None))))
assert(createTest(Error.left, "ba".some.right) == -\/(Error))
where any acquiring of data is an error, and on the right I get my datastructure,
if b is only set when a is set, when a is set, or when both are none.
If b is set without a it is an error.
I wrote:
def createTest(a: S[String], b: S[String]): S[Test] = {
val x = (a |@| b).tupled
if (x.isLeft) Error.left else {
x.flatMap {
case (Some(x), y) => Test(x.some, y).some.right
case (None, None) => Test(none, none).some.right
case (None, Some(y)) => Error.left
case _ => none.right
}
}
}
which feels clumsy. Is there a better way? I tried with applicatives and for comprehensions, but there was always a case which did not fit. My guess, is that I am mssing something useful.
Thanks for your help,
Markus