fn sum<A, F>(v: F) -> A where A: Monoid, A: Semigroup, F: Foldable<A, A=A> {
v.fold(A::mempty(), |b, a| A::mappend(&b, a))
}
Monoid
and Semigroup
independent, or does one require the other?
A: Monoid + Semigroup
pub trait Monoid: Semigroup { ... }
A: Semigroup
specifically, as A: Monoid
already implies that.
pub trait Semigroup: Clone {
fn mappend(self: &Self, other: &Self) -> Self;
}
pub trait Monoid: Semigroup {
fn mempty() -> Self;
}
Foldable<A, A=A>
and that works for my HKT type that i found on some gist
A
is A