Ive found myself in the situation a couple of times where i have a reducer / combine fn like so:
def combiner(a: String, b: String): Either[String, String] = {
(a + b).asRight[String]
}
Its a dummy implementation but the fn can fail so it returns an either. I then I have a list of values I want to pass through this with reduce / fold. The best I can come up with (assuming the List's type is a monoid) is this:
def combine(items: Vector[String]) = {
items.foldLeft(Monoid[String].empty.asRight[String]) { case (acc, value) =>
acc.flatMap( accStr => combiner(accStr, value))
}
}
Its a bit clumsy and as its a fairly generic pattern I suspect there's a better way to do it using cats.