Monoid for case class?

392 views
Skip to first unread message

Ian Hummel

unread,
Feb 13, 2014, 9:44:27 AM2/13/14
to alge...@googlegroups.com
Hey guys,

Is there a way to get a monoids "automatically" for a case class, assuming each component of the case class has a monoid?

For example,

case class Impressions(load: Long, uniques: HLL)

There are monoids for both Long and HyperLogLog... so can I get a Monoid[Impressions] somehow (without the boilerplate of implementing it myself)?  I think this is what the ProductXMonoids do, but wasn't clear exactly how to wrangle the right implicit into scope.

Cheers!

Oscar Boykin

unread,
Feb 13, 2014, 1:20:25 PM2/13/14
to Ian Hummel, algebird
Yes, this is what the product implicits are for, and yes it is still a pain. When we switch to 2.10 only, this can be done with a macro, and we will add that.

you should do:

scala> import com.twitter.algebird.Semigroup
import com.twitter.algebird.Semigroup

scala> case class Test(x: Int, y: String)
defined class Test

scala> Semigroup.apply(Test.apply _, Test.unapply _)
res1: com.twitter.algebird.Semigroup[Test] = com.twitter.algebird.Product2Semigroup@730476e8

scala> res1.plus(Test(1, "hello"), Test(2, "world"))
res2: Test = Test(3,helloworld)

The apply, unapply line is unfortunate, but oh well.

What I want is a default implicit macro that can build these for any case class. Pull req?


--
You received this message because you are subscribed to the Google Groups "algebird" group.
To unsubscribe from this group and stop receiving emails from it, send an email to algebird+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Oscar Boykin :: @posco :: http://twitter.com/posco
Reply all
Reply to author
Forward
0 new messages