commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matthew Pocock <>
Subject Re: [Graph] On graph weight type(s)
Date Thu, 15 Dec 2011 12:37:40 GMT

On 15 December 2011 11:35, James Carman <> wrote:

> public interface BinaryOperation<T>
> {
>  public T execute(T operand1, T operand2);
> }
> Perhaps we can come up with an interface that combines the two
> aspects.  I'm trying to think of mathematically what that would be
> called.  By the way, what do you need to know "HasZero"?  A sum
> operation has to have a "zero", doesn't it?

The mathematical hierarchy goes: semigroup -> monoid.

You don't need a full group here as you are only interested in a single
operation, not a pair of interacting operations. There are several
JVM-hosted libraries that model this hierarchy to a greater or lesser
degree. scalaz uses:

trait Semigroup[S] {
  def append(s1: S
s2: => S): S <>

trait Zero[Z] {
  val zero: Z <>

trait Monoid[M] extends Zero
with Semigroup <>[M]

If you're not used to reading scala, here's the essentially equivalent
definitions in Java:

public interface Semigroup<S> {
  public S append(S s1, S s2);

public interface Zero<Z> {

  public Z zero();

public interface Monoid<M> extends

So, given that you have Ordering<O> already (or is that Comparator<C>?),
I'd say that Weight is defined as:

// insert comments here about the consistency of append and compare
public interface Weight<W> extends Monoid<W>, Ordered<W>

Of course, a sensible default implementation of Weight would delegate to
Monoid and Ordered instances and you can then pray to the gods of hotspot
to inline this all away.

public <W> Weight<W> weight(Monoid<W> m, Ordered<W> o) {
  new Weight<W>() {
    public W zero() { return; }


Dr Matthew Pocock
Integrative Bioinformatics Group, School of Computing Science, Newcastle
msn: drdozer
skype: matthew.pocock
tel: (0191) 2566550
mob: +447535664143

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message