Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

scalaz - Update operations on a Scala Case Class

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...