12

I have two instantiated case classes of the same type.

case class Foo(x : Option[String], y : Option[String], z : Option[String])

Lets call the instantiated classes A and B.

val a = Foo(x=Some("foo"), y=Some("bar"), z=Some("baz"))
val b = Foo(x=None, y=Some("etch"), z=None)

I'm wondering if its possible to update case class A with B in a single operation in a generic way.

val c = b *oper* a // produces Foo(x=Some("foo"), y=Some("etch"), z=Some("baz")) 

with parameters that are set as None ignored. Ideally the operation should also be generic so it can act on any type of case class.

I have some intuition that it might be possible to do this with Scalaz by converting the class into a tuple/list first and converting back to a class after the operation is complete - perhaps using the ApplicativeBuilder? Am I thinking about this in the right way? Any ideas?

8
  • Your example doesn't compile.
    – Landei
    Commented Jul 11, 2012 at 20:23
  • 1
    I'm sorry? What exactly doesn't compile? The code snippet? :) Commented Jul 11, 2012 at 20:58
  • possible duplicate of Updating a scala case class Commented Jul 12, 2012 at 0:19
  • @Rasputin Jones: You're not only using a and b both as variable names and named arguments, but also pass a String where a Option[String] is expected. This will confuse especially beginners.
    – Landei
    Commented Jul 12, 2012 at 6:47
  • The variable names are not as problematic as the wrong types. The latter will not compile. See my edit...
    – kiritsuku
    Commented Jul 12, 2012 at 10:25

2 Answers 2

24

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.

0
12

As discusssed in this thread, this is how you can solve the problem with shapeless, in a completely type-safe manner.

scala> import shapeless._
import shapeless._

scala> import HList._
import HList._

scala> case class Foo(a: Option[Int], b: Option[Int])
defined class Foo

scala> val a = Foo(Some(3), None)
a: Foo = Foo(Some(3),None)

scala> val b = Foo(Some(22), Some(1))
b: Foo = Foo(Some(22),Some(1))

scala> implicit val fooIso = HListIso(Foo.apply _, Foo.unapply _)
fooIso: shapeless.HListIso[Foo,shapeless.::[Option[Int],shapeless.::[Option[Int],shapeless.HNil]]] = shapeless.HListIso@11c5b77

scala> type O2[+A] = (Option[A], Option[A])
defined type alias O2

scala> object mapper extends (O2 ~> Option) {
     |   def apply[A](x: O2[A]): Option[A] = x._1.orElse(x._2)
     | }
defined module mapper

scala> fooIso.fromHList(fooIso.toHList(a).zip(fooIso.toHList(b)).map(mapper))
res13: Foo = Foo(Some(3),Some(1))
1
  • I've added a Shapeless example inspired by your answer in my answer—I hope you don't mind. Commented Jul 12, 2012 at 15:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.