Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:56:10+00:00 2026-06-07T21:56:10+00:00

I have two instantiated case classes of the same type. case class Foo(x :

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:56:12+00:00Added an answer on June 7, 2026 at 9:56 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes, a Teacher class and a Student class. In my program
Let's say I have two classes that look like this: public class ByteFilter {
Say you have two classes, A and B. Is it possible to instantiate both
I have two columns say Main and Sub . (they can be of same
I have a case class which takes a list of functions: case class A(q:Double,
I have the following classes: public class MyItems : List<MyItem> { ... } public
I have two objects - one that contains some code with will fire an
For example I have two entities Class A { public Guid Id {get;set;} public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.