Fairly straightforward Scalaz solution (not very general)
You can use a semigroup instance to wrap up a lot of the details:
import scalaz._, Scalaz._
case class Foo(a: Option[String], b: Option[String], c: Option[String])
implicit object fooSemigroup extends Semigroup[Foo] {
def fromFoo(f: Foo) = (f.a.fst, f.b.fst, f.c.fst)
def toFoo(t: (FirstOption[String], FirstOption[String], FirstOption[String])) =
Foo(t._1.value, t._2.value, t._3.value)
def append(x: Foo, y: => Foo) = toFoo(fromFoo(x) |+| fromFoo(y))
}
Which gives us:
scala> val a = Foo(Some("foo"), Some("bar"), Some("baz"))
a: Foo = Foo(Some(foo),Some(bar),Some(baz))
scala> val b = Foo(None, Some("etch"), None)
b: Foo = Foo(None,Some(etch),None)
scala> b |+| a
res11: Foo = Foo(Some(foo),Some(etch),Some(baz))
Which I think is what you want, although it's not very general.
Scalaz + Shapeless solution
If you want something that works for all case classes (given the appropriate type class instances for members), you can use the following combination of Shapeless and Scalaz. Note that I'm drawing on missingfactor's answer and this example by Miles Sabin. First for some monoid instances:
import scalaz._, Scalaz._
import shapeless._, HList._
implicit object hnilMonoid extends Monoid[HNil] {
val zero = HNil
def append(a: HNil, b: => HNil) = HNil
}
implicit def hlistMonoid[H, T <: HList](
implicit mh: Monoid[H],
mt: Monoid[T]
): Monoid[H :: T] = new Monoid[H :: T] {
val zero = mh.zero :: mt.zero
def append(a: H :: T, b: => H :: T) =
(a.head |+| b.head) :: (a.tail |+| b.tail)
}
implicit def caseClassMonoid[C, L <: HList](
implicit iso: Iso[C, L],
ml: Monoid[L]
) = new Monoid[C] {
val zero = iso.from(ml.zero)
def append(a: C, b: => C) = iso.from(iso.to(a) |+| iso.to(b))
}
Next for the sake of simplicitly I'm just going to put the "First" monoid instance for Option
in scope, instead of using the FirstOption
wrapper as I did above.
implicit def optionFirstMonoid[A] = new Monoid[Option[A]] {
val zero = None
def append(a: Option[A], b: => Option[A]) = a orElse b
}
Now for our case class:
case class Foo(a: Option[String], b: Option[String], c: Option[String])
And the Iso
instance to convert it to an HList
and back:
implicit def fooIso = Iso.hlist(Foo.apply _, Foo.unapply _)
And we're done:
scala> val a = Foo(Some("foo"), Some("bar"), Some("baz"))
a: Foo = Foo(Some(foo),Some(bar),Some(baz))
scala> val b = Foo(None, Some("etch"), None)
b: Foo = Foo(None,Some(etch),None)
scala> b |+| a
res0: Foo = Foo(Some(foo),Some(etch),Some(baz))
You could use semigroups instead of monoids here as well and save a few lines, but I was trying to get away with as much copying and pasting from the shapeless/examples
code as possible, so I'll leave that as an exercise.
Performance
To address your comment about performance, here's a completely unscientific benchmark of the latter solution versus a standard library solution using orElse
(Scala 2.9.2, IcedTea7 2.2.1):
def add(x: Foo, y: Foo) = Foo(x.a orElse y.a, x.b orElse y.b, x.c orElse y.c)
def ros = if (util.Random.nextBoolean)
Some(util.Random.nextString(util.Random.nextInt(10))) else None
val foos = Seq.fill(500000)(Foo(ros, ros, ros))
def time(block: => Unit) = {
val start = System.currentTimeMillis
(block, System.currentTimeMillis - start)
}
And then after running each a couple of dozen times:
scala> Iterator.fill(10)(time(foos.reduce(add(_, _)))._2).sum / 10
res4: Long = 49
scala> Iterator.fill(10)(time(foos.reduce(_ |+| _))._2).sum / 10
res5: Long = 265
Somewhat surprisingly, the Shapeless-less Scalaz solution is a little slower:
scala> Iterator.fill(10)(time(foos.reduce(_.|+|(_)(fooSemigroup)))._2).sum / 10
res6: Long = 311
But as I said, this is an extremely off-the-cuff approach to benchmarking, and you should run your own (Caliper is a great library for this).
In any case, yes, you're paying for the abstraction, but not that much, and it's often likely to be worth it.