I have a list of type List[Result[BigDecimal]] that I want to sum.
type Result[A] = OptionT[Future, A]
The rule would be that if there is any Future(None) then we get the result Future(None).
I have the function [1]:
def sum[A: Monoid](as: List[A]): A = {
val M = implicitly[Monoid[A]]
as.foldLeft(M.zero)(M.append)
}
However, I am missing the Monoid instance for Result[BigDecimal]. How do I define it using Scalaz?
I'm not sure why Scalaz doesn't provide this instance—it does provide a
Monoid[Future[A]]whereAhas a monoid instance, and where there's an implicit execution context in scope. You can easily make your own, though,either by defining an isomorphism between[this wouldn't actually have the desired semantics] by just defining one directly:ResultandFuture[Option[?]]and then usingIsomorphismMonoid, orAnd then (using Scalaz's own
suml, but yoursumwould work as well):For what it's worth, Cats provides this instance, but (like the
Future[Option[?]]instance in Scalaz) it hasNoneas identity.